Python built-in object

This time I posted about the main built-in objects of Python. First of all, Python has built-in object types as standard, but it seems that the most commonly used ones are numbers, strings, lists, dictionaries, tuples, files, etc. When I actually implemented Stack and Queue, which are the simple data structures I posted earlier, I used numbers, strings, and lists, but when it comes to more flexible and complex ones, I also use tuples and files a lot. I think it will be. It seems that Python does not have primitive types, and unlike primitive types in JAVA etc., methods etc. can be used in built-in objects.

Numerical value

As the name implies, numbers are objects that handle numbers and are divided into five types: short integers, long integers, floating point numbers, octal and hexadecimal numbers, and complex numbers. It seems that long integers have the advantage of being able to increase the size to the limit of memory, but I think that it is hard to use. I think that operators are almost always necessary when dealing with numerical values, so I will introduce them.+,-,*,%Can be treated with the same symbols as in other languages, but the power of power is calculated by superimposing ** and asterisk in C and JAVA.^It's different. For other bit operations<<,>>Bit shift with&,|,^And AND,OR,It seems that XOR bit operations can also be performed. set()It seems that you can also make a set using|’To combine sets,&It seems that you can also calculate the intersection of sets. After that, it seems that you can do various math calculations by importing a module called math, so I will link it at the end.

String

Strings are immutable objects, and it seems that they are surrounded by single quotation marks ('), but double quotation marks (") are also possible. In other common languages, single quotation marks are characters (char). It is used when dealing with the type of, but it is recognized as a string in Python. So what if you want to use single or double quotes in the string as part of the string? Enclose it in singles. In that case, if you include a double in the character string, it will be recognized as a character, and vice versa. In addition, you can use the backslash'' and put it in the character string as''','\ "'and it will be recognized as a character. Will be done. Backslash can use many other functions, so I will only pick up the ones that you are likely to use frequently. '\ n'is used to start a new line in a character string, and'\ t'is used to insert a tab in a character string. '\', this is included when the backslash is recognized as a character. I'll write code for other features later, and I'll explain them one by one as I use them. It seems that there are raw strings and unicode strings, but I will save these when I use them later. I don't think it's a string object, but Python has triple quotes ("" "), which will be recognized as comments if you enclose the code, so the code won't be executed. There is a code for formatting in the string, but I will put a link at the end of the post so please refer to it, or I will explain it lightly when using it. Immutability means that once you create the object, you can't make any changes to it. So what if you want to make changes to a string? Has a feature called sequence, and numbers from index 0 to n-1 are assigned in order from index 0 with the character length as n. I think this is the same as array in C or JAVA. It's easy to understand if you get it. I think that it is a feature of Python that you can perform an operation called slicing using this feature, but what is slicing? Slicing is a new character string object that extracts a part of the character string. The act of creating a string. Here is an example.

Slicing.py


#Create and print string objects
s = 'I am tired!'
print s

#Get the length of a string
print len(s)

#Get one character from one index of a string
print s[0]

#Slicing to get part of a string does the same for all of the following
print s[5:11]
print s[5:]
print s[5:len(s)]

#Copy the string and change part of the content(Two newly generated
#Create a single string object by concatenating string objects)
print s[:5] + 'fine!'
s = s[:5] + 'fine!'
print s

If you create one string object by connecting two new string objects as in this last example, you can create an object that looks like it was changed without changing it to the original object, and this is the original variable. You can do the same thing as changed by substituting for. It seems that this operation can also be done with replace () of the method of the string object, but since I will not mention the method here, if there is an interesting method to use, I will explain it at that time. By the way, if you want to include the last character when slicing, the length of the character string is n, but if you want to access that character alone, use n-1 as described above.

list

A list is also a sequence type object, and you can think that the basic concept is the same as a character string. However, there are two major features that are different from character strings. The first is that things that can be saved become objects. In the string, one character is saved in one index, but in the list, any object can be saved in one index. Therefore, since the list itself is an object, it is possible to save the list in the list. For example, suppose you have Listing A and Listing B. You can put List B in index 0 of List A and access List B from List A to retrieve the elements of List B. This is a convenient technique that plays the same role as a two-dimensional array and can also be used for calculations using matrices. The second is that it is a variable object. Unlike strings, lists can change the shape of the object itself without making a copy. For example, append () can be used to add a new element to the end of the list to increase the length of the list by one, and pop () can be used to extract and shorten the first element of the list. However, since it is a sequence type, slicing and combining with character strings can also be used. As a caveat, it seems that the list may change unintentionally when writing a program. Below is an example of access to a simple two-dimensional list element.

Slicing.py


#Create 3 lists
B1 = [1,2,3]
B2 = [4,5,6]
B3 = [7,8,9]

#Save the list created above to the list
A = [B1,B2,B3]

#Check if a two-dimensional list has been created
print A

#Access the value in the middle of the two-dimensional list (output 5)
print A[1][1]

dictionary

The dictionary is also an object for storing objects. However, the dictionary saves in the object in a messy way, unlike saving in a fixed order of sequence. So every time I print a dictionary element, the order of the elements is displayed differently. Then, how to access it is to give a keyword for the element when saving the element (object). When you want to retrieve an element, specify a keyword to retrieve the element. Therefore, you may feel that the genre you see at the bookstore is a dictionary and the order of Aiueo in the genre is a system like a list.

Tuple

Tuples are invariant sequence objects and can be thought of as almost the same as lists. However, because it is immutable, it is necessary to create a new tuple object using slicing etc. like a character string when making changes, and since there are no methods etc., it is functionally a low-performance version of the list. I think it's good. So do you need tuples here? You may be wondering, but it doesn't happen in tuples that the notes in the list say that sometimes changes are made unintentionally. I think there are many useful methods if you use it according to the characteristics here. By the way, when I made a tuple with JAVA, I made it because there was a case that I had to use one set of elements for the array.

Other

The file object is not well understood, so I will explain it when I use it from now on. I don't think Bool needs to explain. It seems that various objects other than these are prepared, but since they are not specifically mentioned in the reference book, I would like to explain each time within the scope of my understanding.

This is the end of the memo of the main objects, so from the next time I will post it all together after reading the statement (format?) Chapter. Also, if you have any mistakes in the post, please point them out.

math module: http://docs.python.jp/2/library/math.html String format (5.6.2): http://docs.python.jp/2/library/stdtypes.html#string-formatting

Reference: First Python 3rd Edition (Publisher: O'Reilly Japan)

Recommended Posts

Python built-in object
Python built-in object
Object oriented in python
Python built-in functions ~ Zip ~
About Python3 ... (Ellipsis object)
Wrap Python built-in functions
String object methods in Python
[python] Value of function object (?)
Null object comparison in Python
Various Python built-in string operations
Python variables and object IDs
[Ubuntu] [Python] Object tracking using dlib
python dict object memorandum (mysterious document)
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Python comprehension
Python technique
Studying python
Build an environment for Blender built-in Python
Python memorandum
Python FlowFishMaster
Python service
[Python] Attribute Error:'list' object has no attribute'replace'
python tips
python function ①
Python basics
Python memo
Conquer 69 Python built-in functions 6th p ~ r
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Create a JSON object mapper in Python
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
[Python3] Rewrite the code object of the function
Start python
Note: Python
Built-in functions
Python basics ③