L5- Python Iteratives
L5- Python Iteratives
Control structures
Control structures
Conditional Iterative
Yes
Actions
Example
Variable
number = 1
Initialization
while (number <= 10): Condition for repetition
print(number) Repeated actions
number+=1 Update
Example 2
What actions should we do if we want to add all the natural
numbers from 1 to 10?
NOT VALID
1+2+3+4+5+6+7+8+9+10
0
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
15 + 6 = 21
21 + 7 = 28
28 + 8 = 36
36 + 9 = 45
45 + 10 = 55
Example 2
What actions should we do if we want to add all the natural
numbers from 1 to 10?
0
• What variables do we need? 0 + 1 = 1
1 + 2 = 3
• How should we initialize them? 3 + 3 = 6
• What actions should we repeat? 6 + 4 = 10
+ 5
10+ = 15
– Update 15+
+ 6 = 21
• How do we know when to finish? + 7
21+ = 28
+ 8
28+ = 36
36+
+ 9 = 45
+
45+10 = 55
number sum_all
Example 2 (while)
no
number <= 10
yes
Yes
Actions
Example
Variable
number = 1
Initialization
sum_all= 0
1 1 2 2 1 1
2 3 3 5 2 3
3 6 4 9 3 6
4 10 5 14 4 10
5 15 6 20 5 15
6 21 7 27 6 21
7 28 8 35 7 28
8 36 9 44 8 36
9 45 10 54 9 45
10 55 11 65 10 55
num sum
Example 3
Modify the program in the example above to add all the
natural numbers smaller than a number n entered by the
user.
Example 3 (while)
no
number <= limit
number = 1
yes sum_all = 0
print(sum_all)
print (sum_all)
Example 3
Modify the program in the example above to add all the
natural numbers smaller than a number n entered by the
user.
number = 1
sum_all = 0
• We will start from the program implemented in previous lecture: It asked the
user to enter two numbers through the keyboard, showed a menu with the
available operations, and asked the user what operation wanted to do.
• 2nd version: Once the user has selected the option to exit, the program will
ask if he/she wants to change the operands.
Ø If the user answers "Y", the program will go back to the beginning
(ask the user to enter two numbers)
Ø If the user answers "N", the program will end.
Ø Otherwise, the program will show an error message and will repeat the
question.
Iterative structures: for
The for structure is basically used when we want to execute a set of actions a
given number of times.
Syntax Flowchart
for <variable> in range(<start>,<stop>,<step>):
<actions>
Variable
range(<start>,<stop>,<step>))
Condition?
Function that generates the sequence of numbers
starting at <start> and ending at <stop> -1 with a Yes
step <step>
Actions
No
By default, <start>= 0 and <step>= 1
Update
Example
range(11)
Generates 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
range(2,11,2)
Generates 2, 4, 6, 8, 10
Iterative structures: for
The for structure is basically used when we want to execute a set of actions a
given number of times.
Syntax Flowchart
for <variable> in range(<start>,<stop>,<step>):
<actions>
number = 0
number < 11
Example (summation)
suma_all = 0 Yes
sum_alll += number
for number in range(11): No
suma_all += number
number += 1
print(“Summation: “, sum_all)
Example 3: summation (for)
Modify the program in the example above to add all the
natural numbers smaller than a number n entered via
keyboard.
In this case:
• Use a for structure to compute the sum.
• Use a while structure to ensure the number n is positive and
show an error message otherwise.
Example 3: summation (for)
sum_all = 0
print(sum_all)
Exercise: Count positives and negatives
Write a program that reads 10 numbers from the keyboard and tells us
how many positive numbers and how many negative numbers we have
entered.
Zero does not count neither as positive nor as negative.
Exercise: Statistics of marks
Write a program that calculates some statistics from a series of marks
entered by the user.
The program must ask the user how many marks he/she is going to
enter.
Then, the program must ask the user to enter the marks one by one.
while for
Nº of It depends on Known
repetitions condition: 0 or + 0 or +