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

Explain 10 operation methods of Python tuple in detail, and attach sample code

編輯:Python

Python A tuple of is similar to a list , The difference is that the elements of a tuple cannot be modified , You can't add or delete elements , The reason why the above operations cannot be carried out can also be seen from its name ,“ element ” The meaning of has the smallest unit , Immutable .

Tuples use braces ( ), Use square brackets for lists [ ].
How to operate a list , You can refer to my other blog post , Links are as follows :
https://blog.csdn.net/wenhao_ir/article/details/125400072

Catalog

  • 01- Tuple creation
  • 02- Access and slicing of tuple elements
  • 03- Use “+” Operator to connect tuples ( Merge )
  • 04- Use del Statement to delete tuples
  • 05- Using functions len() measurement ( return ) The length of the tuple
  • 06- Use the operator "*" Implement tuple repeat extension
  • 07- Use “in” Determine whether an element is in a tuple
  • 08- Using functions max() Returns the maximum value of an element in a tuple
  • 09- Using functions min() Returns the maximum value of an element in a tuple
  • 10- Using functions list() Converts a tuple to a list

01- Tuple creation

Tuples are easy to create , You just need to add elements in parentheses , And separate them with commas , Even without brackets .
The sample code is as follows :

tup1 = ('Google', 'CSDN', 1997, 1999)
tup2 = (1, 2, 3, 4, 5)
tup3 = 'a', 'b', 'c', 'd' # You don't need brackets 

The operation results are as follows :

Be careful : When a tuple contains only one element , You need to add a comma after the element , Otherwise parentheses will be used as operators .
The sample code is as follows :

tup1 = (50)
tup2 = (50,)

The operation results are as follows :

From the above running results, we can see that ,tup1 Because there is no comma after the element , So it was taken as int type , instead of tuple type .

02- Access and slicing of tuple elements

The sample code is as follows :

tup1 = ('Google', 'CSDN', 1997, 1999)
tup2 = (1, 2, 3, 4, 5)
str1 = tup1[0]
tup3 = tup2[2:5]

The operation results are as follows :

03- Use “+” Operator to connect tuples ( Merge )

The sample code is as follows :

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2

The operation results are as follows :

04- Use del Statement to delete tuples

Element values in tuples are not allowed to be deleted , But we can use del Statement to delete the entire tuple .
The sample code is as follows :

tup1 = ('Google', 'CSDN', 1997, 1999)
tup2 = ('abc', 'xyz')
del tup1

The operation results are as follows :

05- Using functions len() measurement ( return ) The length of the tuple

The sample code is as follows :

len1 = len((1, 2, 3))
tup1 = ('Google', 'CSDN', 'tencent', 1997, 1999, 1998)
len2 = len(tup1)

The operation results are as follows :

06- Use the operator "*" Implement tuple repeat extension

The sample code is as follows :

tup1 = ('Google', 'CSDN', 'tencent', 1997, 1999, 1998)
tup2 = (4, 5, 6)
tup3 = tup1*2
tup4 = tup2*3

The operation results are as follows :

07- Use “in” Determine whether an element is in a tuple

The sample code is as follows :

tup1 = ('Google', 'CSDN', 'tencent', 1997, 1999, 1998)
bool1 = 'CSDN' in tup1
bool2 = 'zhihu' in tup1

The operation results are as follows :

08- Using functions max() Returns the maximum value of an element in a tuple

The sample code is as follows :

tup1 = (456, 700, 200)
max1 = max(tup1)

The operation results are as follows :

09- Using functions min() Returns the maximum value of an element in a tuple

tup1 = (456, 700, 200)
min1 = min(tup1)

The operation results are as follows :

10- Using functions list() Converts a tuple to a list

The sample code is as follows :

tup1 = ('Google', 'Taobao', 'CSDN', 'Baidu')
list1 = list(tup1)

The operation results are as follows :

A detailed introduction to list related operations , You can refer to another blog post of mine , Links are as follows :
https://blog.csdn.net/wenhao_ir/article/details/125400072

Reference material :
https://blog.csdn.net/wenhao_ir/article/details/125100220


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