(1) Getting to know tuple for the first time
Tuples in Python are similar to lists, except that the elements of the tuple cannot be modified. Use parentheses for tuples and square brackets for lists. The list is a mutable sequence, and the elements in the list can be modified arbitrarily. Tuples are immutable sequences, and the elements in the tuple cannot be modified.
Therefore, tuples do not have methods for adding elements, modifying elements, or deleting elements. We only need to learn the creation and deletion of tuples, and the access and counting of elements. Tuples support the following operations:
(2) Creation of tuples
Create tuples via () .
Parentheses can be omitted. a = (10,20,30) or a = 10,20,30
a = (10)
print(type(a))#<class 'int'>
a = (10,) #或者 a = 1,
print(type(a))#<class 'tuple'>
Create tuples (iterable objects) through tuple()
a = tuple() #创建一个空元组对象
b = tuple("abc")
c = tuple(range(3))
d = tuple([2,3,4])
-
tuple() can receive lists, strings, other sequence types, iterators, etc. to generate tuples.
-
list() can receive tuples, strings, other sequence types, iterators, etc. to generate lists.
(3) Element access and counting of tuples
The elements of the tuple cannot be modified
Element access, index() , count() , slicing and other operations of tuples are the same as lists.
a = (20,10,30,9,8)
print(a[1])#10
print(a[1:3])#(10, 30)
print(a[:4])#(20, 10, 30, 9)
a = (20,10,30,9,8)
b = sorted(a) #b是新对象,内容是:[8, 9,10, 20, 30]
a = [10,20,30]
b = [40,50,60]
c = [70,80,90,100]
d = zip(a,b,c)
print(d) #zip object
e = list(d) #列表:[(10, 40, 70), (20, 50,80), (30, 60, 90)]
print(e)
#列表推导式: [0, 2, 4, 6, 8]
#a = [x*2 for x in range(5)]
#print(a)
s = (x*2 for x in range(5))
print(s) #<generator object <genexpr> at0x0000021C80BE2880>
b = tuple(s)
print(b) #(0, 2, 4, 6, 8)
c = tuple(s)
print(c) #()
s2 = (x for x in range(3))
print(s2.__next__()) #0
print(s2.__next__()) #1
print(s2.__next__()) #2
#print(s2.__next__()) #报错:
StopIteration
(4) Modify the tuple
The element values in the tuple are not allowed to be modified, but we can connect and combine the tuples. The specific code is as follows:
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# 以下修改元组元素操作是非法的。
# tup1[0] = 100
# 创建一个新的元组
tup3 = tup1 + tup2
print tup3
(5) No closing delimiter
Arbitrary unsigned objects, separated by commas, are tuples by default, as in the following example:
(6) Tuple operator
Like strings, tuples can be operated using + and * symbols. This means they can be combined and copied, resulting in a new tuple after the operation.
Python expression | result | describe |
---|---|---|
len((1, 2, 3)) | 3 | Count the number of elements |
(1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | connect |
('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | copy |
3 in (1, 2, 3) | True | element exists |
for x in (1, 2, 3): print x, | 1 2 3 | iteration |
(7) Tuple built-in functions
The Python tuple contains the following built-in functions
method | and description |
---|---|
cmp(tuple1,tuple2) | Compare two tuple elements. |
len(tuple) | Count the number of tuple elements. |
max(tuple) | Returns the maximum value of elements in the tuple. |
min(tuple) | Returns the minimum value of the elements in the tuple. |
tuple() | tuple(seq) converts a list to a tuple. |