score = 0
# First mission
has_succeeded = True
# Check if the mission was successful
if has_succeeded:
print("Mission Passed")
print("Respect+")
score += 10
print(score)Mission Passed
Respect+
10
if statements using correct syntax, indentation, and conditions.if statements to control program behaviour based on conditions.So far you’ve only written programs that run the same instructions everytime. But what if a program needs to make a choice and based on it change behaviours?
For example, one might ask:
Is the password correct?
The answer could be True or False. Based on that, the program must behave differently:
- If True, display Access Granted!
- If False, display Access Denied
Python uses comparison operators and logical operators to ask YES/NO questions. Then, based on the answer it uses conditional statements to change the behaviour of the program.
If you haven’t read the pages:
- Comparison Operators
- Logical Operators
Then a short version is provided below. As you start using if statements, you’ll want to go back and read those pages (these topics go hand-in-hand).
Comparison operators
a == ba != ba < ba <= ba > ba >= bLogical operators
and: combines two conditions as True if both are True.or: combines two conditions as True if either is True.not : inverts a condition.if statementConditionally execute a set of instrcutions if a given condition is true.
if
A conditional if statement in Python includes the following elements in this specific order.
if keyword followed by a spacecondition that evaluates to True/False.: followed by a newline.statement_body) or set of instructions executed if the condition is True.The statement works as follows:

A common mistake beginners make is to forget the indentation of the lines of code within the if statement
condition of the ifLet’s focus on how to build conditions using comparison operators, booleans and logical operators effectively.
This will be the most common way of building conditional statements in a program for this course. Comparing variables, especially numbers with each other to add decisions.
For instance, let’s consider a program which determines if a user is old enough to vote:
if statement checks if the age is greater or equal to 18. If the condition is true, it prints You can vote.if statement checks the opposite condition, so if the user’s age is lower than 18. If it’s true, it prints You can’t vote.print statement is outside the if blocks and will execute every time the code runs, no matter the age variable’s value. Showing that it is independent from the if condition.🎯 Test Myself: Modify the value age in the code above to display You can vote.
Set the value of age to 18 (or greater).
Boolean variables and conditional statements are designed to be used together. Since a condition is a boolean, you can simply use any boolean value or variable as is in an if statement. For instance, consider the boolean is_raining. We would like to display a message if this boolean is True. One might be tempted to write:
This isn’t wrong, but it’s redundant!
is_raining == True creates a boolean expression that asks whether is_raining is True (or not).True, it can serve directly as the condition of the ifThis code can be re-written as such:
When adding decision making, we sometimes need to check if multiple conditions are met at the same time or if one of them is met. This is when logical operators come in handy!
Let’s go back an example we’ve used in a previous topic. You’re trying to make a program which verifies if it will rain tomorrow based on the humidy_index and the temperature_variation:
It will rain tomorrow if the humidify is above 0.8 AND the temperature is decreasing (variation is negative)
To combine these two conditions we can use the and operator:
🎯 Test Myself: What would be the output of the program if we replaced the and with an or?
The conditon will be True since the OR required at least one to be True. The output will be: "It will rain tomorrow"
Keep in mind the order of operations when evaluating a conditional statement. For instance, let’s test if a is smaller or equal to b and c is NOT true in the following example:
Let’s break it down:
<= takes precedence over the rest and runs first:
a is smaller than b, so a <=b → Truenot operation takes precedence over the and and runs second
c is True it gets inverted by the not becoming True.and is now executed combining: True and True → Trueif is True, the condition body is executed nextprint("Yeeyy") gets executed.Remember using more () than required is always better than writing ambigious statements
PAY ATTENTION TO THE PARENTHESIS
I want a large pizza and either a coke or rootbeer
I want a large pizza or 2 small pizzas and a coke
I want 8 large pizzas and 15 cokes and 15 rootbeers, and if they don’t have rootbeers, then 30 cokes
🎯 Test Myself: Complete the following program to print Team C qualifies! if they won at least one game, Or, if they lost none, but scored at least 3 goals while conceding not more than 1 goal.
statement_body of the ifAll the lines indented under the if condition: are part of the body and are executed if the condition is True, otherwise they’re skipped.
A variable defined before an if statement can be modified inside its body. If the condition is True, the instructions in the if body are executed, and any changes made to the variable are retained after the if block.
For example, imagine a game where a player earns 10 points each time they successfully complete a mission:
Mission Passed
Respect+
10
Since has_succeeded is True:
if condition is satisfied, so its body is executed.score is increased by 10, changing its value from 0 to 10.if block finishes, score still has the value 10.🎯 Test Myself: What would be the output of this program if the assignment changes tohas_succeeded = False?
0
if statements
Nesting a condition is placing it inside an outer condition.
For instance, consider those numbers: number1 = 200,number2 = 33,number3 = 500. Let’s imagine we’re trying to verify if number2 is the smaller number. This translates in verifying if number1 > number2, then if that’s true, verify if number3 > number2 to conclude if it’s the smallest number:
This would translate into:
These nested conditions can often be simplified using logical operators and counter conditions.