Lecture 15 — If Conditions⌗

Instructor: Laksh Budhrani


Objectives⌗

  1. Understand how an if statement checks whether a condition is True or False.
  2. Understand how relational operators (>, <, ==, >=, <=, !=) evaluate to True or False.
  3. Use if statements to control the movement of objects in an animation.

Objective 1 — Understanding If Conditions⌗

An if statement allows a program to make decisions.

It checks whether a condition is True or False.

  • If the condition is True, Python executes the indented code.
  • If the condition is False, Python skips the indented code.

Example⌗

is_raining = True

if is_raining:
    print("Take an umbrella!")

Since is_raining is True, Python executes the print statement.

If we change is_raining to False, Python skips the print statement.

Remember: An if statement executes its code only when the condition is True.


Objective 2 — Relational Operators⌗

Relational operators compare two values. The result of a comparison is always either True or False.

OperatorMeaning
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
==Equal to
!=Not equal to

Example⌗

temperature = 35

if temperature > 30:
    print("It is a hot day!")

Here, temperature > 30 evaluates to True because 35 is greater than 30.

Therefore, Python executes the print statement.

If the temperature were 25, the condition would be False, and Python would skip the print statement.


Knowledge Check — Quick MCQs⌗

Question 1: When does Python execute the code inside an if statement?

A) When the condition is False
B) When the condition is True
C) Every time
D) Only when the program starts


Question 2: What is the result of the following comparison?

10 > 5

A) True
B) False
C) 10
D) 5


Question 3: What happens when the following code executes?

age = 12

if age >= 13:
    print("You can join!")

A) It prints “You can join!”
B) It prints “12”
C) Nothing is printed
D) The program crashes


Question 4: Which relational operator checks whether two values are equal?

A) =
B) !=
C) ==
D) >=


Objective 3 — Using If Conditions in Animation⌗

The Problem: A Bug That Leaves the Screen⌗

Imagine a bug moving from the left side of the screen to the right side.

Eventually, the bug reaches the edge of the screen and disappears.

How can we make the bug return to the beginning?

We can use an if statement to check whether the bug has reached the edge.

if bug_position >= 600:
    bug_position = 0

When the bug’s position reaches 600, the condition becomes True, and its position is reset to 0.


Guided Activity — Wrapping Ladybug⌗

We will write a looping ladybug animation together in class.

Create a new Python file named animation_bug.py.

Ladybug Animation Video⌗

Starter Template⌗

Copy this template into animation_bug.py. We will fill in the code together.

from animation import run_sketch

# Part 1 - setup. runs one time


# Part 2 - draw. repeats 60 fps
def draw(p):
	pass

    # Draw the ladybug here


    # Move the ladybug forward


    # Check if the ladybug has reached the edge


# don't write anything below this line
run_sketch(draw, bgcolor="lightblue")

Independent Activity — Swimming Fish⌗

Create a new Python file named animation_fish.py.

Fish Animation Video⌗

Your Task⌗

Create a fish that swims from left to right and returns to the beginning when it reaches the edge.

Your animation should:

  • Draw a fish with a body, tail, and eye.
  • Use a position variable starting at 0.
  • Increase the fish’s position so it moves to the right.
  • Use an if statement to check whether the fish has reached the edge.
  • Set the fish’s position back to 0 when it reaches the edge.

Use the ladybug animation as a guide, but write the fish code yourself.

Submission Instructions⌗

Submit your work in Google Classroom by:

  1. Creating a Google Doc.
  2. Pasting your code into the document.
  3. Adding a screenshot of the animation window.
  4. Turning in the Google Doc to the assigned activity.