tuple
Another ordered list is called tuple: tuple. Tuples are very similar to lists, but tuples cannot be modified once initialized. So what does it mean that it cannot be modified?
Tuple immutability means that when you create a tuple, it cannot be changed. That is to say, it does not have methods such as append() and insert(), but it also has the ability to obtain a certain index value. Method, but cannot be assigned a value. So why do we have tuples? That's because tuples are immutable, so the code is safer. Therefore, it is recommended to use tuple as much as possible instead of list.
1. Create a tuple
Tuple creation is very simple, just add elements in brackets and separate them with commas.
tuple1=('两点水','twowter','liangdianshui',123,456) tuple2='两点水','twowter','liangdianshui',123,456
Create an empty tuple
tuple3=()
When the tuple contains only one element, you need to add a comma after the element
tuple4=(123,)
If you do not add a comma, the creation will not be tuple (tuple), but refers to the number 123. This is because brackets () can represent both tuple and parentheses in mathematical formulas, which creates ambiguity. Therefore, Python stipulates that in this case , calculated according to parentheses, the calculation result is naturally 123. Specifically, look at the output values of tuple4 and tuple5 in the figure below
2, the index of tuple (tuple)
Tuple subscript The index starts from 0 and can be intercepted, combined, etc.
3. Access tuple (tuple)
tuple (tuple) can use subscript index to access the value in the tuple
#-*-coding:utf-8-*- tuple1=('两点水','twowter','liangdianshui',123,456) tuple2='两点水','twowter','liangdianshui',123,456 print(tuple1[1]) print(tuple2[0])
Output result:
4. Modify tuple (tuple)
Didn’t I spend a long paragraph saying that tuple is Immutable? Why are tuples modified here again? That's because the element values in the tuple are not allowed to be modified, but we can concatenate and combine tuples, and affect the value of tuple by modifying the values of other lists.
Look at the following example specifically:
#-*-coding:utf-8-*- list1=[123,456] tuple1=('两点水','twowater','liangdianshui',list1) print(tuple1) list1[0]=789 list1[1]=100 print(tuple1)
Output results:
('两点水', 'twowater', 'liangdianshui', [123, 456]) ('两点水', 'twowater', 'liangdianshui', [789, 100])
You can see that the tuple values output twice have changed. Let's see how tuple1 is stored.
Modify the tuple flow chart
You can see that tuple1 has four elements, the last element is a List, and there are two elements in the List list. When we put the two elements in the List list When elements 124 and 456 are modified to 789 and 100, judging from the output value of tuple1, it seems that they have indeed changed, but in fact, what has changed is not the elements of the tuple, but the elements of the list. The list pointed to by the tuple at the beginning has not been changed to another list. Therefore, the so-called "unchanged" of the tuple means that each element of the tuple will never change. Note that the fourth element in tupe1 still points to the original list and remains unchanged.
5. Delete tuple (tuple)
tuple The element value in the tuple is not allowed to be deleted, but we can use the del statement to delete the entire tuple
#-*-coding:utf-8-*- tuple1=('两点水','twowter','liangdianshui',[123,456]) print(tuple1) del tuple1
6, tuple (tuple) operator
Like strings, tuples can be operated using the sign and * sign. This means that they can be combined and copied, resulting in a new tuple.
7. Tuple built-in function