程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Illustration of Python | tuple

編輯:Python

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


1.Python Tuples

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. .

2. Access tuples

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]

3. Modify tuple

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')

4. Delete tuples

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

5. Tuple operators

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 .

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 Whether elements exist for x in (1, 2, 3): print x,1 2 3 iteration

6. Tuples index , Intercept

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!')
Python expression result describe L[2]'SPAM!' Read the third element L[-2]'Spam' Reverse reading , Read the penultimate element L[1:]('Spam', 'SPAM!') Intercept elements

7. No closing separator

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

8. Tuple built-in functions

Python Tuples contain the following built-in functions

Serial number Method effect 1cmp(tuple1, tuple2) Compare two tuple elements .2len(tuple) Count tuple elements .3max(tuple) Returns the maximum value of an element in a tuple .4min(tuple) Returns the minimum value of an element in a tuple .5tuple(seq) Convert list to tuple .

9. Video tutorial

Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition

https://www.bilibili.com/vide...

Data and code download

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 :

  • Python Quick reference table

Expand references

  • Python course —Python3 file
  • Python course - Liao Xuefeng's official website

ShowMeAI Recommended articles

  • python Introduce
  • python Installation and environment configuration
  • python Basic grammar
  • python Basic data type
  • python Operator
  • python Condition control and if sentence
  • python Loop statement
  • python while loop
  • python for loop
  • python break sentence
  • python continue sentence
  • python pass sentence
  • python String and operation
  • python list
  • python Tuples
  • python Dictionaries
  • python aggregate
  • python function
  • python Iterators and generators
  • python data structure
  • python modular
  • python File read and write
  • python File and directory operations
  • python Error and exception handling
  • python object-oriented programming
  • python Namespace and scope
  • python Time and date

ShowMeAI A series of tutorials are recommended

  • The illustration Python Programming : From introduction to mastery
  • Graphical data analysis : From introduction to mastery
  • The illustration AI Mathematical basis : From introduction to mastery
  • Illustrate big data technology : From introduction to mastery


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved