Skip to content

5.2 Conditional operators

5.2a What are Python conditional operators?

Conditional operators in Python are used to compare values. These comparisons return a Boolean value (True or False). Here are the primary conditional operators:

==equal to
!=not equal to
>greater than
<less than
>=greater than or equal to
<=less than or equal to

 

== (equal to) - Checks if two values are equal.

Example 5.2.1

5 == 5 
Example 5.2.1 - Output

True

 

!= (not equal to) - Checks if two values are not equal.

Example 5.2.2

5 != 3
Example 5.2.2 - Output

True

 

>(greater than) - Checks if the value on the left is greater than the value on the right.

Example 5.2.3

5 > 3 

Example 5.2.3 - Output

True

 

< (less than) - Checks if the value on the left is less than the value on the right.

Example 5.2.4

3 < 5

Example 5.2.4 - Output

True

 

>= (greater than or equal to) - Checks if the value on the left is greater than or equal to the value on the right.

Example 5.2.5

Example: 5 >= 5

Example 5.2.5 - Output

True

 

<= (less than or equal to) - Checks if the value on the left is less than or equal to the value on the right.

Example 5.2.6

Example: 3 <= 5 

Example 5.2.6 - Output

True

 


 

5.2b Why do we use conditional operators?

Conditional operators are essential for decision-making in programs. They allow you to compare values and execute different code blocks based on the result of these comparisons. This enables more complex and dynamic behavior in your programs.

 


 

5.2c Interpret a program snippet that includes conditional operators

Here’s a simple example to demonstrate how these conditional operators can be used in Python.

Example 5.2.7 - Conditional operators

a = 10
b = 20

# Equal to
if a == b:
    print("a is equal to b")
else:
    print("a is not equal to b")  # This will be printed

# Not equal to
if a != b:
    print("a is not equal to b")  # This will be printed

# Greater than
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")  # This will be printed

# Less than
if a < b:
    print("a is less than b")  # This will be printed

# Greater than or equal to
if a >= b:
    print("a is greater than or equal to b")
else:
    print("a is not greater than or equal to b")  # This will be printed

# Less than or equal to
if a <= b:
    print("a is less than or equal to b")  # This will be printed

Example 5.2.7 - Explanation

  • a == b: - Checks if a is equal to b. Since 10 is not equal to 20, it prints "a is not equal to b".
  • a != b: - Checks if a is not equal to b. Since 10 is not equal to 20, it prints "a is not equal to b".
  • a > b: - Checks if a is greater than b. Since 10 is not greater than 20, it prints "a is not greater than b".
  • a < b: - Checks if a is less than b. Since 10 is less than 20, it prints "a is less than b".
  • a >= b: - Checks if a is greater than or equal to b. Since 10 is not greater than or equal to 20, it prints "a is not greater than or equal to b".
  • a <= b: - Checks if a is less than or equal to b. Since 10 is less than or equal to 20, it prints "a is less than or equal to b".

 

As you can see, conditional operators are crucial for making decisions in programs. They allow you to compare values and take different actions based on the results of these comparisons, enabling more sophisticated and responsive behavior in your code.

 


 

Exercise 5.2.1 - Write a Python program that determines the price of a movie ticket based on the age of the person. Use if, elif, and else statements to implement the following rules:

  1. If the person is less than 5 years old, the ticket is free.
  2. If the person is between 5 and 12 years old (inclusive), the ticket price is €5.
  3. If the person is between 13 and 17 years old (inclusive), the ticket price is €10.
  4. If the person is 18 years old or older, the ticket price is €15.

The program should ask the user to input their age and then print out the appropriate ticket price and a message indicating the price category.

Exercise 5.2.1 - Model Answer - Make sure to work out the exercise before checking the model answer.