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, which has the genaral form:
if BOOLEAN EXPRESSION: STATEMENTS
A few important things to note about
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statements:
The colon [
if True: # This is always true pass # so this is always executed, but it does nothing else: pass
8] is significant and required. It separates the header of the compound statement from the body.The line after the colon must be indented. It is standard in Python to use four spaces for indenting.
All lines indented the same amount after the colon will be executed whenever the BOOLEAN_EXPRESSION is true.
Here is an example:
food = 'spam' if food == 'spam': print['Ummmm, my favorite!'] print['I feel like saying it 100 times...'] print[100 * [food + '! ']]
The boolean expression after the
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statement is called the condition. If it is true, then all the indented statements get executed. What happens if the condition is false, and
if x y: STATEMENTS_B else: STATEMENTS_C0 is not equal to
if x y: STATEMENTS_B else: STATEMENTS_C1? In a simple
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statement like this, nothing happens, and the program continues on to the next statement.
Run this example code and see what happens. Then change the value of
if x y: STATEMENTS_B else: STATEMENTS_C0 to something other than
if x y: STATEMENTS_B else: STATEMENTS_C1 and run it again, confirming that you don’t get any output.
Flowchart of an if statement
As with the
if x y: STATEMENTS_B else: STATEMENTS_C5 statement from the last chapter, the
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statement is a compound statement. Compound statements consist of a header line and a body. The header line of the
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statement begins with the keyword
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 followed by a boolean expression and ends with a colon [
if True: # This is always true pass # so this is always executed, but it does nothing else: pass8].
The indented statements that follow are called a block. The first unindented statement marks the end of the block. Each statement inside the block must have the same indentation.
Indentation and the PEP 8 Python Style Guide
The Python community has developed a Style Guide for Python Code, usually referred to simply as “PEP 8”. The Python Enhancement Proposals, or PEPs, are part of the process the Python community uses to discuss and adopt changes to the language.
PEP 8 recommends the use of 4 spaces per indentation level. We will follow this [and the other PEP 8 recommendations] in this book.
To help us learn to write well styled Python code, there is a program called pep8 that works as an automatic style guide checker for Python source code.
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]0 is installable as a package on Debian based GNU/Linux systems like Ubuntu.
In the section of the appendix, , there is instruction on configuring vim to run
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]0 on your source code with the push of a button.
4.1.2. The if choice == 'a':
print["You chose 'a'."]
elif choice == 'b':
print["You chose 'b'."]
elif choice == 'c':
print["You chose 'c'."]
else:
print["Invalid choice."]
2 statement
It is frequently the case that you want one thing to happen when a condition it true, and something else to happen when it is false. For that we have the
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]2 statement.
if food == 'spam': print['Ummmm, my favorite!'] else: print["No, I won't have it. I want spam!"]
Here, the first print statement will execute if
if x y: STATEMENTS_B else: STATEMENTS_C0 is equal to
if x y: STATEMENTS_B else: STATEMENTS_C1, and the print statement indented under the
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]6 clause will get executed when it is not.
Flowchart of a if else statement
The syntax for an
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]2 statement looks like this:
if BOOLEAN EXPRESSION: STATEMENTS_1 # executed if condition evaluates to True else: STATEMENTS_2 # executed if condition evaluates to False
Each statement inside the
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 block of an
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]2 statement is executed in order if the boolean expression evaluates to
if x y: STATEMENTS_B else: STATEMENTS_C0. The entire block of statements is skipped if the boolean expression evaluates to
if x y: STATEMENTS_B else: STATEMENTS_C1, and instead all the statements under the
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]6 clause are executed.
There is no limit on the number of statements that can appear under the two clauses of an
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]2 statement, but there has to be at least one statement in each block. Occasionally, it is useful to have a section with no statements [usually as a place keeper, or scaffolding, for code you haven’t written yet]. In that case, you can use the
if x y: STATEMENTS_B else: STATEMENTS_C4 statement, which does nothing except act as a placeholder.
if True: # This is always true pass # so this is always executed, but it does nothing else: pass
Python terminology
Python documentation sometimes uses the term suite of statements to mean what we have called a block here. They mean the same thing, and since most other languages and computer scientists use the word block, we’ll stick with that.
Notice too that
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]6 is not a statement. The
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statement has two clauses, one of which is the [optional]
if choice == 'a': print["You chose 'a'."] elif choice == 'b': print["You chose 'b'."] elif choice == 'c': print["You chose 'c'."] else: print["Invalid choice."]6 clause. The Python documentation calls both forms, together with the next form we are about to meet, the
if True: # This is always true pass # so this is always executed, but it does nothing else: pass6 statement.
4.2. 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: STATEMENTS_B else: STATEMENTS_C
Flowchart of this chained conditional
if x y: STATEMENTS_B else: STATEMENTS_C9 is an abbreviation of
if 0