Welcome to stationmaster Online Station master school study Python Knowledge, this paper studies in Python Medium access tuple Element details. The main contents of this knowledge point include: use print () function output content, access tuple elements by index, access tuple elements by slicing, and output instances of daily language.
catalogue
1. Use the print() function to output the content.
2. Access tuple elements by index.
3. Access tuple elements by slicing.
4. Example: let's output the daily language.
Webmaster Online Warm tips: This course is related to< Detailed explanation of accessing list elements in Python >Generally consistent, you can compare and learn.
1. Use print () function output content.
In Python, if it is relatively simple to output the contents of tuples, you can use the print() function.
For example, we can output the mixed hunhe tuples in the content of the last knowledge point by using the function print(hunhe)
shuzi = (1,2,3,4,5,6,7) #number shige = ("abed, I see a silver light","It's suspected to be frost on the ground","look at the bright moon","Bow your head and think of your hometown") #Poetry hunhe = (66,"Python",('Life is short','I use Python'),["WEB development","cloud computing","Reptile"]) #Mixed tuple python = ('grace',"to make clear",'''simple''') print(hunhe) print(python)
The results are as follows:
(66, 'Python', ('Life is short', 'I use Python'), ['WEB development', 'cloud computing', 'Reptile']) ('grace', 'to make clear', 'simple') >>>
After comparison, single quotation marks, double quotation marks and three quotation marks are not input, but single quotation marks are output.
Through the above output, we find that when outputting tuples, the parentheses () on both sides are included.
2. Access tuple elements by index.
Tuple is another important sequence structure in Python, and it is also an ordered sequence structure aggregate Therefore, to access any element in a tuple, you only need to tell Python the location (index) of the element. To access a tuple element, first indicate the name of the tuple, and then indicate the position of the element in the tuple.
Next, let's output the third and second elements in the mixed tuple, code As follows:
hunhe = (66,"Python",('Life is short','I use Python'),["WEB development","cloud computing","Reptile"]) print(hunhe[2]) print(hunhe[1])
The output results are:
('Life is short', 'I use Python') Python >>>
Yesterday's content:
python1 = ('grace',) python2 = ('grace') print("python1 The type of is:",type(python1)) print("python2 The type of is:",type(python2)) print(python1) print(python2)
The output result is:
python1 The type of is: <class 'tuple'> python2 The type of is: <class 'str'> ('grace',) grace >>>
As can be seen from the above results, the output of a single tuple also includes parentheses (). If it is character string , excluding the left and right quotation marks.
stay Access list elements We use the index method, but we don't talk about the slice access method list Element. Here, we talk about using slicing to access tuples.
3. Access tuple elements by slicing.
Accessing tuple elements is a method that can use slicing to access tuple elements.
We access the first two elements of the mixed tuple, code Is:
hunhe = (66,"Python",('Life is short','I use Python'),["WEB development","cloud computing","Reptile"]) #Mixed tuple print(hunhe[:2])
The operation result is:
(66, 'Python') >>>
We access the last two elements of the mixed tuple. The code is:
hunhe = (66,"Python",('Life is short','I use Python'),["WEB development","cloud computing","Reptile"]) #Mixed tuple print(hunhe[2:])
The operation result is:
(('Life is short', 'I use Python'), ['WEB development', 'cloud computing', 'Reptile']) >>>
When we access the second to third elements of the mixed tuple, we will report an error. The code is:
hunhe = (66,"Python",('Life is short','I use Python'),["WEB development","cloud computing","Reptile"]) #Mixed tuple print(hunhe[2,3])
The operation result is:
Traceback (most recent call last): File "D:\Python\Python310\Doc\000.py", line 2, in <module> print(hunhe[2,3]) TypeError: tuple indices must be integers or slices, not tuple >>>
4. Example: let's output the daily language.
import datetime #Import date time class mot = ("Trust is the most scarce currency.", #Define a list "Pursue fast, but die miserably; Sometimes, slow is fast!", "Adaptation is a technology. If you learn adaptation technology, you will change words into gold. You won't be short of money in your life.", "Flow is natural, customers are natural, and methods are natural.", "Every time you spend is precious. You should focus on the most productive things.", "Everything is not for me, everything is for my use.", "The diamond is in your backyard, the secret collection is in front of you, but you turn a blind eye to it every day.") day=datetime.datetime.now().weekday() #Get current week print(mot[day]) #Output daily language
Note: in the above code, datetime.datetime.now() is used to obtain the current date, and weekday() is used to obtain the week from the date time object. Its value is one of 0 ~ 6, 0-bit Monday, and 6 is Sunday.
The operation results on November 9, 2021 are as follows:
Pursue fast, but die miserably; Sometimes, slow is fast! >>>
So far, the content of this article< In the detailed explanation of accessing tuple elements in Python, the four points of using print() function to output content, accessing tuple elements by index, accessing tuple elements by slicing, and outputting examples of daily language are explained. You can leave me a message if you don't understand!