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.
Operator |
Description |
Python syntax |
---|---|---|
\(=\) |
equal |
|
\(\neq\) |
not equal to |
|
\(>\) |
greater than |
|
\(<\) |
less than |
|
\(\geq\) |
greater than or equal to |
|
\(\leq\) |
less than or equal to |
|
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