Tuples are another important sequence structure in Python. Similar to lists, tuples are composed of a series
of elements ordered in a specific order.
Tuples differ from lists in that:
The elements of the list can be changed, including modifying element values, deleting and inserting
elements, so the list is a mutable sequence;
Once a tuple is created, its elements cannot be changed, so tuples are immutable sequences.
Tuples can also be viewed as immutable lists. Generally, tuples are used to hold content that does not
need to be modified.
Formally, all elements of the tuple are enclosed in a pair of parentheses (), and adjacent elements
are separated by commas, as shown below:
(element1, element2, ..., elementn)
Where element1 ~ elementn represents each element in the tuple, the number is not limited, as long as it is
a data type supported by Python.
From the perspective of storage content, tuples can store any type of data such as integers, real numbers,
strings, lists, and tuples. In the same tuple, the types of elements can be different, for example:
In this tuple, there are multiple types of data, including integers, strings, lists, and tuples. In
addition, we all know that the data type of a list is list, so what is the data type of a tuple?
Let's take a look at the type function:
One thing to note is that when there is only one element of type string in the created tuple, the element must
be followed by a comma, otherwise the Python interpreter will treat it as a string. Look at the following code:
# Finally add a comma
a = ("www.freelearningpoints.com",)
print (type (a))
#No commas at the end
b = ("www.freelearningpoints.com")
print (type (b))
As with lists, we can use an index to access an element in a tuple (getting the value of an element),
or we can use a slice to access a group of elements in the tuple (getting a new child) group).
The format for accessing tuple elements using an index is:
Among them, tuplename represents the tuple name, and i represents the index value. The index
of a tuple can be positive or negative. The format for accessing tuple elements using slices is:
tuplename [start: end: step]
Among them, start represents the start index, end represents the end index, and step represents the step size.
The above two methods have been explained in the "Python sequence", so I won't repeat them here,
just for example demonstration, please see the following code:
website= tuple ("www.freelearningpoints.com")
#Access an element in a tuple using an index
print (website ) #Use positive index
print (website [-4]) #Use negative index
#Access a set of elements in a tuple using a slice
print (website [8: 18]) #Use positive slice
print (website [8: 18: 2]) #Specify the step size
print (website [-5: -1]) #Use negative slice
As we mentioned earlier, tuples are immutable sequences, and the elements in a tuple cannot be
modified, so we can only create a new tuple to replace the old tuple. For example, to reassign a
You see, after using + stitching tuples, the contents of tup_1 and tup_2 cannot be changed, which indicates
that a new tuple is generated.
Python - Remove Tuples
When the created tuple is no longer used, you can delete it with the del keyword, for example:
tup = ("Python", "www.freelearningpoints.com")
The output is:
NameError Traceback (most recent call last)
[ipython-input-25-6e7b3c328f18] in [module]
3 del tup
----> 4 print(tup)
NameError: name 'tup' is not defined
Python's built-in garbage collection function automatically destroys unused tuples, so generally you
don't need to manually delete through del.