In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Python tuple definition and how to use it", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What is the definition and use method of Python tuple"!
1. Preface
In Python, a tuple is a data type very similar to a list, except that elements in a list can be modified, while elements in a tuple cannot.
2. Defining and using tuples 2.1 Definitions
Defining elements usually uses parenthesis () literal syntax. Example code is as follows:
tuple1 = ("Hello", "One Bowl Week", "Hello")print(type(tuple1)) #View tuple type print(tuple1) #('Hello ', ' One Bowl Week ', ' Hello ')2.2 Usage
Tuples support the same operators and methods as lists. The example code is as follows:
tuple1 =("Hello", "Bowl Week", "Nuggets", "Developer", "strike")tuple2 =(10, 20, 60, 34)#splice tuple3 = tuple1 + tuple2print(tuple3) #('Hello ', ' Bowl of weeks','Nuggets', 'developers',' strive', 10, 20, 60, 34)print (tuple2 * 2) # (10, 20, 60, 34, 10, 20, 60, 34)#index and slice print(tuple1[1], tuple1[-4]) #bowl week print(tuple3[:2]) #('Hello ', ' bowl week') print(tuple3[::2]) #('Hello ', ' strive', 20, 34)#traversal operation for ch in tuple1: print(ch)'''Hello One Bowl Week Nuggets Developer strike'''#Member Operations print("One Bowl Week" in tuple1) # Trueprint("Nuggets" not in tuple1) # False
An empty () indicates an empty tuple, a tuple with two elements is called a tuple, five are called a quintuple, but if it is ('hello '), it is not a tuple, and the bracket becomes a parenthesis that changes priority; if you want to make it a parenthesis, you have to add a comma, otherwise it is not a tuple.
The sample code is as follows:
a = ()print(type(a)) # b =('a bowl of weeks') print(type(b)) # c = (100)print(type(c)) # d =('a bowl of weeks',)print(type(d)) # e = (100, )print(type(e)) # 3. Tuple Application Scenario 3.1 Packaging and Unpackaging
When multiple comma separated values are assigned to a variable, the values are packed into a tuple type; when a tuple is assigned to multiple variables, the tuple is unpacked into multiple values and assigned to the corresponding variable.
The sample code is as follows:
t = "Hello", "Bowl Week", "Nuggets", "Developer", "strike"print(type(t)) # print(t) #('Hello ', ' Bowl Week ', ' Nuggets ', ' Developer ', ' strive') x, y, z, i, j = tprint(x, y, z, i, j) # Hello Bowl Week Nuggets Developer strike
If the number of elements and the number of variables do not match during unpacking, an error is caused, such as the following code
t= ("Hello", "Bowl Week", "Nuggets", "Developer", "strike")# x, y, z = t # ValueError: too many values to unpack x, y, z, i, j, k, l = t # ValueError: not enough values to unpack ValueError: too many values to unpack ValueError: not enough values to unpack
The solution to this error is to use the * wildcard, with which a variable can receive multiple values, turning a variable into a list. This wildcard can only appear once,
The sample code is as follows:
t =("Hello", "Bowl Week", "Nuggets", "Developer", "strike")x, y, *z = tprint(x, y, z) # Hello Bowl Week ["Nuggets","Developer","strike"]x, *y, z = tprint(x, y, z) # Hello ["Bowl Week","Nuggets","Developer"] strike *x, y, z = tprint(x, y, z) # ["Hello",'Bowl of weeks',' Nuggets '] Developer strive3.2 Swap the values of two variables
In Python, swapping the values of two variables a and b requires only code like this
a, b = b, a
Similarly, if you want to interchange the values of three variables a, b, and c, that is, b is assigned to a, c is assigned to b, and a is assigned to c, you can do the same.
a, b, c = b, c, a
3.3 Let the function return multiple values
If there are two values in the return statement, the two values are assembled into a binary and returned. So calling the defined function will get this binary, or you can assign two values in the binary to two variables separately by unpacking syntax.
At this point, I believe that everyone has a deeper understanding of "Python tuple definition and use method", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.