python learning, tuple

python tuple

What is a tuple

A tuple is a collection of data types, represented by parentheses ()

1
2
3
4
5
6
7
8
# Empty tuple
student = ()

# A tuple containing one element, note that there must be a comma at the end,
school = ('Shenzhen University',)

# Tuple with 4 elements
teacher = ('age', 1, 1998, 2019)

Access tuple

1
2
3
4
5
6
7
teacher = ('age', 1, 1998, 2019)

# Access the first element of the tuple 'age'
teacher[0]

# Access tuple element 4 2019
teacher[3]

Modify tuple

Tuples are immutable, and tuple elements cannot be modified

Delete tuple

Tuples are immutable, so tuple elements cannot be modified, but the entire tuple can be deleted

1
2
3
teacher = ('age', 1, 1998, 2019)

del teacher

Tuple built-in system functions

1
2
3
4
teacher = ('age', 1, 1998, 2019)

# Calculate the number of teacher tuple elements
len(teacher)
Persist in original technology sharing, your support will encourage me to continue to create!