The fifth day of python (Review data types), python Data Types
Supplement the Data Types of previous days
1. Integer int
A. Several input methods
A = 123
A = int (123)
When we input a = 123 on the device, the Python actually converts it to int (123 ), the following brackets are actually called _ init _ inside Python __
B .int internal optimization
In the following case, they share an address in the memory:
N1 = 123
N2 = n1
In this case, they use different addresses in the memory for storage.
N1 = 123
N2 = 123
However, in Python, he will optimize the number between-5----127 and think that they are commonly used numbers. Therefore, they will still use the same memory address for storage in the program.
C. length constraints
Different operating systems have different int lengths.
32bit-2 ** 31-----------2 ** 31-1
64bit-2 ** 63-----------2 ** 63-1
But for Python, if the number we use exceeds the maximum number of int, it will not report an error, but will convert it to a long integer. That is, l is added to the end of the number.
The range of the long integer can be said to be infinitely large. As long as you increase the memory size, the larger the size, the more memory will be consumed.
2. String and str
A. Creation Method
S1 = "rasir"
S2 = str ("rasir ")
B. special features that should be remembered
S1.strip () ---------------------------- remove spaces at both ends of the string
S1.startswith () -------------------- in the search string, check whether it is the same as the one you searched.
S1.find () ----------------------------- search for a character in a string
S1.replace () ------------------------- replace a sequence in the string with the character you want to replace
S1.upper () --------------------------- convert all letters in the string into uppercase letters.
S1.isapha () --------------------------- check whether the string contains only letters. If only letters exist, True is returned. Otherwise, False is returned.
C. Public Functions
Index: only one element can be retrieved.
Slice: Multiple elements can be obtained.
Encoding,
Example:
Temp = "hello"
For I in temp:
Print (I)
Bytes_list = bytes (I, encoding = 'utf-8 ')
Print (bytes_list)
For I in bytes_list:
Print (I)
Print (bin (I ))
Output result:
You
B '\ xe4 \ xbd \ xa0'
228
0b11100100
189
0b10111101
160
0b10100000
Good
B '\ xe5 \ xa5 \ xbd'
229
0b11100101
165
0b10100101
189
0b10111101
Summary: For UTF-8 encoding, Chinese characters are stored in three bytes and expressed in hexadecimal notation, while gbk encoding is stored in two bytes.
For versions 2.7 and 3.7, when the for loop outputs Chinese characters, versions 2.7 are output in bytes, while versions 2.7 are output in characters.
From the example above, we can see that the for Loop will automatically convert other hexadecimal values to decimal values.
Conversion between d. str and Bytes
(1) convert a string to a byte:
A = 'xiaobai'
N1 = bytes (a, encoding = 'utf-8 ')
N2 = bytes (a, encoding = 'gbk ')
Print (n1)
Print (n2)
Output result:
B '\ xe5 \ xb0 \ x8f \ xe7 \ x99 \ xbd'
B '\ xd0 \ xa1 \ xb0 \ xd7'
(2) convert bytes into strings
A = 'xiaobai'
N1 = bytes (a, encoding = 'utf-8 ')
N2 = bytes (a, encoding = 'gbk ')
New1 = str (n1, encoding = "UTF-8 ")
New2 = str (n2, encoding = 'gbk ')
Print (new1, new2)
Output result:
Xiao Bai
Summary: Bytes can be forcibly defined to create Bytes, or can be used to convert other types to Bytes.
Str can be used to forcibly define a string or convert other types of objects to a string.
For man UTF-8, it is stored in three bytes, while GBK is stored in two bytes.
3. list: list
A1 = ['cui', 'rain', 'Sir '] -------------- create a list
A2 = list () ------------------------------- create an empty list
A2 = list (['cui', 'rain', 'Sir '])
Def _ init _ (self, seq = () ----------------------------------------- it can be iterated on the converted object (the only thing that can be iterated is the for loop)
(1) convert string to list
Example:
A = 'xiaobai'
A1 = list (a) --------------------------- a is equivalent to a for loop in the brackets.
Print (a1)
Output result: ['小', 'white']
(2) convert tuples to lists
Example:
A = ('cui', 'rain', 'Sir ')
A1 = list (a) ---------------------------------------------------- for loop, the elements of each loop are treated as list elements
Print (a1)
Output result:
['Cui', 'rain', 'Sir ']
(3) convert a dictionary to a list
Example:
A = {'rain ': 'Sir ',
'Any': 'sea '}
A1 = list ()
A2 = list (a. values ())
A3 = list (a. items ())
Print (a1)
Print (a2)
Print (a3)
Output result:
['Rain', 'any']
['Sir ', 'sea']
[('Rain ', 'Sir'), ('any', 'sea')]
A. Some Special List Functions
A1.clear () --------------------------------------------------------------------- clear list Function
A1.append () ------------------------------------------------------------------- append an element
A1.extend () -------------------------------------------------------------------- append an element by Iteration
A1.insert () -------------------------------------------------------------------- add elements at the specified position
A1.reverse () ------------------------------------------------------------------- reverse the list
B. Common Features
Can all be indexed ---------------- it must be an element after the index
Can be sliced ------------------ for slice, if it is a character before the slice, it is also a character after the slice, if it is a list before then it is also a list
Can all be for Loop
C. nested Indexes
A1 = ['rain ', 'Sir', 'sea', 123, {'aaa': 'bbb ', 'ccc': {'ddd ': 'Eee '}]
A2 = a1 [4] ["ccc"] ['ddd ']
4. yuanzu. tuple
A. Create and convert
A1 = ('cui', 'rain', 'Sir ')
A2 = tuple (a1) ----------------------------------- can also be iterated
A3 = tuple ('cui', 'rain', 'Sir '))
B. Basic Features
Count ------------------------------------------------ count the number of elements
Index ------------------------------------------------ query an element and return to the position
C. The element cannot be modified.
A1 = ('cui', 'rain', 'Sir ', ['hell', 'kekeke'])
Print (a1)
A1 [3]. append ('stop ')
Print (a1)
Output result:
('Cui', 'rain', 'Sir ', ['hell', 'kekeke'])
('Cui', 'rain', 'Sir ', ['hell', 'kekeke', 'wait'])
Summary: For the ancestor, his element cannot be modified, but the content in the element can be modified. For example, if we want to modify the element 'cui', no.
Important: Generally, the original content of a string is not changed when a function is used. For list, tuple, and dict, the original object is generally modified.
5. dictionary and dict
A. Create and convert
N1 = {'rain ': 'Sir', 'Tan ': 'aaa '}
N1 = dict ({'rain ': 'Sir', 'Tan ': 'aaa '})
List to string
A1 = ['rain', 'Sir ']
A2 = dict (enumerate (a1 ))
Print (a2)
B. Basic Features
N1.fromkeys () -------------------------------------------------- is used to create a new dictionary.
If a method has the word staticmethod on it, it is called by class. Normally, we use an object to call the method.
Example:
N1 = dict. fromkeys (['k1', 'k2', 'k3 '], [])
Print (n1)
N1 ["k1"]. append ('bbb ')
Print (n1)
Output result:
{'K1 ': [], 'k2': [], 'k3': []}
{'K1 ': ['bbb'], 'k2': ['bbb '], 'k3': ['bbb ']}
Conclusion:
For fromkeys, the vlan values created later use the same memory address. This explains why other values are added after the Value of k1 is added.
N1.get () returns the Value based on the key Value. Unlike the index, if the key Value is not available, get returns a null Value for it, and the index does not report an error, here, d is the default filling value. When vlaue is not available, we can use the default value to fill it.