capypad
0 day streak
python / beginner
Snippet

Python Tuples

Tuples are ordered, immutable collections of items. Once created, their contents cannot be changed, making them useful for fixed data structures.

snippet.py
python
1
2
3
coordinates = (10, 20)
print(coordinates[0])
# coordinates[0] = 15 # This would raise a TypeError
Breakdown
1
coordinates = (10, 20)
Define a tuple using parentheses instead of square brackets.
2
print(coordinates[0])
Access the first element using index 0, just like a list.