Sequence sequence
A sequence sequence is a set of sequential elements
(strictly speaking, It is a collection of objects, but given the concept of not introducing objects, temporarily say elements)
A sequence can contain one or more elements , or it can have no elements
The basic data types we mentioned earlier can be used as elements of a sequence. The element can also be another sequence, as well as other objects we later Have.
There are two types of sequences: tuple ( fixed value table : Also called tuple ), and list ( list )
S1 = (2,1.3, ' Love ', 5.6,9,false) #是一个tuple
s2 = [true,5, ' Smile '] #是一个list
Print (s1,type (s1))
([True, 5, ' Smile '], <type ' tuple ' >)
Print (s2,type (s2))
([True, 5, ' Smile '], <type ' list ' >)
The main difference between a tuple and a list is that once established,each element of a tuple cannot be changed, and the elements of the list can be changed Again.
A sequence as an element of another sequence
S3 = [1,[3,4,5]]
Empty sequence
S4 = []
References to elements
The subscript of the sequence element starts with 0 :
Print (s1[01])
1.3
Print (s2[2])
Smile
Print (s1[1][2])
V
Because the list element is notconsistent, you can assign a value to the list
s2[1] = 3.0
Print (s2)
Above output
3.0
If you do this to a tuple, you get an error message.
so, you can see that the reference to the sequence is implemented by s[int] , and int is Subscript.
Other citation methods (slices)
Range reference: basic style [ lower limit: Upper limit: step length ]
Print (s1[:5]) #从开始到下标4 (the Elements of subscript 5 are not included)
(2, 1.3, ' Love ', 5.6, 9)
Print (s1[2:]) #从下标2到最后
(' Love ', 5.6, 9, False)
print (s1[0:5:2]) #从下标0到下标4 (not included in subscript 5), each 2 element ()
(2, ' Love ', 9)
Print (s1[2:0:-1]) #从下标2到下标1
(' Love ', 1.3)
As can be seen in the scope reference, if the upper limit is indicated, then the upper limit itself is not included .
Trailing element reference
Print (s1[-1]) #序列last element
Print (s1[-3]) #序列倒数第三个元素
string is a tuple
Strings are special tuples, so you can perform related operations on Tuples.
str = ' abcdef '
Print (str (2:4))
' CD '
Summarize
Tuple element immutable, list element variable
A reference to a sequence s[2],s[1:8:2]
A string is a tuple
Python base 03 Sequence