Tuples
Lists are pretty flexible, we can change their values whenever we want to. For e.g. consider:
In programming, the fancy word we use to describe that we can change the value after it's set is mutable. Tuples are like lists in that they store a sequence of data but tuples are immutable. You guessed it, once the data is defined then we can't change it.
Remember that whole immutability thing? Let's try it with coordinate1:
You should get the following error
Let's go again...
Wait a minute, tuples are immutable so why are we changing them? Well the + operator is actually creating a new tuple. As we learn about Classes and Objects we'll also learn about references and why this is important and correct.
Before we get more practice with tuples, one more bit of fun!
Exercises
t1 = ('w', 'a', 'l', 'l', 'e', 4, 0, 4) Slice the tuple so that it only contains ('w', 'a', 'l', 'l', 'e').
Write a function
funky_points_sum
which takes tuplest1
andt2
as arguments and adds the 2nd item of t1 to the 1st item of t2, then multiplies that result by the 2nd item of t2 minus the first item of t1.
Last updated