Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lecture 01: Python primer

University of Lausanne

Course: Deep Learning for Solving and Estimating Dynamic Models in Economics and Finance
Script reference: Front matter and Appendix~E — conditionals (if/elif/else)
Notebook role: primer (pre-course self-study; skip if you write Python every day)
Author: Simon Scheidegger

Python Basics 5: Conditionals

Conditional statements give you a way to change the flow of a Python program depending on the result of a boolean expression. Let’s first see what are boolean expressions. Boolean expressions are Python expressions that return either True of False. The simplest ones are:

True
False

But here are some more:

1 > 2
25.0 <= 25.1
5 == 5

Or with variables:

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

Here are a few things with strings:

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

So, what can you put in a boolean expression. Well, any expression that gives you 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 simplet 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.')
    
if not len(x) <= 20:
    print('The string x does not have less than 20 characters.')
    
if len(x) >= 10 and len(x) <= 30:
    print('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

Very important: 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.')

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.')

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 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.')

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.')

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.