Tuples - Lakelands Computing

Title
Go to content
Tuples
Tuples are similar to lists in that they allow you to store multiple pieces of information at once. Like Lists, Tuples use indexes  to indentify the position of the value. The big difference between Tuples and Lists are that tuples are unchangeable. You can not change the values in a Tuple, you can not add another value to a Tuple.

Tuples use () :

  • cakeIngredients = ("eggs","flour","yoghurt","sugar","strawberries","butter")

The above is a Tuple. The ingredients for this Strawberry cake will not change. It won't be the same cake if it did, so it makes sense to use a tuple (which can't change) rather than a list (which can)
index
012345
value"eggs""flour""yoghurt""sugar""strawberries""butter"
You can access the values of a Tuple in a similar way to you would if it was a List:
  • cakeIngredients[1]
  • print(cakeIngredients[0])
  • print (cakeIngredients[3])

The first one is "flour", the second one prints out "eggs" and the third one prints "sugar"

You can also print sections of the list using the colon : either to separate the start and end of the section, or to say from first value to this index or from this index to the end (third and fourth examples)
  • print (cakeIngredients[0:3])
  • print (cakeIngredients[2:5])
  • print (cakeIngredients[:4])
  • print (cakeIngredients[1:])

You can also use loops with a Tuple in the same way as you can with a List. There's an example in the Trinket
All Text copyright Lakelands Academy & Mr T Purslow 2020.
All images copyright free / creative commons unless otherwise stated.
You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
Back to content