The sequence we're talking about in Python includes :
1. String (a sequence consisting of multiple characters)
2. List
3. Tuples
# What are the similarities and differences between lists and tuples? list=[1,2,3]print type (list) tuple= (all in a)print type (tuple) #< the same points > lists and tuples are containers that can contain any type of element (even a sequence) list and elements also contain the order of the Elements # < The difference > list is mutable, and tuples are immutable, so usually, when you have a queue of the same type with indeterminate length, use a tuple with a list of the number of known elements
#< Assignment of strings >string='sequence'#Assignment of < list >alist=[1,2,3,4]#assignment for < tuples >Seq1= ('Hello',' World', 9,'a') SEQ2=('Hi','123')
The operators for all sequences are (high-to-low priority):
1. Member relationship operator (in, not in)
Print ' World ' inch seq1true Print ' Hello ' not inch Seq1false
2. Connection operator (+)
print seq1+seq2 ('hello'World ' a'hi'123')
3. Repeat operator (*)
PrintAlist[1, 2, 3, 4, 1, 2, 3, 4]PrintSeq1('Hello',' World', 9,'a','Hello',' World', 9,'a','Hello',' World', 9,'a')
4. Slice operator ([], [:], [::])
PrintstringsequencePrintSTRING[0]#Subscript starting from 0sPrintString[1:3]#[n:m] denotes n=<index<m rangeeqPrintSTRING[1::2]#[N:m:x] represents the N=<index<m range and the number of steps is xEunePrintSTRING[-1]#-1 indicates the last element, and so onCPrintSTRING[::-1]#so you can use 1 as the step number to achieve reverse outputEcneuqesPrintalist[1, 2, 3, 4]PrintAlist[0]1PrintAlist[2:] [3, 4]PrintAlist[:3][1, 2, 3]PrintAlist[1:3][2, 3]Printseq1 ('Hello',' World', 9,'a')PrintSeq1[1:3](' World', 9)Print(' This',' is','python') [2]python#an index that is out of bounds causes an exceptionPrintAlist[4]indexerror:list Index out of range
[Python Learning]--sequence (1)--a preliminary understanding of the sequence in Python