Definition of tuple : A tuple is Orderly , Immutable , With Comma separated Of , With Sequence surrounded by parentheses , Can pass **() and tuple()** Function definition , Do not modify , It can only be modified at the moment of definition .
A = (1,2,3)# This is a tuple
advantage :
num = (1,2,3,'ad')
print(num)
type(num)
(1, 2, 3, 'ad')
tuple
If we only put one data in the tuple :
num = (2)
type(num)
int
here , The tuple we define , In the end, it became a int Type data . If you redefine a form :
num1 = ('a')
type(num1)
The final output is also a string str type , that , Why is that ?
Python Medium () It also represents a mathematical operation Basic symbols , such as (1+1)*2, So this is ambiguous with the parentheses of tuples , When there is only one element in a tuple , When there is no comma , It will take this element Calculated , This parenthesis will be Python The interpreter recognizes as an operation symbol , So what you get is the data type of the element itself .
therefore , To solve this problem , We can eliminate this by simply adding a comma to the element that defines the tuple .
num = (2,)
type(num)
tuple
Access to elements in tuples :
The access to elements in tuples is still based on the index .
num = (1,2,3,'jk','hy')
print(num[0])
print(num[1])
1
2
Modify tuple :
It was said that , Tuples are immutable , in other words , Elements in tuples cannot be changed after being assigned . however , If the element itself is a list of variable data types , Then its nested items can be changed .
for example :
num = (1,2,[7,8])
print(num)
num[2].append(3)
print(num)
(1, 2, [7, 8])
(1, 2, [7, 8, 3])
If we try to change the value of an element in a tuple :
num = (1,2,[7,8])
print(num)
num[2][1] = 'jk'
print(num)
(1, 2, [7, 8])
(1, 2, [7, 'jk'])
If we embed a list in a tuple , We can only add or delete data in the list .
tuple() function :
tuple The function of function and list The function is basically the same , Take a sequence as a parameter and convert it to a tuple , If the parameter is a tuple , The parameter will be returned as is .
num1 = [2,3,4,'jk']
num1 = tuple(num1)# List tuples
print(num1)
str1 = 'beautiful'
str1 = tuple(str1)# String variable
print(str1)
(2, 3, 4, 'jk')
('b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l')
The index of tuples is exactly the same as the access list and string .
Access mode of nested tuples : In fact, this way is different from c The matrix access method of the language is similar .
num = ((1,'jk'),(2,'hy'),(3,'aj'))
print(num[0][1])
print(num[1][1])
jk
hy
Tuple search
index Returns the index of the first encountered specified element from left to right , without , Report errors
num = (1,2,3,'jk','hy')
num.index(2)
1
count Returns the number of specified elements in a tuple
num = (1,2,3,'jk','hy')
num.count(3)
1
python The difference between tuples and lists
1、 Tuples and lists are ordered , Tuples cannot be modified , The list can be modified .
2、 Elements of tuples and lists can be of any type
3、 The element length of a tuple can be arbitrary .