First, let's do a quick recap of tuple basics before we move on to their typical applications.
Tuples are a bit similar to lists, but they are immutable—once you create a tuple, you can't change it. Tuples frequently store values of multiple types. The codes below present basic tuple operations in Python.
- Define a tuple (name, age, nationality, city):
person = ('Mark', 28, 'English', 'London')
- Access second element:
- Access second and third element:
- Iterate over tuple:
for item in person:
- Check if tuple contains item:
if item in person:
- Unpack tuple:
name, age, nationality, city = person