Tuple tuples
Tuples are called read-only lists, where data can be queried but cannot be modified, so the slicing of strings also applies to tuples. Example: ("A", "B", "C")
tu= ('qiaofeng', 2,'xuzhu', True)print(tu [2:])
Tuple child elements cannot be changed, but child-to-child elements may change
tu= ('wuyazi', 2,['qiaofeng','xuzhu' ,'duanyu'],true) tu[2].pop (1)print(TU)
Ture converted to 1 in dictionaries and tuples
Tu = (wusir', 'taibai', True)print( Tu.count (1))
Lists List
The list is one of the underlying data types in Python, and other languages have data types similar to lists, such as JS called arrays, which are enclosed in [] and each element is separated by commas, and he can store various data types such as:
Li = [' Alex ', 123,ture, (Wusir), [All-in-all, ' xiaoming ',],{' name ': ' Alex '}]
Compared to strings, lists can store not only different data types, but also large amounts of data, 32-bit Python is limited to 536,870,912 elements, and 64-bit Python is limited to 1.,152,921,504,606,85e,+18 elements. And the list is ordered, there are index values, can be sliced, convenient to take value.
1. Increase
Append increase to the end , only one element can be added at a time
Li = [1,'a','b', 2,3,'a'] Li.append ('aaa'# added to last # added to last Print (LI)
Insert Insert, increase by index to add only one element at a time
Li = [1,'a','b', 2,3,'a'] Li.insert (# to add print(LI) by index
extend tail insertion, iteration increment
Li = [1,'a','b', 2, 3,'a']li.extend (['q,a,w'])#the increment of iterationLi.extend (['q,a,w','AAA']) Li.extend ('a') Li.extend ('ABC') Li.extend ('A,b,c')Print(LI)
2. By deleting
Pop can only be deleted by location, there is a return value , only one element can be deleted at a time
li = [1, " a , " b ", 2,3," a " ]l1 = Li.pop (0) # Delete by location, with return value print (L1 )
Li = [1,'a','b', 2,3,'a'] Li.pop () # default Delete last element print(LI)
Remove Delete by element, no return value
Li = [1,'a','b', 2,3,'a'] Li.remove ('a'# to delete print(li) by element
Clear Clear
Li = [1,'a','b', 2,3,'a'] Li.clear () # empty list print(LI)
Del
1: Delete list
Li = [1,'a','b', 2,3,'a'] del liprint(LI)
2. Can be deleted according to the slice, index, no return value
# Li = [1, ' A ', ' B ', 2,3, ' a '] # del li[0] #按照索引去删除 # del Li[1:3] #按照切片去删除 # del Li[1:5:2] #加步长 # print (LI)
3. Change
Li = [1,'a','b', 2, 3,'a']li[1] ='Dfasdfas' #follow the index to changePrint(LI) li[1:3] = ['N','k']#follow the slices to changePrint(LI)
4. Check
By slice, index, or loop
Li = [1,'a','b', 2,3,'a']a =li[2]b=li[0:3]print(a)print(b)
Li = [1,'a','b', 2,3,'a'] for in Li: print(i)
5. Other operations
count (number) (method counts the number of occurrences of an element in the list)
A = ["q","w","q"," R ","t","y"]Print (A.count ("q"))
Index (method used to find the index position of the first occurrence of a value from the list)
A = ["q","w","R"," T ","y"]print(A.index ("R "))
Sort (method used to sort the list at the original location)
ls = [1,3,6,8,7,4,2,9,10]ls.sort ()print(LS)
# sort (reverse) t from big to small F to big
Ls.sort (Reverse=true)
Print (LS)
Reverse (method to reverse-store elements in a list)
A = [2,1,3,4,5]a.sort ()# He has no return value, so it can only print aprint(a) a.reverse ()# He also has no return value, so it can only print a Print (a)
Nesting of lists
1L1 = ['Xiao Xin','Egon','Wusir', [99,'Tiabai','Ritian', [2,3]],2]2L1[1] = l1[1].capitalize ()3L1[1] ='Alex'4 Print(L1)5L1[3][1] = l1[3][1].upper ()6 Print(L1)7L1[3][0] = str (l1[3][0] + 1)8 Print(L1)
Others (For,enumerate,range)
For Loop : The user iterates through the contents of an object in order.
msg ='old boy Python is the best Python training organization in the country' forIteminchmsg:Print(item) Li= ['Alex','Silver Corner','Goddess','Egon','Taibai'] forIinchLi:Print(i) DIC= {'name':'Taibai',' Age': 18,'Sex':'Mans'} forKvinchDic.items ():Print(K,V)
Range: As a list of ranges, the elements in the list are numbers, and the valid numbers are controllable
for in range (1,10): print(i) for in range (1,10,2): # step print(i)for in# Reverse Step Print(i)
Python Basics Four