Dr. Roger Ianjamasimanana

Conditional statements in Python

By Dr. Roger Ianjamasimanana

1. What are conditional statements?

Conditional statements allow us to make decisions in our programs. Just like in real life, we make choices based on conditions:

  • If it rains, I will take an umbrella.
  • If there’s milk in the fridge, I won’t buy more.
  • If I have enough money, I will buy new shoes.

In Python, conditional statements use Boolean logic to decide which block of code to run. These statements include if, if-else, and if-elif-else.

2. Boolean values in Python

Booleans are values that are either True or False. Python evaluates conditions using Booleans:

  • True: means the condition is met.
  • False: Means the condition is not met.

3. Truthy and falsy values in Python

Some values are inherently considered True or False in Python:

  • True: non-zero numbers, non-empty strings, and non-empty collections.
  • False: zero, empty strings, and empty collections.

Note that collections refer to a group of data structures that can hold multiple items together like lists, tuples, sets, dictionaries, sets, and range. Let's illustrate these in the table below.

Collection types Non-empty example Empty example Non-empty truthiness Empty truthiness
List [1, 2, 3] [] True False
Tuple (1, 2, 3) () True False
Set {1, 2, 3} set() True False
Dictionary {"a": 1} {} True False
Range range(1, 5) range(0) True False

Example:

4. The if statement

The if statement checks if a condition is True. If it is, Python runs the indented block of code.

5. The if-else statement

An if-else statement gives an alternative block of code to run when the condition is False.

6. The if-elif-else statement

The if-elif-else statement checks multiple conditions in order. Only one block of code runs, even if more than one condition is true.

7. Combining conditions

In Python, you can combine conditions using and, or, and not to create more complex logic for your conditional statements. Let’s focus on and and or in detail.

7.1 What is and?

The and keyword is used when you want all conditions to be true for the entire condition to be true. If even one of the conditions is false, the result will be false.

Condition 1 Condition 2 Result
True True True
True False False
False True False
False False False

7.2 Example 1: using and

Imagine you’re checking whether someone is eligible to enter a restricted zone. To enter, the person must:

  1. Be at least 18 years old.
  2. Have a valid ID.

7.3 What is or?

The or keyword is used when you want the entire condition to be true if at least one condition is true. The result is false only if all conditions are false.

Condition 1 Condition 2 Result
True True True
True False True
False True True
False False False

7.4 Example 2: using or

Now imagine you’re setting up a system to grant access to a building. A person can enter if they:

  1. Have a physical key, OR
  2. Know the access code.

7.5 Difference between and and or

To summarize:

  • Use and when all conditions must be true.
  • Use or when at least one condition must be true.

7.6 Example: comparing and and or

Here’s an example to help clarify the difference:

Now here is an example using or:

Practice exercise

Imagine you’re writing a program to check if a user qualifies for a discount. The user qualifies if:

  • They are a student, OR
  • They are over 60 years old.

Write a Python program using or to check these conditions and print whether the user gets the discount.

8. Using the ternary operator

The ternary operator is a shorthand for an if-else statement. It’s written in one line:

value = "even" if number % 2 == 0 else "odd"

It evaluates the condition and assigns the result to value.

feature-top
Readers’ comment
feature-top
Log in to add a comment
🔐 Access