1. Number (int)
numbers are divided into integers and floats, declaring variables in Python without declaring them so that they will recognize
A = ten #整型a1 = 1.24 #浮点型
Supports scientific notation and replaces 10 with E
2. String (str)
Quoted in Python is a string, and single and double quotes are no different
A = "string" a1 = ' string ' a2 = ' 123 '
A string is an immutable object, that is, once it is created in memory that cannot be modified, if the modification or connection string re-opens up space
" string " a.replace ("s","b")
In this case we replace the "s" in A, and the "btring" is output in the Python interpreter, but when we print (a) we will still output "string", meaning that a does not change
In the string, we need to remember some common methods
" Hello " "_"# connect print(r) in "_" mode h_e_l_l_o
" Hello " = Test.split ("e") #将字符串以e来分开 print(r) [' h ' ' Llo ']
Test = "hello"r1 = Test.find ("l") # finds " L "First position r2 = test.find ("l", 3) # starting from position 3 Print (R1,R2)2 3
""print= test.strip () #去掉字符串两边的空格 print(r) Hello Hello
Test = "hello"r = test.upper () #将字母大写 print(R) HELLO
" HELLO " = Test.lower ()# the letter lowercase casefold can achieve the same effect and, more powerful, print(r) Hello
" Hello " = Test.replace ("l","a") #将l Replace with aprint(r) Heaao
" Hello " = test[0] # The following index v1 = test[0:2] #切片
v2 = test[0:-1] #在此0位可以取到, 1 bits is undesirable, that is, the range of values for this slice is 0<= <-1
Print (V,V1) H He
" Hello " Print # Len in Python3 to get the length of the string in Python len to get the byte length (bytes length is determined by encoding)5
Note:
Len and join can also be used for other data types
For example: List
3. Lists (list)
Lists are the concept of arrays, and list is an ordered set, which is mutable and can be arbitrarily deleted and added
A = ["Liu","Yong"] # one-dimensional array " Liu ", ["Zhang","li"]," Yong " # two-dimensional arrays
Can a[0],a[-1], negative is the index from behind
The list index is starting from 0, and the first bit of the list is 0 bits.
There are some common methods in the list
A = ["Liu","Yong"]a.append ("Nihao # on the last side add print(a) ['Liu' 'Yong'nihao']
A = ["Liu","Yong"]a.insert (1,"wo "# Add print(a) ['Liu'] at the specified location 'wo'Yong']
A = ["Liu","Yong"]a.pop () # Delete last one
Print(a)
A.pop (0) #删除指定位置
Print (a)
['Liu']
["Yong"]
A = ["Liu","Yong""wo" # Specify location to replace print(a) ['wo' Yong']
Z4. Tuples (touple)
Tuples are similar to lists, but the difference is that once a tuple is defined, it cannot change
A = ("ni","hao"
But if the tuple contains list is still mutable, but the tuple is still immutable, we can understand the variable and immutable as memory point, if a tuple points to a list then no matter how the list inside changes the position of the list in memory will not change
Note:
You can use parentheses in the outer layer of the tuple, or you can use no
This must be defined when defining a single tuple
A = (1,)
5. Dictionary (DIC)
The dictionary corresponds to a map that has a key and a value in DIC, but dic in Python simplifies the map
A = {"Liu": 99,"Li": 85}Print(a["Liu"])#value if no direct error exists for the key being takenPrint(A.get ("Liu"))#value if not present returns nonea["Nihao"] = 100#Store value in dic
"Li" in a #判断li是否存在a中
1, CMP (Dict1, DICT2): compare two dictionary elements.
2. Len (dict): Calculates the number of dictionary elements, that is, the total number of keys.
3, str (dict): output dictionary printable string representation.
4. Type (variable): Returns the type of the input variable and returns the dictionary type if the variable is a dictionary.
5, D.pop ("key") Delete key
1, Radiansdict.clear (): Delete all elements in the dictionary
2, Radiansdict.copy (): Returns a shallow copy of a dictionary
3. Radiansdict.fromkeys (): Create a new dictionary with the key of the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionary
4, Radiansdict.get (Key, Default=none): Returns the value of the specified key if the value does not return the default value in the dictionary
5, Radiansdict.has_key (key): If the key in the dictionary dict returns True, otherwise returns false
6, Radiansdict.items (): Returns a traversed (key, value) tuple array in a list
7, Radiansdict.keys (): Returns a dictionary of all keys in a list
8, Radiansdict.setdefault (Key, Default=none): Similar to get (), but if the key does not already exist in the dictionary, the key will be added and the value will be set to default
9, Radiansdict.update (DICT2): The dictionary dict2 key/value pairs updated to Dict
10. Radiansdict.values (): Returns all values in the dictionary as a list
Python Basic data type