Beginner Python tutorial – 02 – Conditionals statements

Video version:

Sometimes we want to run a code depending on if one or more conditions apply (the user’s age is more than 18 or the temperature is lower than a 100 degrees). That’s when we use conditionals.

If

If statement in Python

Let’s start with a simple example. We want to print a text message if we are in the future (That’s it, in a year bigger than current one, 2019):

year = 2020

if year > 2019:
  print('Welcome to the future!')

It’s easy to understand as Python is a high level language, meaning that it’s close to human language. The 3rd line checks “If 2020 is bigger than 2019, run the next code”. We have to keep the code indented 4 spaces or a tab to the right to mark it as a code that only will run if the condition applies.

Else

If else statement in Python

But sometimes we want to run one code or the other. For example, if it is raining, grab a big jacket. If not, a sweater.

year = 2015

if year > 2019:
  print('Welcome to the future!')
else:
  print('You are in the past!')

Now checks if 2015 is bigger than 2019. As it is not true, it will run the code inside the ‘else’ statement. It has no conditions. The code will run the first or the second code, not both.

Elif

If elif else statement in Python

The past code is pretty limited. What if we want to check multiple things? Maybe I want to check if the temperature is below X degrees to grab a jacket, if it is above Y degrees to wear a shirt and if it is between X and Y to wear a thin sweatshirt. We can create a secondary (and a third, and a fourth, and…) conditional statement with “elif“:

year = 2019

if year > 2019:
  print('Welcome to the future!')
elif year == 2019:
  print('Cool, we are in the same space-time')
else:
  print('You are in the past!')

It is the current year bigger than 2019? No.

Let’s check the second condition: It is year equal to 2019? Yes. Then we will run the code. We can write more conditions that are true: It doesn’t matter, the code always will run the code inside the first condition that it’s true.

And

A condition statement can check multiple statements, not just one:

if 5 > 4 and 5 < 10:
  print('Yeah!')

This code will check both operations and both have to be true to run the code printing “Yeah”.

It is 5 bigger than 4? Yes.

It is 5 smaller than 10? Yes.

The program then will print ‘Yeah!’.

Or

But we can also check if one of two (or more) conditions apply. As long as one condition is true, the program will stop checking the other conditions and will run the code below it:

if 5 == 6 or 5 < 10:
  print('Yeah!')

As 5 is not the same as 6, but 5 is lower than 10, the program will print ‘Yeah!’.

Challenge: Bouncer

Ok, I have a job for you: You are going to program our bouncer.

Our bouncer is sick and is not coming today, so we have to program a robot to ban the enter to teenagers (and even more younger people!).

Don’t worry, it is pretty easy, just ask the user how old they are (Remember the ‘input‘ function?) and check their age. If it’s younger than 18, don’t let them enter. Anything else, let them enter (with a greet!)

You can re-read this entry or the past one if you need help. This is an easy one and you shouldn’t have a problem. So stop reading and try to solve the problem.

Did you managed? Here’s how I have solved it:

user_age = int(input('How old are you? '))

if user_age < 18:
  print('Sorry mate you are too young')
elif user_age > 100: 
  print('Are you sure you should be here? At this time?')
else:
  print('Welcome to our pub')

Now you can try and create your own little programs. Ask the user for a name and a password and if they enter the correct one, display a message. Or maybe a little game you can play with your friends.

Experiment, but above all, have fun.

I will see you on my next lesson.

Repl.it code: https://repl.it/@DavidMM1707/02-Conditionals

Video version: https://www.youtube.com/watch?v=uoXoAFxrZWk

Follow me on Twitter: https://twitter.com/DavidMM1707

My GitHub: https://github.com/david1707

Learn more about:
– Conditional statements: https://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements

Third lesson: 03 – Lists