This article is participating in Python Theme Month. See the link to the event for more details
preface
Last time we talked about lists, and then a lot of people in the background said that lists were a little bit more difficult to learn, a little bit more content, but what I want to say is that this is not a very difficult class, as long as you pay attention to the difference between strings, you’ll get it pretty quickly. I also remind you that it is really difficult to stick to the process of learning, but you will surely have a harvest day by day, believe me, follow me to learn, the future you will be grateful for now so hard themselves.
What is a tuple
Lists are similar to tuples in that they hold elements in order, and each element has its own index, so both lists and tuples can access elements through the index.
Tuples characteristics
Tuples are immutable. Once a tuple is constructed, the program cannot modify the members contained in the tuple (just as a string is immutable, and the program cannot modify the sequence of characters contained in the string).
Creating a tuple
The syntax for creating a primitive is as follows: (ele1, ELE2, ele3,……….) Ex. :
my_tuple = ('virus'.20.'python')
printMy_tuple (my_tuple)'virus'.20.'python')
Copy the code
Tuple element
Get a single, for example:
my_tuple = ('virus'.20.'python')
print(my_tuple[0]) Output result: virusCopy the code
Get multiple, for example:
my_tuple = ('virus'.20.'python'.'php'.'java'.'c#')
print(my_tuple[0:4]) Output: ('virus'.20.'python'.'php')
Copy the code
Get the interval element, for example:
my_tuple = (1.2.3.4.5.6.7.8.9)
print(my_tuple[2:8:2]) Output: (3.5.7)
Copy the code
A combination of two or more tuples
Ex. :
a_tuple = ('virus'.20, -1.2)
b_tuple = (127.'python'.'fkit'.3.5)
sum_tuple = a_tuple + b_tuple
print(sum_tuple)'virus'.20, -1.2.127.'python'.'fkit'.3.5)
Copy the code
Determine whether it is inside a tuple
The in operator is used to determine whether a list or tuple contains an element. For example:
a_tuple = ('virus'.20.50)
print(20 ina_tupleTrue
Copy the code
Length, maximum and minimum values
Length len(), for example:
a_tuple = ('virus'.20.50)
print(len(a_tuple)3
Copy the code
Max (), for example:
a_tuple = (80.20.50)
print(max(a_tuple)80
Copy the code
Min (), for example:
a_tuple = (80.20.50)
print(min(a_tuple)20
Copy the code
Remember that some elements are numbers and some are strings. You can’t compare them. For example:
a_tuple = ('virus'.'python'.'study')
print(min(a_tuple)) Output: PythonCopy the code
a_tuple = ('virus'.'python'.'study')
print(max(a_tuple)Copy the code
a_tuple = ('virus'.20.30)
print(maxA_tuple = (a_tuple)'virus'.20.30)
print(maxTypeError: (a_tuple)'>' not supported between instances of 'int' and 'str'
Copy the code
After-class supervision
Recently a part of the students in the background of private chat I said, I am ready to learn every day, but a go to the computer desk is not up to work, how to do? For students in this situation, I decide to spend some time every day to supervise and tutor your study. If you need any help, please scan the pictures below and click “Contact author” to sign up.
conclusion
Lists and tuples are similar in that they hold elements in order, and each element has its own index, so both lists and tuples have access to elements through the index. The difference between them is that tuples are not modifiable, but lists are. Students should keep this feature in mind. Stay tuned for the next lesson on the use of dictionaries!