Python introductory tutorial | Python3 tuple (tuple)

create tuple

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.

Tuple creation is very simple, just add elements in parentheses and separate them with commas.
insert image description here

>>> tup1 = ('Google', 'Tarzan', 1997, 2000)
>>> tup2 = (1, 2, 3, 4, 5 )
>>> tup3 = "a", "b", "c", "d"   #  不需要括号也可以
>>> type(tup3)
<class 'tuple'>

create empty tuple

#示例
tup1 = ()

When the tuple contains only one element, you need to add a comma, after the element, otherwise the parentheses will be used as operators:

>>> tup1 = (50)
>>> type(tup1)     # 不加逗号,类型为整型
<class 'int'>

>>> tup1 = (50,)
>>> type(tup1)     # 加上逗号,类型为元组
<class 'tuple'>

Tuples are similar to strings, the subscript index starts from 0, and can be intercepted, combined, etc.
insert image description here

access tuple

Tuples can use subscript indexes to access the values ​​​​in the tuple, as in the following example:

tup1 = ('Google', 'Tarzan', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
 
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])

The output of the above example:

tup1[0]: Google
tup2[1:5]: (2, 3, 4, 5)

modify tuple

The element values ​​in the tuple are not allowed to be modified, but we can connect and combine the tuples, as shown in the following example:

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
 
# 以下修改元组元素操作是非法的。
# tup1[0] = 100
 
# 创建一个新的元组
tup3 = tup1 + tup2
print (tup3)

The output of the above example:

(12, 34.56, ‘abc’, ‘xyz’)

delete tuple

The element value in the tuple is not allowed to be deleted , but we can use the del statement to delete the entire tuple , as shown in the following example:

cmd
 
print (tup)
del tup
print ("删除后的元组 tup : "+tup)

After the above instance tuple is deleted, the output variable will have abnormal information, and the output is as follows:

删除后的元组 tup :
>>> print ("删除后的元组 tup : "+tup)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tup' is not defined

tuple operator

Like strings, +, += and * symbols can be used between tuples for operations. 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
a = (1, 2, 3)
b = (4, 5, 6)
c = a+b
c
(1, 2, 3, 4, 5, 6) Concatenation, c is a new tuple that contains all the elements in a and b.
a = (1, 2, 3)
b = (4, 5, 6)
a += b
a
(1, 2, 3, 4, 5, 6) Concatenated, a becomes a new tuple containing all the elements in a and b.
(‘Hi!’,) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') copy
3 in (1, 2, 3) True element exists
for x in (1, 2, 3):
    print (x, end=" ")
1 2 3 iteration

tuple index, intercept

Because a tuple is also a sequence, we can access the element at a specified position in the tuple, or intercept a section of elements in the index, as follows:
tuple :

tup = ('Google', 'Runoob', 'Taobao', 'Wiki', 'Weibo','Weixin')

insert image description here

Python expression result describe
tup[1] 'Runob' read the second element
tup[-2] ‘Weibo’ Read in reverse, read the second-to-last element
tup[1:] ('Runoob', 'Taobao', 'Wiki', 'Weibo', 'Weixin') Intercept elements, all elements after the second start.
tup[1:4] ('Runoob', 'Taobao', 'Wiki') Intercept elements, starting from the second to the fourth element (index 3).

The running instance is as follows:

>>> tup = ('Google', 'Tarzan', 'Taobao', 'Wiki', 'Weibo','Weixin')
>>> tup[1]
'Tarzan'
>>> tup[-2]
'Weibo'
>>> tup[1:]
('Tarzan', 'Taobao', 'Wiki', 'Weibo', 'Weixin')
>>> tup[1:4]
('Tarzan', 'Taobao', 'Wiki')
>>>

tuple built-in functions

The Python tuple contains the following built-in functions

method describe example
len(tuple) Count the number of tuple elements. >>> tuple1 = (‘Google’, ‘Runoob’, ‘Taobao’)
>>> len(tuple1)
3
>>>
max(tuple) Returns the maximum value of elements in the tuple. >>> tuple2 = (‘5’, ‘4’, ‘8’)
>>> max(tuple2)
‘8’
>>>
min(tuple) Returns the minimum value of the elements in the tuple. >>> tuple2 = (‘5’, ‘4’, ‘8’)
>>> min(tuple2)
‘4’
>>>
tuple(iterable) Convert an iterable series to tuples. >>> list1= [‘Google’, ‘Taobao’, ‘Tarzan’, ‘Baidu’]
>>> tuple1=tuple(list1)
>>> tuple1
(‘Google’, ‘Taobao’, ‘Tarzan’, ‘Baidu’)

Regarding tuples being immutable

The so-called immutable tuple means that the content in the memory pointed to by the tuple is immutable.

>>> tup = ('t', 'a', 'r', 'z', 'a', 'n')
>>> tup[0] = 'g'     # 不支持修改元素
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> id(tup)     # 查看内存地址
4440687904
>>> tup = (1,2,3)
>>> id(tup)
4441088800    # 内存地址不一样了

Guess you like

Origin blog.csdn.net/weixin_40986713/article/details/132654611