Module 2 - Section 1 - Controlling the flow
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Python Lab |
Book: | Module 2 - Section 1 - Controlling the flow |
Printed by: | Guest user |
Date: | Saturday, 23 November 2024, 5:54 PM |
Description
After the completion of the this module the students will be able to:
- create code using the if statement
- recognize and use indented blocks of code
- solve Boolean algebra logic problems
- create code using the complicated if statements with elif and else
- recognize nested if statements and use them
2.1.1 - The if statement
How do we tell if a player has beat the high score? How can we tell if he has run out of lives? How can we tell if she has the key required to open the locked door?
What we need is the if statement. The if statement is also known as a conditional statement. (You can use the term “conditional statement” when you want to impress everyone how smart you are.) The if statement allows a computer to make a decision. Is it hot outside? Has the spaceship reached the edge of the screen? Has too much money been withdrawn from the account? A program can test for these conditions with the if statement.
Lets see an example code:
>>> # Variables used in the example if statements
>>> a = 4
>>> b = 5
>>> # Basic comparisons
>>> if a < b:
print("a is less than b")
>>> if a > b:
print("a is greater than b")What do you believe the code will send to the screen(output)?
Since a is less than b, the first statement will print out if this code is run. If the variables a and b were both equal to 4, then neither of the two if statements above would print anything out. The number 4 is not greater than 4, so the if statement would fail.
What other operators can we use for comparing? Copy the following code and save it as basic_comparisons.py inside your working folder.
2.1.2 - Indentation
Indentation matters. Each line under the if statement that is indented will only be executed if the statement is true. After the intended block execution continues as normal. Copy the following code and save it as correct_indentation.py inside your working folder.
Indentation must be exactly the same. Following code doesn't work :
2.1.3 - And and Or
An if statement can check multiple conditions by chaining together comparisons with and and or. These are also considered to be operators just like + or - are.
# And
if a < b and a < c:
print("a is less than b and c")
# Non-exclusive or
if a < b or a < c:
print("a is less than either b or c (or both)")
A common mistake is to omit a variable when checking it against multiple conditions. The code below does not work because the computer does not know what to check against the variable c. It will not assume to check it against a.
# This is not correct
if a < b or < c:
print("a is less than b and c")
2.1.4 - Boolean variables
Python supports Boolean variables. What are Boolean variables? Boolean variables can store either a True or a value of False. Boolean algebra was developed by George Boole back in 1854. If only he knew how important his work would become as the basis for modern computer logic!
An if statement needs an expression to evaluate to True or False. What may seem odd is that it does not actually need to do any comparisons if a variable already evaluates to True or False.
# Boolean data type. This is legal!
a = True
if a:
print("a is true")
Ok so a is (==) True. And what about not a. Is it also True? Of course not. If a is True then not a should be False and if a is False then not a should be True
How can we write this using code? It is very simple.
The following two examples are both correct.
# How to use the not function - example 1
a = False
if not(a): # What is not(a) ?
print("a is false")
# How to use the not function - example 2
if not a: # What is not a ?
print("a is false")
Of course we can combine statements using and
a = True
b = True
if a and b:
print("a and b are both true")
or or operands
a = True
b = False
if a or b:
print("one or both of a and b are true")
2.1.5 - If and more If - Else if? or Else?
When we want to make a decision and from the outcome of the decision act then we use the if command.
Copy and paste the following code to the Thonny's editor and save it as temperature_decision.py inside your working folder.
As you can see inside the code there are three cases. One when temperature is greater than 30, one when temperature is lower than 12 and one in every other case.
Try the code using different temperature more than 30, between 12 and 30 and less than 12.
In the previous code , the program will output “It is hot outside” even if the user types in 70 degrees. Why? How can the code be fixed?
If you want to see a solution click here.
2.1.6 - Multiple cases in IF
What is and inside the if command? and means that in order for the statement to be true both statements should be true. Copy the following code and save it as check_up.py inside your working folder.
What is or inside the if command? or means that in order for the statement to be true one or both statements should be true. Copy the following code and save it as friend.py inside your working folder.
In the programs above think which are the variables and what type are these variables.
We have now a new problem for you.
We want to create a program that asks for three numbers and then displays the greatest.
As an example when we execute the program the output could be :
Hello! Please insert three different integer numbers and i will find the greatest.
The first number is : 13
The second number is : 19
The third number is : 8
Great! The greatest number of 13,19,8 is 19.
You can find a solution here
2.1.7 - If inside an if = nested if?
Sometimes we want to check a condition inside another condition.
If we want to issue a forecast for temperature and humidity we can write the simple code below.
In order for the second condition (humidity) to be examined the first (temperature > 30) must be true.
Copy and paste the following code to Thonny. You can view the steps of execution using the Debug button.
print ("This is my weather forecast")
tempr = int(input("Please insert the Temperature in Celsius : "))
hum = int(input("Please insert the Humidity of the air : "))
if tempr > 30:
if hum > 80:
print ("It's hot and humid")
else:
print ("It's hot but not humid")
else:
print ("It's not hot")