External Exam Download Resources Web Applications Games Recycle Bin

Tuples

We generally use tuples for heterogeneous (different) datatypes and lists for homogeneous (similar) datatypes.

tuples.py

#Tuples are just like lists, but read only (aka immutable):
myList = [43, 45, 49, 52, 55]
myList.append(58)
myTuple = tuple(myList)
print(myTuple)
#myTuple.append(59) #big error


  1. Create a tuple with mixed data types - e.g.:
    mixed = ("word", ["a", "b", "c"], (1, 2, 3), -4, 0.5, True)

  2. you can access the elements nested within another element by:
    print(mixed[2][1])
    access the character list in the previous example to print "a"