Python data type: int, string, float, Boolean
mutable variables: List
Immutable variables: string, tuple tuple
1.list
List is lists, array, arrays
The list is based on the subscript (0123), and the subscript is also called index, corner label, number
New_stus =[' Andy Lau ', ' Carina Lau ', ' Sun Li ', ' Fan Bingbing ']
The first element subscript is 0, the last subscript is-1, which means 0 is Andy Lau, 1 is Fan Bingbing
Print (New_stus[0]) print (new_stus[-1])
Increase:
Append is to add an element at the end of the list
Citys=[]citys.append (' Beijing ')
Insert can add elements at a specified position (subscript), 0 is the No. 0 position
Citys.insert (0, ' Shanghai ')
By deleting:
Pop Delete an element, delete-1 is the last element to delete
Citys.pop (-1)
Remove deletes the specified element
Citys.remove (' Beijing ')
Clear is clear
Citys.clear () #清空print (citys)
Del deletes the specified position element
Del Citys[-1]print (Citys)
Modified: Modified by subscript
citys[0]= ' Nanjing ' #修改的时候, if the specified subscript does not exist, will be an error
Check:
Print (Citys[0]) #指定位置print (Citys.index (' BJ ')) #获取元素的下标, if the element is not found, it will error print (Citys.count (' BJ ') #获取元素的数量print ( Citys.reverse ()) #就是把list字符串反转一下, but nothing returned print (citys)
List Loop value:
#list循环lists1 = [' Andy Lau ', ' Fan Bingbing ', ' Nicholas Tse Feng ', ' Faye Wong ', ' Li Bingbing ', [11,33,6,7]]for name in Lists1: if Type (name) ==list: for i in Name: Print (i) print (name)
2. Dictionaries
Dictionaries are key--value forms, dictionaries are unordered.
The dictionary both uses in to judge whether the key exists
Advantages: 1. Easy access to data
2. Fast speed
Infos = { ' name ': ' Andy Lau ', ' sex ': ' Male ', ' addr ': ' Hong Kong ', ' age ': 18}
Check:
Get
Print (Infos.get (' name ')) print (Infos.get (' II ')) #如果ii取不到, which is Noneprint (' II ') #如果ii取不到, specify the value as 110
Brackets []
Print (infos[' name ')) print (infos[' ee ')
Increase:
infos[' phone ']=1242132556 #增加一个keyinfos. SetDefault (' girlfriend ', ' hehe hey ') # key Valueinfos.setdefault (' name ', ' Nicholas Tse Feng ') # The value cannot be modified if key exists
Modify:
infos[' name ']= ' Nicholas Tse Front ' #如果key存在的话, you can modify the value
Delete:
Pop Delete specified key
Infos.pop (' name ') #指定key删除
Popitem Random Deletion
Infos.popitem () #随机删除一个key
Del Delete the specified key
Del infos[' phone '] #指定key来删除
Clear Empty Dictionary
Infos.clear () #清空字典
Method:
Get the key/value of a dictionary
Print (Infos.values ()) #获取到字典所有的valueprint (Infos.keys ()) #获取到字典所有的keyprint (Infos.items ()) #获取到字典多有k-V
3. Tuples
If there is only one element in the tuple, then you must add a comma behind the element
T= (, ' e ', ' a ')
Print type
Print (Type (t))
Find the element subscript
Print (T.index (2)) #找到元素的下标
Number of elements to find
Print (T.count (2)) #找到元素的个数
4. Slicing
A slice is a type of French that lists the values, and the same applies to strings, where they are concatenated with colons
Print (Nums[1:3]) #切片定义: Want to take a number of values from the list Gu Tou regardless of the tail 1 means that the print (nums[1:]) from position 1, the end of the end, the end can not write print (nums [: 2]) #如果从头开始去, take to the end of one of the following subscript, then the beginning can not write print (nums[:]) #取到所有
Lis cast to List
# lis = List (range (1,21)) print (LIS) print (lis[:11]) print (Lis[:11:2]) #2是步长 means to take one time from the default if the step is a positive number Left-to-right value print (Lis[:11:-1]) #步长是 1 If the step is negative from right to left
List reversal
Lis.reverse () #反转, changes the value of list new_list = Lis[::-1]print (lis[::-1]) #反转, produces a new list, does not change the original value
Python Data type details