Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m

# A Smooth Refresher on Python's Conditional Statements

Difficulty:BeginnerLength:ShortLanguages:
This post is part of a series called A Smooth Refresher To Python.
A Smooth Refresher on Python's Tuples
A Smooth Refresher on Python's Loops

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.

## Branching Programs

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:

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:

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 print statement on the same level under the print statement above (the else code)? In other words, if we wrote it as follows:

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:

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.

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:

Boolean operation Description
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 and, then not.

Let's take another example that shows how we can use boolean operations with lists:

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 casesince 13 does not belong to the list. If you run the program, however, you will notice that the print statement is being run. How did that happen? This happened since we used 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 switch statements!