list
A list is an ordered collection of elements that can be added and removed at any time. Unlike java, you can use arr[-1] 0>-x >=- len(arr) to index the numbers 0~ len(arr)-1 -len(arr)~ -1. If you exceed that, you will get an error.
classmates= ['A','B','C','D','E'] print(classmates) print(len(classmates)) for i in classmates: print(i) ''' ['A', 'B', 'C', 'D', 'E'] 5 A B C D E ''' for i in range(0,len(classmates)): print(classmates[i]) ''' A B C D E ''' classmates= ['A','B','C','D','E'] print(classmates[-1]) print(classmates[-2]) print(classmates[-3]) print(classmates[-4]) print(classmates[-5]) ''' print(classmates[-6]) IndexError: list index out of range ''' ''' E D C B A '''
A list is a mutable ordered list, so you can append elements to the end of the list:
append() method
Insert in a position
insert(x,'???')
To delete an element at the end of a list
pop(i) defaults to last without arguments
classmates= ['A','B','C','D','E'] ('F') (1,'G') (-1,'H') (1,'I') print(classmates) for i in range(0,len(classmates)): (0) print(classmates) ''' ['A', 'I', 'G', 'B', 'C', 'D', 'E', 'H', 'F'] ['I', 'G', 'B', 'C', 'D', 'E', 'H', 'F'] ['G', 'B', 'C', 'D', 'E', 'H', 'F'] ['B', 'C', 'D', 'E', 'H', 'F'] ['C', 'D', 'E', 'H', 'F'] ['D', 'E', 'H', 'F'] ['E', 'H', 'F'] ['H', 'F'] ['F'] []
To replace an element with another element, you can directly assign the value to the corresponding index position: change value any attribute
classmates= ['A','B','C','D','E'] classmates[0] ='F' print(classmates) ''' ['F', 'B', 'C', 'D', 'E'] '''
A list element can also be another list, and the data types of the elements inside the list can be different.
p = [1,2,3] classmates= ['A',p,True ] print(classmates)
tuple
tuple and list are very similar, but tuple once initialized can not be modified, more secure compared to the list does not append () pop () insert () and other methods
classmates = ('Michael', 'Bob', 'Tracy') The difference from list is the brackets [] / ().
Note: This is because the definition of the tuple type is confused with the () of the quadratic operator.
false_t = (1) # It's not tuples that are defined, it's the number 1! print(false_t) true_t = (1,) # Tuples with only 1 element must be defined with a comma,, to disambiguate: print(true_t) true_t1 = () print(true_t1) ''' 1 (1,) () '''
Although the tuple is immutable as follows
p = ['Male',20] change_t = (1,'czncai',p) print(change_t) p[0] = 'Slugger' p[1] = 21 print(change_t) ''' (1, 'czncai', ['Male', 20]) (1, 'czncai', ['Slugger', 21]) '''
Conditional judgment + input()
input('Please enter a number')
if boolean expression :
elif :
else :
circulate
for x in list :
is to substitute each element of the list into the variable x and then execute the indented block statement
for x in range() The range() function, which generates a sequence of integers
list(range(5)) [0, 1, 2, 3, 4]
while boolean expression :
n = n+1 b = true
break continue
- break allows you to exit the loop prematurely.
- continue skips the current loop and starts the next one.
This is the whole content of this article.