Warning: the HTML version of this document is generated from
Latex and may contain translation errors. In
particular, some mathematical expressions are not translated correctly.
Conditionals and recursion
4.1 The modulus operator
The modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for other operators:
>>> quotient = 7 / 3
So 7 divided by 3 is 2 with 1 left over.
The modulus operator turns out to be surprisingly useful. For
example, you can check whether one number is divisible by another
4.2 Boolean expressions
A boolean expression is an expression that is either true or false. One way to write a boolean expression is to use the operator ==, which compares two values and produces a boolean value:
>>> 5 == 5
In the first statement, the two operands are equal, so the value of the expression is True; in the second statement, 5 is not equal to 6, so we get False. True and False are special values that are built into Python.
The == operator is one of the comparison operators; the others are:
x != y # x is not equal to y
Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols. A common error is to use a single equal sign (=) instead of a double equal sign (==). Remember that = is an assignment operator and == is a comparison operator. Also, there is no such thing as =< or =>.
4.3 Logical operators
There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10.
n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that is, if the number is divisible by 2 or 3.
Finally, the not operator negates a boolean expression, so not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.
Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as "true."
>>> x = 5
4.4 Conditional execution
In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement:
if x > 0:
The boolean expression after the if statement is called the condition. If it is true, then the indented statement gets executed. If not, nothing happens.
Like other compound statements, the if statement is made up of a header and a block of statements:
The header begins on a new line and ends with a colon (:). The indented statements that follow are called a block. The first unindented statement marks the end of the block. A statement block inside a compound statement is called the body of the statement.
There is no limit on the number of statements that can appear in the body of an if statement, but there has to be at least one. Occasionally, it is useful to have a body with no statements (usually as a place keeper for code you haven't written yet). In that case, you can use the pass statement, which does nothing.
4.5 Alternative execution
A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this:
if x%2 == 0:
If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that effect. If the condition is false, the second set of statements is executed. Since the condition must be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution.
As an aside, if you need to check the parity (evenness or oddness) of numbers often, you might "wrap" this code in a function:
For any value of x, printParity displays an appropriate message. When you call it, you can provide any integer expression as an argument.
4.6 Chained conditionals
Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional:
if x < y:
elif is an abbreviation of "else if." Again, exactly one branch will be executed. There is no limit of the number of elif statements, but the last branch has to be an else statement:
if choice == 'A':
Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
As an exercise, wrap these examples in functions called compare(x, y) and dispatch(choice).
4.7 Nested conditionals
One conditional can also be nested within another. We could have written the trichotomy example as follows:
if x == y:
The outer conditional contains two branches. The first branch contains a simple output statement. The second branch contains another if statement, which has two branches of its own. Those two branches are both output statements, although they could have been conditional statements as well.
Although the indentation of the statements makes the structure apparent, nested conditionals become difficult to read very quickly. In general, it is a good idea to avoid them when you can.
Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional:
if 0 < x:
The print statement is executed only if we make it past both the conditionals, so we can use the and operator:
if 0 < x and x < 10:
These kinds of conditions are common, so Python provides an alternative syntax that is similar to mathematical notation:
if 0 < x < 10:
4.8 The return statement
The return statement allows you to terminate the execution of a function before you reach the end. One reason to use it is if you detect an error condition:
The function printLogarithm has a parameter named x. The first thing it does is check whether x is less than or equal to 0, in which case it displays an error message and then uses return to exit the function. The flow of execution immediately returns to the caller, and the remaining lines of the function are not executed.
We mentioned that it is legal for one function to call another, and you have seen several examples of that. We neglected to mention that it is also legal for a function to call itself. It may not be obvious why that is a good thing, but it turns out to be one of the most magical and interesting things a program can do. For example, look at the following function:
countdown expects the parameter, n, to be a positive
integer. If n is 0, it outputs the word, "Blastoff!"
Otherwise, it outputs n and then calls a function named
What happens if we call this function like this:
The execution of countdown begins with n=3, and since n is not 0, it outputs the value 3, and then calls itself...
The execution of countdown begins with n=2, and since n is not 0, it outputs the value 2, and then calls itself...The execution of countdown begins with n=1, and since n is not 0, it outputs the value 1, and then calls itself...The execution of countdown begins with n=0, and since n is 0, it outputs the word, "Blastoff!" and then returns.
The countdown that got n=3 returns.
And then you're back in __main__ (what a trip). So, the total output looks like this:
As a second example, look again at the functions newLine and threeLines:
Although these work, they would not be much help if we wanted to output 2 newlines, or 106. A better alternative would be this:
This program is similar to countdown; as long as n is greater than 0, it outputs one newline and then calls itself to output n-1 additional newlines. Thus, the total number of newlines is 1 + (n - 1) which, if you do your algebra right, comes out to n.
4.10 Stack diagrams for recursive functions
In Section 3.11, we used a stack diagram to represent the state of a program during a function call. The same kind of diagram can help interpret a recursive function.
Every time a function gets called, Python creates a new function frame, which contains the function's local variables and parameters. For a recursive function, there might be more than one frame on the stack at the same time.
This figure shows a stack diagram for countdown called with n = 3:
As usual, the top of the stack is the frame for __main__. It is empty because we did not create any variables in __main__ or pass any arguments to it.
The four countdown frames have different values for the parameter n. The bottom of the stack, where n=0, is called the base case. It does not make a recursive call, so there are no more frames.
As an exercise, draw a stack diagram for nLines called with n=4.
4.11 Infinite recursion
If a recursion never reaches a base case, it goes on making recursive calls forever, and the program never terminates. This is known as infinite recursion, and it is generally not considered a good idea. Here is a minimal program with an infinite recursion:
In most programming environments, a program with infinite recursion does not really run forever. Python reports an error message when the maximum recursion depth is reached:
File "<stdin>", line 2, in recurse
This traceback is a little bigger than the one we saw in the previous chapter. When the error occurs, there are 100 recurse frames on the stack!
As an exercise, write a function with infinite recursion and run it in the Python interpreter.
4.12 Keyboard input
The programs we have written so far are a bit rude in the sense that they accept no input from the user. They just do the same thing every time.
Python provides built-in functions that get input from the keyboard. The simplest is called raw_input. When this function is called, the program stops and waits for the user to type something. When the user presses Return or the Enter key, the program resumes and raw_input returns what the user typed as a string:
>>> input = raw_input ()
Before calling raw_input, it is a good idea to print a message telling the user what to input. This message is called a prompt. We can supply a prompt as an argument to raw_input:
>>> name = raw_input ("What...is your name? ")
If we expect the response to be an integer, we can use the input function:
prompt = "What...is the airspeed velocity of an unladen swallow?\n"
The sequence \n at the end of the string represents a newline, so the user's input appears below the prompt.
If the user types a string of digits, it is converted to an integer and assigned to speed. Unfortunately, if the user types a character that is not a digit, the program crashes:
>>> speed = input (prompt)
Warning: the HTML version of this document is generated from Latex and may contain translation errors. In particular, some mathematical expressions are not translated correctly.