Conditionals are one of the cornerstones of any programming language.
They allow you to control the program flow according to specific conditions you can check.
The if
statement
The way you implement a conditional is through the if
statement.
The general form of an if
statement is:
if expression:
statement
The expression
contains some logic that returns a boolean, and the statement
is executed only if the return is True
.
If you want to know more about booleans, check out my detailed post Booleans in Python.
A simple example:
bob_age = 32
sarah_age = 29
if bob_age > sarah_age:
print('Bob is older than Sarah')
#output:
#Bob is older than Sarah
I have two variables indicating the ages of Bob and Sarah, the condition in plain English says "if Bob’s age is greater than Sarah’s age, the print the phrase ‘Bob is older than Sarah’".
Since the condition returns True
, the phrase will be printed on the console.
The if else
and elif
statements
In my last example, the program only does something if the condition return True
.
But I also want it to do something if it returns False
or even check a second or third condition if the first one wasn’t met.
In this example, I swapped Bob’s and Sarah’s age, the first condition will return False
since Sarah is older now, and then the program will print the phrase after the else
instead.
bob_age = 29
sarah_age = 32
if bob_age > sarah_age:
print('Bob is older than Sarah')
else:
print('Bob is younger than Sarah')
#output:
#Bob is younger than Sarah
Now, consider the example below with the elif
.
bob_age = 32
sarah_age = 32
if bob_age > sarah_age:
print('Bob is older than Sarah')
elif bob_age == sarah_age:
print('Bob and Sarah have the same age')
else:
print('Bob is younger than Sarah')
#output:
#Bob and Sarah have the same age
The purpose of the elif
is providing a new condition to be checked before the else
is executed.
Once again I changed their ages and now both are 32 years old.
As such, the condition in the elif
is met since both have the same age the program will print "Bob and Sarah have the same age".
Notice you can have as many elif
as you want, just put them in sequence.
bob_age = 32
sarah_age = 32
if bob_age > sarah_age:
print('Bob is older than Sarah')
elif bob_age < sarah_age:
print('Bob is younger than Sarah')
elif bob_age == sarah_age:
print('Bob and Sarah have the same age')
else:
print('This one is never executed')
#output:
#Bob and Sarah have the same age
In this example, the else
is never executed because all the possibilities are covered in the previous conditions and thus could be removed.
Nested conditionals
You might need to check more than one conditional for something to happen.
In this case, you can nest your if
statements.
For instance, the seconde phrase "Bob is the oldest" is printed only if both if
pass.
bob_age = 32
sarah_age = 28
mary_age = 25
if bob_age > sarah_age:
print("Bob is older than Sarah")
if bob_age > mary_age:
print("Bob is the oldest")
#output:
#"Bob is the oldest"
Or, depending on the logic, make it simpler with Boolean Algebra.
This way, your code is smaller, more readable and better to maintain.
bob_age = 32
sarah_age = 28
mary_age = 25
if bob_age > sarah_age and bob_age > mary_age:
print("Bob is the oldest")
#output:
#"Bob is the oldest"
It is also possible to make it even simpler with chained operators as I’ve explained in Using chained comparison operators in Python.
bob_age = 32
sarah_age = 28
mary_age = 25
if bob_age > sarah_age > mary_age:
print("Bob is the oldest")
#output:
#"Bob is the oldest"
And last but not least, you can make use of ternary operators to make your whole logic in one line.
bob_age = 32
sarah_age = 28
mary_age = 25
result = "Bob is the oldest" if bob_age > sarah_age > mary_age else "Bob is not that old"
print(result)
#output:
#"Bob is the oldest"
Check this explanation on Python Ternary Operator if you want to know more.
As you could see, there are many ways to make conditions, choose the one that better fits your needs, there is no "one size fits all" in programming.