When we are learning Python, we need to use many tools to help us learn more conveniently. Functions are one of them. Using functions can help us perform calculations, value searches and other operations faster.
The built-in functions provided by Python in the following table can be used directly.
This section mainly introduces the first four functions, and the remaining functions will be interspersed in subsequent sections of this chapter.
The syntax format of the sum function is:
sum(iterable[start:end]
Among them, iterable refers to iterable, here we refer to the sequence, start represents the starting position of the corresponding sequence (including), and end is the ending position (excluding this position). Using this slicing method, we can find the sequence of the specified fragment. And, when only sum (iterable) is used, the sum of the entire sequence is found.
Let's take a look at it through examples:
my_list=[1,2,3,4,5,6]my_tuple=(1,2,3,4,5,6)my_set={1,2,3,4,5,6}print(sum(my_list ))print(sum(my_tuple))print(sum(my_set))
The output is:
212121
Regarding the sum() function, please note that it only sums integers. If there are non-integer parts in the items we require in the sequence, an error will be reported.
For these two functions, you can easily understand from the literal meaning that they find the maximum and minimum values , so we can also use them in sequences. At the same time, we should also note that they are not only for integer segments, they also support Processing of strings.
Let’s see an example:
my_list=[1,2,3,4,5,6]my_str='123456'my_tuple=(1,2,3,4,5,6)my_set={1,2,3,4,5,6} string='abcedfg'#When for For English letters, listing=['a','b','c','d']print(max(my_list),min(my_list))#Find the maximum and minimum values in the list print(max(my_str) ,min(m y_str))#Find the maximum and minimum values in the string print(max(my_tuple),min(my_tuple))#Find the maximum and minimum values in the tupleprint(max(my_set),min(my_set))# Find the maximum and minimum values in the set print(max(string),min(string))#The maximum and minimum values in the letters print(max(listing),min(listing))#The maximum and minimum values of the letters in the list
The output is:
61616161gada
It should be noted here that these two functions can find the maximum and minimum values in the string. For the 26 English letters, the system stipulates that the maximum value is 'z' and the minimum value is 'a'. These two functions are used in the basic algorithm questions The process of solving can provide us with great help.
This function can be understood by length (length), which is used to count the length of the sequence, that is, how many elements are in the list. When it comes to indexing issues, the len() function can provide great help. One of the most commonly used methods when traversing a list is for i in range(len(list)), so that when traversing You can traverse directly from the first item to the last item in the list, paying attention to the index.
Consider the following example:
my_list=[1,2,3,4,5,6]my_str='123456'my_tuple=(1,2,3,4,5,6)my_set={1,2,3,4,5,6} print(len(my_li st))#Find the length in the list print(len(my_str))#Find the length in the string print(len(my_tuple))#Find the length in the tuple print(len(my_set))#Find the length in the set
Output result:
6666