A sequence is a data structure that contains elements that are numbered (starting at 0). A typical sequence consists of a list, a string, and a tuple. Where the list is mutable, and the tuple and string are immutable.
A personal feeling list is similar to an array in C, but it does not, for example, the types of elements in an array are the same, and the types of elements in the list can be mixed.
Index (subscript):
As with arrays in the C language, the list supports subscript access, with subscripts to access the elements in the list.
#sequence Assignmentx = [1, 2, 3,"Hello",'ABC']Printx#Subscript AccessPrintX[0]PrintX[3]#Modify the contents of an element by subscriptX[0] ="zzzzz"Printxx[2] = 999PrintX[2]
>>>
[1, 2, 3, ' hello ', ' abc ']
1
Hello
[' Zzzzz ', 2, 3, ' hello ', ' abc ']
999
Sharding:
With a shard operation, you can access part of a sequence where the Shard requires two index numbers to indicate the start and end of the Shard.
Strbuff ="www.baidu.com"#The string is truncated from subscript 4 to the end of subscript-4 (right-to-left subscript 4)Strbuff = strbuff[4:-4]Printstrbuffnnumbers= [1, 2, 3, 4, 5, 6]#access to three elements after a shardPrintNnumbers[-3:]#or subscript from left to rightPrintNnumbers[3:]PrintNnumbers[3:5]#Shard Access, if the right subscript appears to the left of the subscript on the left, an empty list will be returned" "Right-to- left subscript: -6-5 -4-3 -2-1 val:1 2 3 4 5 6" "PrintNnumbers[-3: 0]PrintNnumbers[-3:-6]PrintNNUMBERS[-1]
>>>
Baidu
[4, 5, 6]
[4, 5, 6]
[4, 5]
[]
[]
6
The step size when slicing:
The so-called step is the length of each increment, as in the C language for (i = 0; i < i++) loops, i++ is the step of the For loop
Nnumbers = [1, 2, 3, 4, 5, 6]#If you do not add a step, the default step is 1, which is:PrintNnumbers[1:5:1]#Modify the step size to 2PrintNnumbers[1:5:2]#all the elements in the output list, the following three ways can bePrintnnumbers[:]PrintnnumbersPrintnnumbers[0:]#Modify the step size to 2PrintNnumbers[::2]PrintNNUMBERS[0::2]
>>>
[2, 3, 4, 5]
[2, 4]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 3, 5]
[1, 3, 5]
nnumbers = [1, 2, 3, 4, 5, 6 # list add nnumbers + = [9, 9, 10 print Span style= "color: #000000;" > nnumbers # list multiplication # multiply the number x by a list to generate a new list, and in the new list, the original list will be repeated x times data = [22] print Datadata *= 10print data # list empty nnumbers = [] print nnumbers
>>>
[1, 2, 3, 4, 5, 6, 9, 9, 10]
[22]
[22, 22, 22, 22, 22, 22, 22, 22, 22, 22]
[]
Membership:
Checks whether a value (element) is in the list, uses the In operator, returns true if it exists, otherwise returns false
data = [1, 2, 3, 10, 22,"Hello","Python",". PY"]Print1inchDataPrint22inchDataPrint "Python" inchDataPrint "python" inchDataPrint "py" inchDatadatabase= [ ["Admin","123456"], ["Adminn","admin888"], ['AAAA',"6666"]]username= Raw_input ("Input UserName:") Password= Raw_input ("Input PassWord:")if[Username, password]inchDatabase:Print "OK"Else: Print "No"
>>> ================================ RESTART ================================
>>>
True
True
True
False
False
Input Username:admin
Input password:123456
Ok
>>> ================================ RESTART ================================
>>>
True
True
True
False
False
Input Username:adminn
Input password:123456
No
Take the minimum, maximum, and number of elements in the list:
" Hello " " Python " " . PY " ] print len (data)print min (data)print max ( Data)print max (10, 22, 11,-7)
>>>
8
1
Python
22
As
Dasd
Basic Python Tutorial 2nd: List and tuple learning notes