-
Book Overview & Buying
-
Table Of Contents
Python Illustrated
By :
A tuple is similar to a list. There is one important difference: after you create a tuple, it cannot be modified. We call that: immutability. A tuple is immutable and cannot be changed after creation. It’s like your date of birth; you can’t change it, no matter how badly you might want to be younger!
Let’s see how we can create tuples!
Again, let’s start by creating an empty tuple. It’s like creating a list, but we replace the square brackets with parentheses, like this:
my_first_tuple = ()
This is an empty tuple. It’s immutable, so we can’t place items in there. It’s likely that we’d want to create a tuple with items. Again, this is similar to creating a list with items! We can create a tuple by placing items inside parentheses (), separated by commas.
# A tuple of coordinates
position = (10, 20)
Just like lists, tuples can have items with...