A data structure in Python is a collection of data elements that are organized in some way, and these data elements can be numbers, characters, or even other data structures.
In Python, the most basic data structures are sequences (lists and tuples), where each element in the sequence has a number (the specific location of the element), which is called an index, and the index subscript starts at 0 and so on ……
-
Python’s tuples are similar to lists, except that the elements of a tuple cannot be modified.
-
Use parentheses
()
for tuples and square brackets[]
for lists.
1. basic tuple operations
1.1 Creating a tuple
Tuple creation is simple, just add elements in parentheses (without parentheses is fine) and separate them with commas.
tup1 = ('baidu', 'google', 12, 34)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
# Create an empty tuple
tup4 = ()
# View the type of tup4 and tup3
type(tup4)
# <class 'tuple'>
type(tup3)
# <class 'tuple'>
Note: When the tuple contains only one element, you need to add a comma after the element, otherwise the brackets will be used as operators, as follows.
TupNum = (34)
type(TupNum) # integer without comma
# <class 'int'>
TupNum = (34,)
type(TupNum) # plus comma to tuple
# <class 'tuple'>
A tuple is similar to a string, with subscript indexes starting at 0. It can be intercepted, combined, etc.
1.2 Accessing tuples
Accessing a tuple is the same as accessing an element in a sequence, and is done through the subscript index
tup1 = ('baidu', 'google',1,2)
tup2 = (1, 2, 3, 4, 5, 6, 7)
tup1[0:2]
# ('baidu', 'google')
tup2[1:4]
# (2, 3, 4)
1.3 Modifying tuples
The values in a tuple cannot be modified once they are defined, but we can modify a tuple by linking it to a tuple, e.g.
tup1 = ('baidu', 'google',1,2)
tup2 = (1, 2, 3, 4, 5, 6, 7)
tup1 + tup2
# ('baidu', 'google', 1, 2, 1, 2, 3, 4, 5, 6, 7)
Note: The following modifications to tuples are illegal because tuples do not support modification by index columns, only copy and join operations can be performed on tuples
**tup1[0] = 100**
(this operation cannot be performed)
1.4 Deleting a tuple
Due to the unmodifiable nature of tuples, the values of the elements in a tuple are not allowed to be deleted, but we can use the del statement to delete the entire tuple, as in the following example:
#! /usr/bin/python3
tup1 = ('baidu', 'google',1,2)
print(tup)
del tup
print("deleted tup tup : ")
print(tup)
# The following error message proves that the entire tuple has been deleted
# The deleted tuple tup1 :
# Traceback (most recent call last):
# File "tuple.py", line 29, in <module>
# print(tup1)
# NameError: name 'tup1' is not defined
2 Tuple Operators
As with strings, tuples can operate on each other using the + and * signs. This means that they can be combined and copied, and a new tuple is created after the operation. In short, some operations on the whole tuple result in a new tuple.
2.1 Tuple length
To find the length of a tuple, use the operator len, as follows
# Find the length of the tup1 tuple
tup1 = ('baidu', 'google',1,2)
len(tup1)
# 4
2.2 Connecting tuples
Two or even pairs of tuples are joined using the + conjunction, e.g.
tup1 = (1,2,3)
tup2 = (4,5,6)
tup3 = (7,8,9)
tup1 + tup2 + tup3
# (1, 2, 3, 4, 5, 6, 7, 8, 9)
2.3 Duplicate tuples
tup1 = ('abc')
# The tuple is copied with a separator and the contents copied after it are separated by the separator
(tup1,) * 3
# ('abc', 'abc', 'abc')
2.4 Determining elements
Determines whether an element in a tuple exists using the in keyword, and the result returns a Boolean value
tup1 = 'abc'
'a' in tup1
# True
2.5 Access to elements at specified positions in a tuple
As with sequences, elements in a tuple can likewise be accessed using an index number to access the element at the specified position, e.g.
content = ('hello','world','!')
content
# ('hello', 'world', '!')
content[1:]
# ('world', '!')
content[:2]
# ('hello', 'world')
content[-1]
# '!'
content[-2]
# 'world'
3 Tuple Built-In Functions
Like lists, tuples also have some built-in functions to determine the size of the elements in the tuple and to transform the tuple accordingly
# Count the number of tuple elements.
len(tuple)
# Return the maximum value of the elements in the tuple.
max(tuple)
# Return the minimum value of the elements in the tuple.
min(tuple)
# Convert a list to a tuple.
tuple(seq)
Summary
This section gives an introduction to the manipulation and use of tuples of Python data structures, and provides support for Python engineers working with lists.
Reference https://github.com/JustDoPython/python-100-day/tree/master/day-009