Introduction to Python
Floor 4 - Computer Room
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
Write a Python code to create a loop printing number from 1 to 100, each loop increases 5.
Question
While loop statement - Ex01
0
5
10
15
...
95
100
Example
Save the file as "XX_YYYY_Ex01.py"
Write a Python code to create a loop printing number from 1 to 1024, each loop multiplied by two.
Question
While loop statement - Ex02
1
2
4
8
...
512
1024
Example
Save the file as "XX_YYYY_Ex02.py"
Write a Python program that calculates and prints the values of 𝑦 for 𝑥 ranging from 1 to 20, where 𝑦 is defined by the equation . Your program should use a for loop to iterate through the values of 𝑥 and print the corresponding 𝑦 values.
Question
While loop statement - Ex03
x = 1, y = 1
x = 2, y = 4
x = 3, y = 9
x = 4, y = 16
...
x = 19, y = 361
x = 20, y = 400
Example
Save the file as "XX_YYYY_Ex03.py"
print( 'x = ', x, ', y = ', y )
String concatenation for printing
* Consider looping x from 1 to 20, then loop y from 1 to 400 within the same while loop.