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

Basic operations of tuples in Python

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python The basic operation of tuples .


Two 、 Basic operation

1、 Concept

Tuples (tuple): Like a list , The difference is that elements of tuples cannot be modified , Tuples contain elements in parentheses , The list contains elements in square brackets .

2、 Creating a tuple

Just add the element in parentheses , And use commas to separate elements , Parentheses can also be omitted , Use commas to separate elements directly . (1) If you want to define an empty tuple , Can be represented by parentheses that do not contain content . (2)Python Only 1 Tuples of elements , You must add a comma after the element “,”, for example .

x = ('a', 'b', 1, 2, 3) # Creating a tuple x
print(x)
y = 'a', 'b', 'c', 'd' # Creating a tuple y The parentheses are omitted
print(y)
x = () # Create an empty tuple x
print(x)
x = (1,) # Create a tuple with only one element x
print(x)

give the result as follows .

3、 Access tuples

Like a list , You can use a subscript index to access values in tuples .

x = ('a', 1, 3.14) # Creating a tuple x
print(x[0]) # Output tuple index is 0 The elements of
print(x[1]) # Output tuple index is 1 The elements of
print(x[2]) # Output tuple index is 2 The elements of

give the result as follows .

4、 Merge tuples

Element values in tuples are not allowed to be modified , But we can join tuples .

x = (1, 2, 3) # Creating a tuple x
y = ('a', 'b') # Creating a tuple y
z = x + y # Set tuple x and y Assign to after connection combination z
print(z)

give the result as follows .

5、 Traversal of tuples

Same as list , You can also use for Loop to traverse all elements in a tuple .

x = (1, 2, 3, 4, 5) # Creating a tuple x
for n in x: # loop
print(n, ' ', end="") # Output elements

give the result as follows .


3、 ... and 、 Built in functions

Python The tuple built-in functions provided are len()、max()、min() and tuple(). tuple() The function takes a sequence as an argument , And convert it to tuples , If the parameter itself is a tuple , The parameter is returned as is .

a = tuple([1, 2, 3]) # Convert list to tuple
b = tuple('abc') # Convert a string to a tuple
c = tuple((1, 2, 3)) # Parameters are tuples
print(a)
print(b)
print(c)

give the result as follows .


Four 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


5、 ... and 、 summary

The above is about Python The basic operation of tuples , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


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