Tuples are immutable and they are very similar to lists. When creating tuples you have to use parentheses.
motorbikes = (“Honda”, “Yamaha”, “Suzuki”)
Tuples’ index can be accessed the same way you would access lists’ index.
You can’t reassign a value in a tuple. It causes a TypeError.
motorbikes = “Piaggio”
The terminal output will be:
ddn_ro@linux:~/Desktop$ python file.py Yamaha Traceback (most recent call last): File "file.py", line 3, in
motorbikes = "Piaggio" TypeError: 'tuple' object does not support item assignment ddn_ro@linux:~/Desktop$
- Like lists and dictionaries, tuples can be nested within each other.
- Tuples are faster than lists, but they cannot be changed.
Tuples can be created without the parentheses, by just separating the values with commas.
motorbikes = "Honda", "Yamaha", "Suzuki" print(motorbikes)
If you want to create an empty tuple then you use an empty parenthesis pair.
empty_tpl = ()