3.1. Conditional statements#

Conditional statements are statements which we can use to make decisions based on its result. They return a Boolean value of either True or False depending on what condition we have applied.

Table 3.1 Conditional statements#

Operator

Description

Python syntax

\(=\)

equal

x == y

\(\neq\)

not equal to

x != y

\(>\)

greater than

x > y

\(<\)

less than

x < y

\(\geq\)

greater than or equal to

x >= y

\(\leq\)

less than or equal to

x <= y

Python can also assume a numerical value of a boolean value where 1 is the same as True and 0 is the same as False. Create a new Python file called 3_If_statements.py and save it to your OneDrive folder. Enter the following code into your program.

# 3. If statements

# Conditional statements
print(f"1 == 2 : {1 == 2}")
print(f"1 != 2 : {1 != 2}")
print(f"1 > 2  : {1 > 2}")
print(f"1 < 2  : {1 < 2}")
print(f"1 >= 2 : {1 >= 2}")
print(f"1 <= 2 : {1 <= 2}")

Run your program and your should see the following printed to the console.

1 == 2: False
1 != 2: True
1 > 2 : False
1 < 2 : True
1 >= 2: False
1 <= 2: True