-
Notifications
You must be signed in to change notification settings - Fork 1
Control Flow
In this lesson, we will learn about ways to control how a program behave.
Boolean is a kind of datatype. It have only two values: True
and False
.
Now you can think of your true-false questions on your tests as booleans!
Comparison operators are symbols that will give us results in boolean. Let's first introduce them:
This is used to check if 2 variable/values are same. For example:
5 == 5 # This give us True
5 == 0 # This give us False
Just like equals, this checks for exact opposite of equals:
5 != 5 # This give us False
5 != 0 # This give us True
This checks for if a value/variable is greater than another:
5 > 5 # This give us False
5 > 0 # This give us True
This checks for if a value/variable is less than another:
5 < 5 # This give us False
0 < 5 # This give us True
This checks for if a value/variable is greater than or equals another:
5 >= 5 # This give us True
0 >= 5 # This give us False
This checks for if a value/variable is less than or equals another:
5 <= 5 # This give us True
0 <= 5 # This give us True
Conditions are formulas written with boolean values or operators. That means all the previous examples are conditions! All of them used a boolean operator, and they give back a boolean value as result.
Now, we will show some more complicated conditions (boolean statements): Try to calculate their value!
1. True == True
2. True != False
3. 42 == '42'
4. 0 == 0.0
What if we want something more complicated? So far we have only 1 condition. What if we want multiple conditions or at least 1 condition to happen?
Let's introduce them.
Conditions on both sides of this operator must be True
for this to be True
.
That is, both things must happen for this condition to pass.
For example:
5 == 5 and 5 > 0 # True
5 == 5 and 5 > 9 # False
At least 1 condition on both sides of this operator must be True
for this to be True
.
That is, one thing must happen for this condition to pass.
For example:
True == False or 5 > 0 # True
True == False or 5 > 9 # False
Using this keyword, we can flip boolean values. That is, True
to False
and False
to True
not True # False
not False # True
not 0 == 6 # True
not 10 = 10.0 # False
Now, with these operators, we can write more complex conditions. Try to calculate their boolean value.
1. True != False and False != False
2. not 9 > 0 or not 0 > 9
3. False or True and False
These are harder ;3c
1. 3 > 2 > 1 < 10
2. 1 and True
3. not not True or not not False
Now, we can use these conditions to control programs!
The if statement executes commands base on the boolean value of the condition. As an Example:
condition = True #False
if condition:
print('condition is True!')
else:
print('Condition is False.')
Here, we have the condition to be True
, so then we will print out 'condition is True!'
Now, let's amp it up!
The while statement executes commands until the condition is no longer True
.
Lets look at an example:
condition = 0
while condition < 5:
print(cond)
cond = cond + 1
This code will print out the following:
0
1
2
3
4
Last but not least, let's look at the 'for' statement.
The for statement executes commands a set number of times. Precisely, it goes over a list of things, and stop when it runs out.
Let's solidify this concept with an example:
for i in range(4):
print(2*i)
The output of this program would be:
0
2
4
6
The range(n)
function returns a list of numbers in order from 0
to n-1
. so range(4)
give us 0, 1, 2, 3
.
So the for
loop goes from 0 to 3.
The import
statements are useful for bringing in other programs and functions.
There are many packages that can be used so we do not have to write them ourselves!
import math
print(math.pi)
The output:
3.141592653589793