Form 4 - Computer
2024-2025
Floor 4 - Computer Room
Mr. Peter
While Loop Accumulator

Santa Rosa de Lima English Secondary School
聖羅撒英文中學
Colégio de Santa Rosa de Lima - Secção Inglesa
Outline
What is while accumulator in Python
1.
Explore the concept of Accumulator in Looping
How accumulator works in Python
2.
Focusing on the value change of variables in looping
Python accumulator implementation
3.
Define while loop statement for requirements
What is Accumulator?
What is accumulator?

This is a variable
What is accumulator?

This variable called "accumulator"
What is accumulator?

Zero will be stored in most of cases
0
What is accumulator?

0
accumulator
What is accumulator?

0
accumulator
This is a series of numbers
What is accumulator?

accumulator
Series of numbers
0
What is accumulator?

accumulator
Series of numbers
We use loop statement to process each number in the list to the accumulator
0
What is accumulator?

accumulator
Series of numbers
We use loop statement to process each number in the list to the accumulator
0
What is accumulator?

accumulator
Series of numbers
+
In this case, we process the list with using addition operation
0
What is accumulator?

0
accumulator
Series of numbers
+
In this case, we process the list with using addition operation
=
What is accumulator?

0
accumulator
Series of numbers
+
In this case, we process the list with using addition operation
=
1
What is accumulator?

0
accumulator
Series of numbers
+
The result of processed number will be stored to the accumulator
=
1
What is accumulator?

accumulator
Series of numbers
+
The result of processed number will be stored to the accumulator
=
1
What is accumulator?

accumulator
Series of numbers
Let's process the second number
1
What is accumulator?

accumulator
Series of numbers
1
Let's process the second number
+
=
What is accumulator?

accumulator
Series of numbers
1
Let's process the second number
+
=
3
What is accumulator?

accumulator
Series of numbers
Let's process the second number
+
=
3
What is accumulator?

accumulator
Series of numbers
Let's process the second number
3
What is accumulator?

accumulator
Series of numbers
Let's process the next number
3
+
What is accumulator?

accumulator
Series of numbers
Let's process the next number
6
What is accumulator?

accumulator
Series of numbers
Let's process the next number
6
What is accumulator?

accumulator
Series of numbers
Let's process the next number
6
+
What is accumulator?

accumulator
Series of numbers
Let's process the next number
10
What is accumulator?

accumulator
Series of numbers
Let's process the next number
10
What is accumulator?

accumulator
Series of numbers
Let's process the next number
10
+
What is accumulator?

accumulator
Series of numbers
Let's process the next number
15
What is accumulator?

accumulator
Series of numbers
Let's process the next number
15
What is accumulator?

accumulator
Series of numbers
Let's process the next number
15
+
What is accumulator?

accumulator
Series of numbers
Finally, we get the final result in the final loop
21
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
Start Number
End Number
The increment before numbers
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0

accumulator
0
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0

accumulator
0
Start Number
Addition operator
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
21
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
21
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
21
7
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
21
7
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
21
7
7 21
Accumulator Implementaion
1.
3.
while i <= 6 :
4.
Tab
5.
Tab
i = 1
print(i, s)
s = s + i
6.
Tab
i = i + 1
2.
s = 0
i
_____
s
Output
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
_____
1
1
0
1
2
2 1
3
3
3 3
6
4
4 6
10
5
5 10
15
6
6 15
21
7
7 21
Python Loop Accumulator Statement Exercises
Python Loop Accumulator - Ex01
Write a Python program using a while loop to calculate the sum of all odd numbers from 1 to 99 (i.e., 1, 3, 5, ..., 99).
1.
Save your file as "ClassNumber_YourName_Ex01.py"
Use a while loop to iterate through the numbers.
1
Accumulate the sum of all odd numbers between 1 and 99, inclusive.
2
Print the final sum.
3
The final sum number is 2500
Python Loop Accumulator - Ex02
Write a Python program using a while loop to calculate the product of all integers from 10 down to 1.
1.
Save your file as "ClassNumber_YourName_Ex02.py"
Use a while loop to iterate through the numbers from 10 to 1.
1
Accumulate the product of all these numbers.
2
Print the final product.
3
The final product number is 3628800
F4 Lesson05 - Accumulator in Looping
By Mr Peter
F4 Lesson05 - Accumulator in Looping
- 514