author : Han Xinzi @ShowMeAI
Tutorial address :http://www.showmeai.tech/tuto...
This paper addresses :http://www.showmeai.tech/article-detail/78
Statement : copyright , For reprint, please contact the platform and the author and indicate the source
Python A tuple of is similar to a list , The difference is that the elements of a tuple cannot be modified .
Tuples use braces , Use square brackets for lists .
Tuples are easy to create , You just need to add elements in parentheses , And separate them with commas .
tup1 = ('ByteDance', 'ShowMeAI', 1997, 2022)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
Create an empty tuple
tup1 = ()
When a tuple contains only one element , You need to add a comma after the element
tup1 = (50,)
Tuples are similar to strings , Subscript index from 0 Start , You can intercept , Combination etc. .
A tuple can use a subscript index to access the values in the tuple .
Here is an example code ( The code can be in On-line python3 Environmental Science Run in ):
tup1 = ['python', 'ShowMeAI', 1997, 2022]
tup2 = [1, 2, 3, 4, 5, 6, 7 ]
print("tup1[0]: ", tup1[0])
print("tup2[1:5]: ", tup2[1:5])
The above code execution result :
tup1[0]: python
tup2[1:5]: [2, 3, 4, 5]
Element values in tuples are not allowed to be modified , But we can join tuples , As shown below ( The code can be in On-line python3 Environmental Science Run in ):
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# The following operation to modify tuple elements is illegal .
# tup1[0] = 100
# Create a new tuple
tup3 = tup1 + tup2
print(tup3)
The output of the above example :
(12, 34.56, 'abc', 'xyz')
Element values in tuples are not allowed to be deleted , But we can use del Statement to delete the entire tuple ,
tup = ('python', 'ShowMeAI', 1997, 2022)
print(tup)
del tup
print(" Delete tup after : ")
print(tup)
After the above instance tuples are deleted , The output variable will have exception information , The output is as follows :
('python', 'ShowMeAI', 1997, 2022)
Delete tup after :
Traceback (most recent call last):
File "<string>", line 9, in <module>
NameError: name 'tup' is not defined
Same as string , You can use... Between tuples + Number and * Number to calculate . That means they can combine and copy , A new tuple is generated after the operation .
Because a tuple is also a sequence , So we can access the element at the specified position in the tuple , You can also intercept an element in the index .
Tuples :
L = ('spam', 'Spam', 'SPAM!')
Any unsigned object , Separated by commas , Default to tuple , The following example :
print('abc', -4.24e93, 18+6.6j, 'xyz')
x, y = 1, 2
print("Value of x , y : ", x,y)
Running results of the above examples :
abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2
Python Tuples contain the following built-in functions
Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition
https://www.bilibili.com/vide...
The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , Babies who can surf the Internet scientifically can also use google colab One click operation and interactive operation learning Oh !
This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :