26. Interpreting the Python keywords

In the last lesson we saw how we could use the Python interpreter to do some simple mathematical calculations. In this lesson, we will see how the Python interpreter deals with some of the Python keywords we have seen within Reeborg's world.


Breaking lines

Let's start with something simple: enter the expression 3*(2+2) at the interpreter prompt and press "enter". The results should look like the following:

>>> 3*(2+2)
12
>>>

Now, this time, enter only part of the same expression as written here: 3*(, and press "enter". The result should look like the following:

>>> 3*(
...     |

where the vertical line "|" represents a blinking cursor. There are a couple of important things to note:

Let's continue by entering the rest of the expression on the next line and pressing the "enter" key. Here's the result on my computer:

>>> 3*(
...     2+2)
12
>>>

Python had concluded that we had entered the complete expression and calculated it accordingly, giving us the correct result (12) and telling us it was ready for more input by adding the usual prompt afterwards.

Now, try entering 3* followed by the "enter" key. Here's the result I get:

>>> 3*
  File "<input>", line 1
    3*
      ^
SyntaxError: invalid syntax
>>>

The difference here is that we had given no indication to Python that we intended to add more text. In the first case, we had a left opening parenthese "(" without the corresponding right closing parenthese ")". Python inferred from this that we intended to add more text, including at least the ")" ... and it gave us the opportunity to do so. In the second case, Python just assumed that we add no more to add, tried to make sense of what we wrote, 3*, and informed us it couldn't.

Now, try to reproduce the example below:

>>> 3*(
...     2+2
...
...
...     )
12
>>>

As long as Python "thinks" that we have not finished, it will refuse to calculate the expression and continue to offer us the modified prompt. We can continue with an other "silly" example:

>>> 3*(
...     2+2
...     )+(
...     1+1
...     )
14
>>>

which is, admittedly, harder to read and make sense for us than simply having

>>> 3*(2 + 2) + (1 + 1)
14
>>>

Now, feel free to try some more examples on your own before reading any further.


Defining functions

We are now ready to review some of the Python keywords (and concepts!) that we have seen in Reeborg's world. The first keyword is def. Let's define an admittedly silly function that prints the number 2. Try to reproduce the result shown below.

>>> def print2():
...     print 2
...    
>>> print2()
2
>>> print2()
2
>>>

Now, you have to try it on your own before reading any further.

Notice how helpful Python was to already indent the code within our function? Notice also how it interpreted an empty line as an indication we were finished with defining our function. Notice also how it recognizes that "print2", with no spaces between "print" and "2" is different from
"print 2"?

Now, try the following:

>>> def print_many():
...     print 3
...     print 2
...     print 1
...     print 0
...    
>>> print_many()
3
2
1
0
>>>

As long as we keep adding non-empty lines within the function body, Python prompts us to add more. As soon as we add an empty line, Python assumes we are finished. Thus, empty lines can be interpreted as having some meaning by the Python interpreter; this was not the case in Reeborg's world. It will also not be the case in programs written outside of Python's interpreter (in both of these last two cases, empty lines are ignored.) Note that the behaviour of the Python interpreter in taking an empty line (within a function definition) is different that what we had seen earlier when we opened a parenthese without closing it; then, Python "knew" we had more to add, and it was ignoring empty lines.


if you are confused

Do you find that what we have just seen about empty lines is confusing?... Do not despair, you'll quickly get used to it! I'll just keep going with using other Python keywords that we have seen before, starting with if.

>>> if 5 > 4:
...     print 5 > 4
...    
True
>>>

Since 5 is greater than 4, Python executes the statement inside the if block which is to print the result of
"5 > 4", which is "True". This is probably not what we would want in general. Try instead the following:

>>> if 5 > 4:
...     print "5 > 4"
...    
5 > 4
>>>

Surrounding some text by quotes turns it into what is called a string, which is what Python calls regular text (which it does not try to calculate). We can also write:

>>> if 5 > 4:
...     print '5 is greater than 4'
...    
5 is greater than 4

where we have used words instead of a mathematical symbol and, more importantly, single quotes (') instead of double quotes (") to surround the text; Python gives us the choice to use one or the other as we wish. This is to allow us to include either single quotes or double quotes in the text we want to be displayed:

>>> print "It's easy."
It's easy.
>>> print 'He said: "It is easy".'
He said: "It is easy".

If we want to include both types of quotes within a string of text, we must use a backslash (\) as an escape character preceding the quote (or the double quote) that is the same type as the one that begins and ends the string:

>>> print 'She said: "It\'s easy".'
She said: "It's easy".

Let's go back to if and include else also.

>>> if 5 > 4:
...    print "5 is greater than 4"
... else:
...    print "4 is greater than 4"
...    
5 is greater than 4

If you look at the display above, there should be no surprise. However, you should definitely try to reproduce it yourself: you will need to use the backspace key to align the else keyword with the if.

Make sure you try it before going further.


defining more functions.

We can put the previous code snippet into a useful function:

>>> def greater():
...     if 5 > 4:
...         print "5 is greater than 4"
...     else:
...         print "4 is greater than 5"
...    
>>> # now let's use it!
>>> greater()
5 is greater than 4

You must try to reproduce the above result on your own (and learn to get the indentation right) before you proceed further.

Now, wouldn't it be nice if we could use greater() to compare any two numbers automatically? To do that, we need to learn about variables.

previousPython already knows how to add - home - Variables next