Introduction to Python

Form 4 - Computer

2023-2024

Floor 4 - Computer Room

Mr. Peter

Outline

Outline

While Loop

1

Number Sequences

2

Exercises

3

While Loop Statement

1.

2.

Tab

while Condition :

Command1

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

4

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

4

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

4

True

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

4

True

5

While Loop Statement

1.

2.

while i < 5 :

3.

Tab

4.

Tab

i = 0

print(i)

i = i + 1

i

_______

_______

_______

_______

_______

_______

i < 5

_______

_______

_______

_______

_______

_______

0

True

1

True

2

True

3

True

4

True

5

False

Same

Differrent

1.

2.

Write a Python with While Loop statement to generate 100 numbers starting from number 1, each number should be doubled by previous.

Question

While loop statement - Ex11_1

1 -> 1

2 -> 2

3 -> 4

4 -> 8

5 -> 16

......

100 -> 633825300114114700748351602688

Example

Save the file as "XX_YYYY_Ex11_1.py"

Same

Differrent

1.

2.

Assuming Alice eats apples every day, starting with one apple on the first day and doubling the number each subsequent day, how many apples will Alice have eaten after 30 days?

Question - Apple counting

While loop statement - Ex11_2

Example

Save the file as "XX_YYYY_Ex11_2.py"

Same

Differrent

1.

2.

Assuming Alice has 10,000 dollars, she deposits this amount into a fixed-term bank deposit. In the first year, the bank gives Alice a 5% interest, making it 10,500 dollars. In the second year, Alice deposits the principal of 10,000 dollars and the interest of 500 dollars back into the bank, resulting in 11,025 dollars. The question is, how much will Alice have accumulated after doing this continuously for 30 years?

Question - Compound Effect

While loop statement - Ex11_3

Example

Save the file as "XX_YYYY_Ex11_3.py"

Python - While Loop

By Mr Peter

Python - While Loop

  • 319