Python conditionals

Conditional statements give you a way to change the flow of a Python program depending on the result of a boolean expression.

Bolean expressions

Let’s first see what are boolean expressions. Boolean expressions are Python expressions that return either True of False. The simplest ones are:

True
True
False
False

But here are some more:

1 > 2
False
25.0 <= 25.1
True
5 == 5
True

Or with variables:

x = 'This is a string.'
len(x) > 10
True

Here are a few things with strings:

x == 'This is a string.'
True
x == 'This is a st'
False
'This' < 'This is'
True

So, what can you put in a boolean expression? Well, anything that can be evaluated to either True or False. This is quite general, but very often we use the following building components:

  • >: greater than

  • >=: greater than or equal to

  • ==: equal to

  • < less than

  • <= less than or equal to

  • not gives you the opposite of whatever boolean expression follows

  • or True if any of the boolean expressions to the left or to the right are True

  • and True if both of the boolean expressions to the left and to the right are True

You absolutely have to remember all these!!!

The simplest if-statement

Now, let’s look at the simplest conditional statement. It’s syntax is as follows:

if <boolean_expression>:
    # Expressions that run if the bolean_expression is True

That’s it. Let’s try it:

x = 'Sort string'
if len(x) <= 20:
    print('The string x has less than 20 characters.')
The string x has less than 20 characters.

The following does not print anything for the default x because the boolean expression is False:

if not len(x) <= 20:
    print('The string x does not have less than 20 characters.')

And here is a somewhat more complicated boolean expression:

if len(x) >= 10 and len(x) <= 30:
    print('The string x have between 10 and 30 characters.')
The string x have between 10 and 30 characters.

Questions

  • Change the string x in the code block above so that nothing is printed in the first if and something is printed in the second if.

Be careful of white spaces

Note

In Python, the empty spaces below if are very important when you have multiple expressions.

Here is an example that works:

if len(x) <= 20:
    print('The string x has less than 20 characters.')
    print('And this is an additional line.')
The string x has less than 20 characters.
And this is an additional line.

Here is an example that doesn’t work because the white spaces are not aligned:

if len(x) <= 20:
  print('The string x has less than 20 characters.')
  print('And this is an additional line.')
  File "<tokenize>", line 3
    print('And this is an additional line.')
    ^
IndentationError: unindent does not match any outer indentation level

By the way, I am using tabs below if’s. It doesn’t matter how many white spaces you use as soon as you are consistent. For example, here is the same code with just one white space:

if len(x) <= 20:
 print('The string x has less than 20 characters.')
 print('And this is an additional line.')
The string x has less than 20 characters.
And this is an additional line.

The best practice (and what Jupyter is trying to do by default) is to use a tab or 4 white spaces.

The if-else statement

Some times you want to test for a boolean expression and run something else if it is false. You can do this with the if-else statement:

if <boolean_expression>:
    # Expressions that run if the bolean_expression is True
else:
    # Expressions n to run if the boolean_expression is False

Let’s try it:

x = 'Sort string'
if len(x) <= 20:
    print('The string x has less than 20 characters.')
else:
    print('The string x does not have less than 20 characters.')
The string x has less than 20 characters.

Questions

  • Change x in the code block above so that the expressions after else run.

The if-elif-else statement

Sometimes you want to test for multiple boolean expressions. You can do this using the if-elif-else statement:

if <boolean_expression>:
    # Expressions that run if the bolean_expression is True
elif <other_boolean_expression>:
    # Expressions that run if the other_bolean_expression is True
    # if boolean_expression is False
else:
    # Expressions n to run otherwise

An example:

x = 'Sort string'
if len(x) <= 20:
    print('The string x has less than 20 characters.')
elif len(x) <= 30:
    print('The string x has between than 21 and 30 characters.')
else:
    print('The string x has more than 30 characters.')
The string x has less than 20 characters.

Note that you can have as many elif’s as you want and that else is always optional.

Questions

  • Change the string x in the above code block so that the elif expression runs.

  • Change the string x in the above code block so that the else expression runs.

  • Add one more elif to test if len(x) <= 40. Modify the print() statements accordingly.