Lesson 11 Slides
Lesson 11 Slides
a.y. 2018-2019
Lesson 11
Decision-making and iterative structures
SEDIN
IT Education Services Center
2
• So far we have been using the sequential structure only, that is a set of
statements which are executed in the order they occur
Sequential structure
Example:
Start
Data
query
Data printing
End
5
The if statement
In Python, the if statement is used to implement a decision-making structure.
In the case of a single-alternative structure, the syntax will be:
if condition: Heading
statement (if clause)
statement
Body
etc. (indented
statement
[rest of the script] block)
If the condition is false, the statement block is skipped and the program
continues
8
Boolean expressions
• The conditions of the if statement are implemented by means of Boolean
expressions, i.e. expressions that can be true or false
• They use comparison operators (<, > etc.) and return True, whether they
are true, or False
• True and False are special values of bool type, and they must be typed
with a capital letter. They are NOT strings!
9
Comparison operators
The comparison
Operator Description Examples can include values
(integers, floats,
4 == 4 -> True
== Equal to strings etc.),
4 == 73 -> False
variables or
4 != 73 -> True functions.
!= Different from
4 != 4 -> False
Example of an if statement
Start
Ask the
value of x
Running the program, it will ask for
the x value and it will behave accordingly:
True
x>0
False Print “x is
positive”
End
11
Start
Ask the
value of x
Print “x is Print “x is
negative or 0” positive”
End
13
Exercise
Set up a program which:
Start
• Requires the user to enter a password (“Enter the
password:”)
Password
request
• Defines whether the entered password is correct, by
comparing it to the one available into the code
(“5ecure”) False Correct True
password
• Prints “Password accepted”, if the password which Print “Sorry, Print “Password
wrong password” accepted”
has been typed is correct
End
• Prints “Sorry, wrong password”, if the password is
not correct
14
Exercise (solution)
Start
Password
request
End
15
Nested conditions
• When more difficult situations occur (more conditions, more possible
outputs), it is possible to nest different if-else statements
• In order to verify two conditions (with three possible outputs), the syntax is
the following:
if condition1: Example:
statements Improve the following script to show also 'x is 0' as a
else: separate output
if condition2:
statements
else:
statements
Ask the
value of x
False True
x>0
False True
Running the program, it x<0
End
17
Exercise
Set up a program which:
• Requires the user to enter how tall is the person on his/her right
(“How tall is the person on your right (in cm)?”)
Exercise (solution)
Start
End
19
Exercise
We need to write a program that converts an input test score (out of 100) into
an American grade (A-F), as shown in the table below:
Score Grade
91-100 A
81-90 B
71-80 C
61-70 D
0-60 F
After the variables have been assigned and the input score requested, we have
to calculate the A-F grade
21
Exercise (solution)
We can do it by means of a if-else chain, but the resulting code is long and
complex. The solution with if-elif is more effective:
Logical operators
They are also called Boolean operators and they enable to create complex
Boolean expressions as well as enhancing conditional constructs potential:
Operator Description
Expression Meaning
Is x greater than y AND is a less than b
x > y and a < b at the same time?
(True or False)
Is x equal to y OR is x equal to z?
x == y or x == z
(True o False)
Example:
if a in range(6,12):
print('The value is OK!')
else:
print('The value is not in the range')
The
…? value is not in the range
>>>
28
Iterative constructs
• Iteration is the ability to execute repeatedly a same block of statements
• Loops are grouped into two categories: loops controlled by a condition and
loops controlled by a counter
Output: You can 3. Modify the example to print a sequence of numbers based on
also write the inputs given by the user (e.g. all even numbers from 8 to 24)
x+=1
31
Output:
32
«Execute the
The syntax is: statements for
each variable
value included in
the sequence»
for variable in [value 1, value 2, etc.]: Has every
value of the
True
statement False
ranges
33
Result:
35
• The break and continue statements respectively are employed for these
two operations:
36
In these cases, the while True syntax could also be employed. It requires the if
and break statements, but not the previous initialization of the variable.
Example:
37
• This happens when we want to test the program or go along with the next
statements, without concluding a certain part
• In these cases we use the pass statement. It acts as a marker for the code
which is going to be completed, avoiding that incomplete constructs return
an error:
38
Book references
Learning Python:
Chapters 5, 6
Assignment:
Exercises of lesson 11