Oliver Irwin
10th March 2023
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:
In Python, booleans are represented by a type: bool
This type can only take two values:
True
False
Booleans are useful, because they allow us to perform checks
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
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 for comparing two values is simple
a > b
a >= b
a < b
a <= b
a == b
a != b
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 |
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 |
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
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!
In Python, the use of for
loops
is simple
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!
Similar use as for
loops