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

Use of tuples in Python learning

編輯:Python

Catalog

Tuple creation

Deletion of tuples

The output of some elements of tuples

Nesting of tuples

Calculation of the number of tuple elements :len()

Solving the maximum and minimum values of elements in tuples :max(),min()

The number of occurrences of an element in a tuple :count function

The index of an element in a tuple :index(), Returned is a subscript value

Modification of tuple element value : Element values of tuples cannot be modified .

Tuple creation tuple=() # Create an empty tuple print(tuple)print(type(tuple))

Output :

()
<class 'tuple'>

Define tuples that contain only one element : The element must be followed by a comma , Otherwise the compiler will recognize it as an integer .

give an example :

tuple2 = (1,)print(tuple2)print(type(tuple2))tuple2 = (1)print(tuple2)print(type(tuple2))

Output :

(1,)
<class 'tuple'>
1
<class 'int'>

Deletion of tuples

del Tuple name to be deleted

tuple2 = (1,2,3,4,5,6)print(tuple2)del tuple2print(tuple2)

Because we are in the third line of code for tuple2 Deleted , So the compiler will report an error ,tuple2 Not defined .

Determine whether an element is in a tuple :in/ not in sentence

give an example :

tuple2 = (1,2,3,4,5,6)if 4 in tuple2: print("YES")else: print("NO")if 10 in tuple2: print("YES")else: print("NO")

Output :

YES
NO

The output of some elements of tuples

Similar to slicing a list .

give an example :

tuple2 = (1,2,3,4,5,6)print(tuple2[0:3])

Output :

(1, 2, 3)

Nesting of tuples

Similar to the nesting of lists , Just replace the list with tuples

give an example :

tuple1=((1,2,3),(4,5,6),(7,8,9))for tuple1s in tuple1: print(tuple1s) for tuple1_s in tuple1s: print(tuple1_s)

Output :

(1, 2, 3)
1
2
3
(4, 5, 6)
4
5
6
(7, 8, 9)
7
8
9

Calculation of the number of tuple elements :len()

give an example :

tuple1=(1,2,3,4,5,6,7,8,9)print(len(tuple1))

Output :

9

Solving the maximum and minimum values of elements in tuples :max(),min()

give an example :

tuple1=(1,2,3,4,5,6,7,8,9)print(max(tuple1))print(min(tuple1))

Output :

9
1

The number of occurrences of an element in a tuple :count function

give an example :

tuple1=(1,2,3,4,5,6,7,8,9,3,3,2,2)print(tuple1.count(3))

Output :

3

The index of an element in a tuple :index(), Returned is a subscript value

give an example :

tuple1=(1,2,3,4,5,6,7,8,9,3,3,2,2)print(tuple1.index(2))

Output :

1

Modification of tuple element value : Element values of tuples cannot be modified .

give an example :

tuple1=(1,2,3,4,5,6,7,8,9,3,3,2,2)tuple1[3]=100print(tuple1)

Output :

This is about Python This is the end of the article on the use of tuples of learning , More about Python Tuple content please search the previous articles of the software development network or continue to browse the following related articles. I hope you will support the software development network in the future !



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