Booleans and Loops

Oliver Irwin

10th March 2023


  • explain the concept of boolean / binary
  • use loops to build more complex scripts
  • build a simple game


When using flowcharts, we used checks

They were a way to change the behaviour of a program according to the value of some parameter

Without surprise, we can do the same in Python


In most programming languages, booleans are the way to represent choice statements

The easiest way to think about this is to think about binary statements:

  • Yes / No
  • True / False

Booleans - Python

In Python, booleans are represented by a type: bool

This type can only take two values:

  • True
  • False
adult = True
student = False

Checks - Python

Booleans are useful, because they allow us to perform checks

  • Are you an adult?
  • Is this value correct?
  • Is the water boiling? The pasta cooked?


Checks in Python are done by using conditions on the code to execute

We use if / then / else methodology to decide what to do

if is_a_student:

Python syntax

In Python, conditions are equalities between boolean values

It’s the same thing as in real life!

is pasta cooked ? \(\implies\) pasta == cooked?

Python syntax

Python syntax for comparing two values is simple

  • Strictly greater: a > b
  • Greater or equal: a >= b
  • Stricly smaller: a < b
  • Smaller or equal: a <= b
  • Equal: a == b
  • Different: a != b

Python checks

if condition:
    code to execute
elif second_condition:
    code to execute
    code to execute

Python example

alive = True
nb_lives = 1

if alive:
    print("I'm alive")
    if nb_lives > 1:
        print(f"I have {nb_lives} lives left")
    elif nb_lives == 1:
        print("On my last life!")
    print("I'm dead")

Complex conditions

Sometimes we need to use more complex conditions to represent

Conditions can be built as a combination of smaller conditions


A condition can be true if ALL its subconditions are true

This is done by using the and keyword

and True False
True True False
False False False

In Python

if condition1 and condition2:
    print("Both are true")
    print("At least one is false")


A condition can be true if ANY its subconditions is true

This is done by using the or keyword

or True False
True True True
False True False

In Python

if condition1 or condition2:
    print("At least one is true")
    print("All conditions are false")


Sometimes, you have to repeat an action to solve a problem

In programming languages, this is done by using loops

Two types of loops in Python: for and while

For loop

Used when you know on what you are looping

Allows to repeat an instruction for a given number of times

Useful when you know how many times you need to repeat

I have to go to school for all my classes!

Python for

In Python, the use of for loops is simple

for i in range(10):
    print(i, sep=" ")

>>> 0 1 2 3 4 5 6 7 8 9
for student in student_list:

While loop

Used to repeat a portion of code while a condition is true

Useful when you do not know how many times to execute

While it’s sunny, I’ll play outside!

While Python

Similar use as for loops

while alive:
    if is_hit:
        alive = False