Python3 tuple
Python tuples are similar to lists. They can also store different types of values. The difference is that the elements of the tuple cannot be modified, and the size of the tuple cannot be changed, which means that modifications and deletions cannot be performed.
Use parentheses for tuples and square brackets for lists.
Tuple creation is very simple, just add elements in parentheses and separate them with commas.
Code example:
tup1=("hello","world",123,12.3,456)
tup2=(10,22,33,45,5,62,15)
tup3=("ls","ll","cd","mv")
tup4=() #This is creating an empty tuple
print(tup1)print(tup2)print(tup3)print(tup4)
operation result:
(‘hello’, ‘world’, 123, 12.3, 456)
(10, 22, 33, 45, 5, 62, 15)
(‘ls’, ‘ll’, ‘cd’, ‘mv’)
()
When the tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be used as operators. Code example:
tup1=(10) #This is a wrong way of declaration, parentheses will be regarded as operators
tup2=(10,) #Correct single-element tuple declaration
print(type(tup1))print(type(tup2))
operation result:
<class 'int'>
<class 'tuple'>
Like lists, tuples also use subscripts to access the values in tuples, code examples:
tup1=("hello","world",123,12.3,456,233)print(tup1[1])print(tup1[1:5])
operation result:
world
(‘world’, 123, 12.3, 456)
The element value in the tuple is not allowed to be modified, but we can connect and combine the tuple. Code example:
tup1 =(10,22,33,45,5,62,15)
tup2 =("ls","ll","cd","mv")
tup3 = tup1 + tup2 #Need to create a new tuple to store the combined data
print(tup3)
operation result:
(10, 22, 33, 45, 5, 62, 15, ‘ls’, ‘ll’, ‘cd’, ‘mv’)
If you modify the elements in the tuple, an error will be reported
**Error example: **
tup1=(10,22,33,45,5,62,15)
tup1[0]=100 #This modification of tuple elements is illegal.
print(tup1)
As a result, the following exception will be thrown:
Traceback (most recent call last):
File “E:/PythonProject/TestTup4.py”, line 2, in
tup1[0]=100
TypeError: ‘tuple’ object does not support item assignment
The element value in the tuple is not allowed to be deleted, but we can use the del statement to delete the entire tuple, code example:
tup1=(10,22,33,45,5,62,15)
del tup1
**Error example: **
tup1=(10,22,33,45,5,62,15)
del tup1[0]
As a result, the following exception will be thrown:
Traceback (most recent call last):
File “E:/PythonProject/TestTup4.py”, line 3, in
del tup1[0]
TypeError: ‘tuple’ object doesn’t support item deletion
Like character strings, you can use + and * to perform operations between tuples. This means that they can be combined and copied, and a new tuple will be generated after the operation
Tuple index, interception
Because a tuple is also a sequence, we can access the element at a specified position in the tuple, and we can also intercept a section of elements in the index, as shown below:
Tuple:
L = (‘Google’, ‘Taobao’, ‘Runoob’)
**Code example: **
tup1 =("hello","world",123,12.3,456,233)print(tup1[2])print(tup1[-2])print(tup1[1:])
operation result:
123
456
(‘world’, 123, 12.3, 456, 233)
Recommended Posts