Touring Turing

1 Manipulating Variable Values

Learn It

  • Last lesson you used Python to perform some calculations.
  • Let's have a quick test to see how well you remember how to perform calculations.

Badge It - Silver

  1. What is the sum of the following numbers - 5,1,4,9,12
  2. What is the product of the following numbers - 4,1,2,8,9,12
  3. What is the remainder of dividing 12 by 5
  4. What is 12% of 32
  5. If I add 6 to 4, multiply the answer by 10 and then raise to the power of 2, what is the answer.

Try It

  • Using variables can help us solve more complex problems.
  • Let's try and use variables to solve some trickier problems.
  • Let's imagine you're in a maths class and you are given the following problem.

A chicken can lay a single egg, every day. If a chicken lives for seven years, what is the maximum number of eggs it can lay in it's lifetime?

  • We can use Python to help us work this out, using a few simple variables.
daysInYear = 365
lifeTime = 7
eggsPerDay = 1
numberEggs = daysInYear * eggsPerDay * lifeTime
  • We started by assigning three variables to have integer values. Then we assigned a third variable (numberEggs) to be the result of multiplying (*) the previous variables together.

Try It

  • Let's try another one.

A snake can slither 20cm in 15 minutes. How many hours would it take the snake to travel 100cm

distance = 100
speed = 20/15
time = distance/speed

Badge It - Gold

  • These ones are even trickier.
    1. A woodchuck can chuck 5 bits of wood in an hour. How long does it take a woodchuck to chuck 45 pieces of wood.
    2. A peck is equal to 9 litres. If Peter picked a peck of pickled peppers and each pepper has a volume of 0.25 litres, how many pickled peppers did Peter pick.
    3. A cannibal can nibble 9 cans in an hour. How many cans can nine cannibals nibble in ninety minutes

Learn It

  • We can easily assign variables to other variables.
foo = 10
bar = foo
  • Let's find out what bar is now.
  • As you probably expected, it is now 10.
  • But what happens if we change foo
foo = 5
  • What's bar now?

Learn It

  • To see why bar stays at 10, we need to think about what happens when we use code like bar = foo. We'll talk in terms of a sort of Turing Machine.
  • We start off with an empty tape and some empty cards.


  • When we write foo = 10 two things happen.
    1. The value of 10 is stored on the tape in a certain box. In this case it is box number 4.
    2. The card is then altered so it has the variable identifier and the box it points to.


  • When we then write bar = foo, a second card is then used to indicate that bar points to the same box number as foo.


  • Lastly, when we reassign foo the value of 5, a second box (in this case box 8) has 5 stored in it and foo's card is altered to now point to the new box.


  • We can see that bar is still pointing to the value in address 4, which is the number 10.

Understanding check

  • Click here to check your understanding of variables

Badge It - Platinum

  • For each of the problems below - you may only use the variables stated in the problem. You can't use additional variables or values.
  • The first has been done for you as an example


foo = 10
bar = 4
baz = 0
  • make foo, bar and baz all equal to 6.


baz = foo - bar
foo = baz
bar = baz

Problem 1

foo = 10
bar = 4
  • Make foo and bar both equal to 6.

Problem 2

foo = 10
bar = 15
baz = 3
  • Make foo and bar both equal to 25 and baz equal to 22

Problem 3

foo = 12
bar = 3
  • Make foo equal to 4 and bar equal to 12

Problem 4

foo = 9
bar = 4
  • Make foo equal to 4 and bar equal to 9