A tuple is an immutable sequence, and its contents cannot be changed. Tuples are sequences, which are similar to lists. The main difference is that after a tuple is created, the elements inside cannot be added or deleted.
The general form of a tuple is:
(1,2,3,4,5,6)
The difference between it and the list structure is that it uses parentheses '()', and each element is separated by ','. The tuple can also store integers, strings, lists and other types of content.
Creation can take many forms.
Creating an empty tuple is the same as a list, and the syntax is:
my_tuple=()
Let’s look at an example:
my_tuple=()#Create an empty tuple print(my_tuple)#Output print('data type', type(my_tuple))#Look at its type
Output result:
()data type<class'tuple'>
The method of assigning values to create tuples is also relatively simple. It is very convenient for everyone to learn the following after learning about lists. You must master the relevant content in the list.
Syntax format for direct assignment:
my_tuple=(value1,value2,value3,value4,value5)
my_tuple is the name of the list, and each element in the value1~value5 bytes can be an integer, a string or a list. See the following example:
my_tuple=('www.dotcpp.com','123456',[1,2,3,4,5,6])#Create a tuple and assign it directly print(my_tuple)#Output
Output result:
('www.dotcpp.com','123456',[1,2,3,4,5])
Using the range() function we can create tuples with a certain data size. Look at the following example:
my_tuple=tuple(range(1,100,3))#range(1,100,3) creates a sequence with a step size of 3 between 1-100, and finally uses tuple() to cast it to the tuple type print(my_tuple)#output
The output is:
(1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73 ,76,79,82,85,88,91,94,97)
The method of deleting tuples is the same as that of lists. Use the del statement to delete directly. The syntax is:
delmy_tuple
For example:
>>>my_tuple=tuple(range(1,100,3))#range(1,100,3) creates a sequence with a step size of 3 between 1-100, and finally uses tuple() to cast it to a tuple type >>> delmy_tuple#Delete Yuanzu>>>print(my_tuple)#If you output it at this time, an error will be reported Traceback(mostrecentcalllast):File<stdin>,line1,in<module>NameError:name'my_tuple'isnotdefined
When we access tuple elements, we also find the element we want to access based on the index position.
We first create a tuple my_tuple = ('a','b','c','d')
Then take a look at the code:
my_tuple=('a','b','c','d')print(my_tuple[0],my_tuple[1],my_tuple[2],my_tuple[3])
Output result:
abcd
Take a look at the corresponding table:
Because a tuple is an immutable array, we cannot modify its elements. If we modify it directly through the index value in list form, an error message will appear.
>>>my_tuple=(1,2,3,4,5,6)>>>my_tuple[2]=6Traceback(mostrecentcalllast):File<stdin>,line1,in<module>TypeError:'tuple'objectdoesnotsupportitemassignment
So if we want to modify the elements in the tuple, we must use other methods. Two methods are usually used, one is reassignment method and the other is type conversion method.
>>>mytuple=(11,22,33,44,55,66,77)>>>mytuple=(1,2,3)>>>mytuple(1,2,3)
This overwriting method can easily modify the elements in the tuple. Of course, the scope of use of this method is limited, so we can use the second method in most cases.
If you want to modify the 100th element of the tuple, it will be very troublesome to use the above method. Therefore, you can use type conversion. First convert the tuple into other data types, such as converting to a list. Generally, we convert Operate as a list.
Look at the following example:
my_tuple=(1,2,3,4,5,6)#First create a tuple my_tuple=list(my_tuple)#Convert the tuple into list form print(my_tuple,type(my_tuple))#Look at the output now Data and types my_tuple.insert(3,'Insert new element')#Insert, change or delete elements under the list type my_tuple[3]=['Change of elements']my_tuple.pop()#Deletion of elements, pop() automatically deletes the last item, which is the corresponding 6my_tuple=tuple(my_tuple)#Finally converted to tuple form print(my_tuple,type(my_tuple))#print
Output result:
[1,2,3,4,5,6]<class'list'>(1,2,3,['Change of elements'],4,5)<class'tuple'>
Tuples lack some functions compared to lists, but tuples also have their own unique features. First of all, the performance of tuples is relatively high, and the processing rate of tuples is faster than that of lists. Secondly, it is very safe. When the data size is large, When it is large, using tuples can ensure that your data will not be modified, ensuring security, and is a good choice in terms of storage.