34. Chocolate mousse cake recipe

I don't know about you, but I'm getting hungry. It would be so nice to have some delicious chocolate mousse cake to eat. We need:

To actually make the cake, we need the recipe. Something like:

A recipe is very much like a computer program: it's a series of instructions that one needs to follow in a precise order to accomplish a desired result. I think we have enough to learn about programming, that I will end the cooking lesson at this point and go back to Python.


Lists

Here's a little something for you to try with the Python interpreter:

>>> ingredients = ["4 eggs", "100 g of sugar", 
...     "250 g of dark semi-sweet chocolate", "30 ml of cocoa powder"]
>>> 

ingredients is an example of a Python list. A list in Python can include any number of items. It the example we have chosen, all the ingredients are strings; however, list items could be numbers, or even other lists and other Python objects that we have yet to see. Now, try the following:

>>> for each_item in ingredients:
...    print each_item
4 eggs
100 g of sugar
250 g of dark semi-sweet chocolate
30 ml of cocoa powder

>>> ingredients[0]
'4 eggs'

>>> ingredients[0:2]
['4 eggs', '100 g of sugar']

It should look familiar, yet slightly different from what we have seen before. Try also the following:

>>> for character in ingredients[0]:
...     print character
...    
4
 
e
g
g
s

>>> ingredients[0][2]
'e'

>>> '4 eggs'[2]
'e'

As we had just seen previously, ingredients[0] is a synonym for the string '4 eggs'; so ingredients[0][2] is the third character (remember Python starts counting at zero) in the string '4 eggs'.

Now, in the recipe above, I forgot one nice ingredient: whipped cream to be used as a topping. I can add it, and verify that it's been added as follows:

>>> ingredients.append("whipped cream")
>>> print ingredients
['4 eggs', '100 g of sugar', '250 g of dark semi-sweet chocolate', '30 ml of cocoa powder', 'whipped cream']

append() is a method that is part of the class "list". ingredients is an instance of that class. We invoke the method append on the instance ingredients by connecting it with a ".", as we have seen in the lesson about object-oriented programming. The argument of append is the object we want to add to the list. It is always appended at the end of the list.

Lists have many other methods; I will introduce them when needed. If you want to see them all, just type dir([]) at the Python interpreter; note that [] is an empty list - any list would give the same result here. See if you can spot the method name append.

Now that we know how to add items to lists, we need to know how to remove them. To do this, we need to use the Python keyword del.

>>> del ingredients[3]
>>> print ingredients
['4 eggs', '100 g of sugar', '250 g of dark semi-sweet chocolate', 'whipped cream']

The Python keyword del can do much more than removing items in a list. For example, it can make Python forget about a variable that had been defined.

>>> a = 3
>>> print a
3
>>> del a
>>> print a
Traceback (most recent call last):
  File "<input>", line 1, in ?
NameError: name 'a' is not defined

Try it!

In Reeborg's world, write a simple program that creates two robots, each with its own name, have them take a few steps and try to make one of them disappear using the keyword del. You may need to have the remaining robot take at least one more step (before turning it off) to have the other robot disappear from the screen.


A word about range()

You are now ready to learn a secret about the Python function range():

>>> print range(6)
[0, 1, 2, 3, 4, 5]

That's right: range() simply creates a list!


Converting between lists, integers and strings

Here are a few useful built-in functions to help converting between one data type and another.

>>>  first_number = 1304
>>>  second_number = 987
>>>  
>>>  # converting an integer to a string using str()
>>>  first_string = str(first_number)
>>>  first_string
'1304'
>>>  second_string = str(second_number)
>>>  second_string
'987'

>>>  # converting a string to a number using int()
>>>  int(first_string)
1304

>>>  # converting a string to a list using list()
>>>  first_list = list(first_string)
>>>  first_list
['1', '3', '0', '4']

>>>  second_list = list(second_string)
>>>  second_list
['9', '8', '7']

>>>  # using join() to create a string from a list of strings
>>>  '...'.join(second_string)
'9...8...7'
>>>  '-'.join(first_string)
'1-3-0-4'
>>>  # use the empty string '' to join the elements...
>>>  ''.join(first_string)
>>>  '1304'
>>>  ''.join(second_string)
>>>  '987'

>>>  # extracting the last element of a list with pop()
>>>  # Note: pop() is, in a way, the opposite of append() 
>>>  first_list
['1', '3', '0', '4']
>>>  last_element = first_list.pop()
>>>  last_element
'4'
>>>  first_list
['1', '3', '0']

>>>  # the function len() gives the number 
>>>  # of items in a list 
>>>  len(first_list)
3

Help Reeborg to add using lists, strings and integers

Help Reeborg to add two numbers, like illustrated below, using what we just saw about lists, strings and integers.

before addition

after addition

previousRepeat()is hiding some Python keywords - home - An other kind of definition next