In addition to indexing and slicing, sequences also have functions such as addition, multiplication, and functions. These functions can provide certain help when we perform sequence operations. This section introduces addition and multiplication.
The sequence has the function of addition. This addition is similar to the addition of our numbers, but the sequence supports adding two sequences of the same type together, using the ' + ' symbol to operate.
Let’s take a brief look at two examples:
1) Add sequences of the same type, the code is as follows:
my_list=[1,2,3,4,5,6,7,8,9,]my_list2=[11,22,33,44,55,66]my_str='abcdefghijklmn'my_str2='opqrstuvwxyz'my_tuple=( 1,2,3,4,5)m y_tuple2=(6,7,8,9)print('The addition of two lists is:',my_list+my_list2)print('The addition of two strings is:',my_str+my_str2)print('Two After adding tuples: ',my_tuple+my_tuple2)
Running results:
Adding the two lists gives: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66]
The two strings added together are: abcdefghijklmnopqrstuvwxyz
After adding two tuples: (1, 2, 3, 4, 5, 6, 7, 8, 9)
2) Add different types of sequences, the code is as follows:
my_list=[1,2,3,4,5,6,7,8,9,]my_str='abcdefghijklmn'my_tuple=(1,2,3,4,5)print('Add list and string: ',my_list+my_str)print('Adding strings and tuples:',my_str+my_tuple)
Running results:
Traceback(mostrecentcalllast):File,line4,in<module>print('Add list and string:',my_list+my_str)TypeError:canonlyconcatenatelist(notstr)tolist
Running diagram:
The error message is: Only lists (not "str") can be connected to lists, so the same type must be used for addition during the '+' operation.
Python provides a sequence multiplication function. This multiplication is not the same as the algorithm. When a sequence is multiplied by x , the new sequence generated is the original sequence repeated x times.
The multiplication of sequences is relatively simple. Let's take a look at it through a simple example.
my_list=[1,2,3,4,5]my_str='www.dotcpp.com'my_tuple=(1,2,3,4,5)print('Multiply the my_list list by 3 to get a new list: ', my_list*3)print('Multiply the my_str string by 3 to get a new string:',my_str*3)print('Multiply the my_tuple tuple by 3 to get a new tuple:',my_tuple*3)
Output result:
Multiply the my_list list by 3 to get a new list: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Multiply the my_str string by 3 to get a new string: www.dotcpp.comwww.dotcpp.comwww.dotcpp.com
Multiply the my_tuple tuple by 3 to get a new tuple: (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
The running diagram is:
The only thing to note is that what is generated here is a new list, string, and tuple. The original list, string, and tuple are unchanged. You can try this by printing again.
Sequence multiplication also has a function to initialize a list of specified length.
Take a look at the following code:
my_list=[None]*5#Implement an initialization list of length 5 print(my_list)
Output result:
[None,None,None,None,None]
This method is usually used when the list operation exceeds the length of the existing data, because when the index of the access list is greater than the length of the list, access errors will occur.
That’s it for adding and multiplying. The next chapter is the essence of sequences, which is particularly convenient when performing algorithmic operations.