Life is about taking the right decisions, and our programs are not exempt from that. Well, in the end we are programming something for some aspects of our lives. We thus should expect the issue of making decisions in our programs.
Here is where conditional statements come into play. They help us make such decisions by the logic control of our programs. The conditional statements in Python are: if, elif, and else.
But, what does the conditional statement do? It simply checks whether a statement (test) is true or false, based on which decision is carried out.
Unlike straight-line programs where the statements are executed in the order in which they appear, branching programs allow us to navigate to statements regardless of the order, but based on the decision. The conditional statements we mentioned above are considered to be of this type of programs, provided that if a conditional statement has been executed, program execution continues at the code following the conditional statement.
Conditional Statement Structure
In this section I will describe the different parts a conditional statement is composed of. A conditional statement basically consists of the following main parts:
- a test that evaluates to either true or false
- a block of code that runs if the test is true
- an optional block of code if the test is false
The conditional statement in Python thus looks as follows:
if test: code else: code
where test is a boolean expression, that is, an expression which evaluates to either true or false. In other words, conditional statements allow us to check the truthfulness of some statement. Let's see a simple example of the above structure:
x = 0 if x == 3: print 'x is equal to 3' else: print 'x is NOT equal to 3' print 'That\'s it!'
What is the output of this code snippet? Go ahead, give it a try.
At this point, I think I should mention quickly about indentation (spaces), which Python uses as opposed to braces in other languages like C. In the above script, what would happen if you wrote the last
else code)? In other words, if we wrote it as follows:
else: print 'x is not equal to 5' print 'That\'s it!'
In this case,
print 'That\'s it!' will be part of the
else code block.
This is just a quick example of indentation, which Python relies on extensively in its syntax. When you program in Python, you should expect errors like this to pop up occasionally when there is an issue with your indentation:
IndentationError: expected an indented block
Nested Conditional Statements
The conditional statement is said to be nested if the true block of code or false block of code (i.e. else) contains another conditional statement. Let's see an example of that:
if course1 == 'Computer Science I': if course2 == 'Computer Science II': print 'student can take the Data Structures course' else: print 'student lacks the necessary requirements to take the Data Structures course'
We here have a nested conditional statement since the first
if statement contains another
if statement inside it. Notice again how the code is indented. This is very critical when programming in Python.
Compound Boolean Expressions
As you remember, we have mentioned that the test in the conditional statement structure is a boolean expression. Sometimes you may need more than one boolean expression in the same test, and this is what we call compound boolean expressions.
Let's take an example that finds the smallest number of three numbers. Notice that the first test is a compound boolean expression.
if a<b and a<c: print 'x is the smallest number' elif b<c: print 'b is the smallest number' else: print 'c is the smallest number'
In this example we use the third conditional statement for the first time in this tutorial, that is
elif, which refers to else if.
We have also used a boolean operation called
and, which means that all the statements have to be true in order for the following statement to run. Python's boolean operations can be summarized in the following table:
|or||the following statement runs if any expression is true|
|and||all the expressions need to be true for the following statement to run|
|not||the expression is false if it evaluates to true, and vice versa|
If you had a statement (test) with a mix of those operations, the order of priority is as follows:
or runs first, then
Let's take another example that shows how we can use boolean operations with lists:
list = [1,2,3,4,5] x = 13 if x not in list: print 'x is not in the list'
See how we used
not in this example? As you know, for the code block in the
if statement to run, the statement (boolean expression) should evaluate to true. The statement originally evaluates to false in our case, since 13 does not belong to the list. If you run the program, however, you will notice that the
not operator, which inverts the original evaluation value. In our case, it inverted false to true.
Python and Switch?
I know you may have been wondering till now about when a
switch example will come along. Maybe if you learned the conditional statements in another programming language, you were confronted with examples showing the beauty of using
switch statements. The reason I haven't mentioned examples of such statements is because Python does not have
For more information about Python's conditions, you can refer to the documentation.