stay python There are a few dedicated to dealing with numbers python function , Using these values can facilitate our processing of complex data
>>>dights=[1,2,3,4,5,6,7,8,9,0]
>>>min(dights)
0
>>>max(dights)
9
>>>sum(dights)
45
min Represents the minimum output is 0
max The output representing the maximum value is 9
sum The representation and output are 45
Let's learn about slicing .
We were talking about del When deleting, I said about slicing , To create slices , You need to specify the index of the first element and the last element , Of course and function range(), The same index you want to output must be a digit after the number .
for example
classnumbers=['zhang','wang','su','meng','fan','chu']
print(classnumbers[0:3])
['zhang','wang','su']
from 0 The index starts from the first start to the third end . When you type [0:4] Will output from ‘zhang’ To ‘meng’
When python If there is no first index, it will automatically start from the beginning of the list .
for example
classnumbers=['zhang','wang','su','meng','fan','chu']
print(classnumbers[:5])
['zhang','wang','su','meng','fan']
You can use a similar method when you want the slice to terminate at the end of the list , Add that you want to extract the third element from the list , To the last element .
classnumbers=['zhang','wang','su','meng','fan','chu']
print(classnumbers[2:])
['su','meng','fan','chu']
Output the list directly to the end .
Or just want the last three in the list , At this time, we need to input
classnumbers=['zhang','wang','su','meng','fan','chu']
print(classnumbers[-3:])
['meng','fan','chu']
In some cases, we need to use slicing in combination with others for It can also be used in the loop .
classnumbers=['zhang','wang','su','meng','fan','chu']
for classnumber in classnumbers[:3]:
print(classnumber.title())
Zhang
Wang
Su