Booleans and Loops

Oliver Irwin

10th March 2023

Objectives

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

Checks

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

Booleans

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?

Conditions

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:
  go_to_class
else:
  stay_in_bed

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
else:
    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!")
else:
    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

And

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")
else:
    print("At least one is false")

Or

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")
else:
    print("All conditions are false")

Loops

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:
    student.grade()

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
    else:
       print("yay")