Skip to main content

Tuples

Tuples are very similar to lists.  The major difference, tuples are immutable.  While lists have many functions to change the data contained within them, tuples prevent this action and will raise an exception if it is attempted.

 

# Tuple example

someVariable = (1, 2, 3)
print(someVariable) # result: (1, 2, 3)

# Once created they are essentially read only.
# with the nesting feature, tuples can contain other datatypes which are mutable however

someVariable = (1, 2, 3, [1, 2, 3]) # the list in the last element of this tuple is mutable

# Tuples are indexed similar to lists
print(someVariable[0]) # result: 1

for x in someVariable:
  print(x)
# Result:
#    1
#    2
#    3
#    [1, 2, 3]