In the previous article, I introduced Python Operation of string in , Let's learn more about Python Another data type in : Tuples (tuple)
Tuples are generally used () identification , Is an ordered data type . It can store data in various formats .
matters needing attention : For empty tuples () Express , When there is only one element in a tuple , You must add a comma after the element , It means tuple , Otherwise, the type is not a tuple .
Tuples can also be omitted ()
tuplea = () #<class 'tuple'> tuplea = (1) #<class 'int'> tuplea = (1,) #<class 'tuple'> tuplea = (1, 'A', 3.14, [123,345],{'Key':'VALUE'}) #<class 'tuple'> a = 1,2,3,4,{1,2,3} #<class 'tuple'>
In the case of tuples , The operations that can be performed are very similar to strings , Such as slicing and so on . The coordinates are from 0 Start .
Access tuples are similar to access lists print(tup1[0]) print(tup1[2:4])
The value of tuple cannot be modified ( Adding, deleting and modifying elements are not supported ). But when the element is a list , You can modify the contents of the list :
a = (1,2,3,[1,2]) print(a, id(a)) a[3].append(4) print(a,id(a)) Output : (1, 2, 3, [1, 2]) 2622536998080 (1, 2, 3, [1, 2, 4]) 2622536998080
Unpacking in tuples :
Creating a tuple , And put multiple data into tuples , This process is called tuple packing . The opposite of tuple packing is unpacking , Is to take out the elements in the tuple , To assign to Values to different variables .
username, password = ('xiaobo','123456') print(username) print(password)
Concatenation of tuples :
Tuples can use the operator + Splicing :
a = (1,2) b = (3,4) print(a+b) # (1, 2, 3, 4)
Other uses of tuples :
As arguments to a function , Use *args The format of .
such as , In my previous articles ,socket Socket programming , Access to the server ip+ port , It can be passed in the form of tuples .
Python The study of basic knowledge , If you find learning boring , You can take a look at the following book ,《 Reading comics Python》, The author teaches you how to learn... In the form of comics python, It's not so boring to learn .
Previous recommendation
Testing starts from scratch -No.1- Deploy a project environment for learning practical use
Testing starts from scratch -No.2- Know the basics of computer
Testing starts from scratch -No.5- Basic concept and classification of testing
Testing starts from scratch -No.7- Test case design method ( Supplement )
Testing starts from scratch -No.8- How to write test cases at work ?
Testing starts from scratch -No.9- Introduction to software configuration management
This article is from WeChat official account. - Xiaobo's road to growth (libotest) , author : Xiaobo knowledge sharing
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .
Original publication time : 2021-09-20
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .