Introduction to Python

Form 3 - 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

Write a Python program that uses a while loop to print lines of asterisks (*). The first line should contain 1 asterisk, the second line should contain 2 asterisks, and so on, up to the tenth line which should contain 10 asterisks.

Question

While loop statement - Ex01

*
**
***
****
*****
******
*******
********
*********
**********

Example

Save the file as "XX_YYYY_Ex01.py"

Asterisks Repeat = String \times Integer

Write a Python program that uses a while loop to print lines of asterisks (*). The program should first print 1 asterisk on the first line, 2 asterisks on the second line, and so on, up to 5 asterisks. After reaching 5 asterisks, the program should then print 4 asterisks on the next line, 3 asterisks on the following line, and so on, down to 1 asterisk.

Question

While loop statement - Ex02

*
**
***
****
*****
****
***
**
*

Example

Save the file as "XX_YYYY_Ex02.py"

Asterisks Repeat = String \times Integer

Write a Python program that uses a while loop to print a right triangle of asterisks (*). The first line should contain 1 asterisk, the second line should contain 2 asterisks, and so on, up to the tenth line which should contain 10 asterisks. The asterisks should be right-aligned.

Question

While loop statement - Ex03

Example

Save the file as "XX_YYYY_Ex03.py"

Asterisks Repeat = String \times Integer
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

String could be a space

Write a Python program that uses a while loop to print a right triangle of asterisks (*). The first part of the program should print lines with 1 asterisk, 2 asterisks, and so on, up to 5 asterisks. The second part should print lines with 4 asterisks, 3 asterisks, and so on, down to 1 asterisk. The asterisks should be right-aligned.

Question

While loop statement - Ex04

Example

Save the file as "XX_YYYY_Ex04.py"

Asterisks Repeat = String \times Integer
    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *

String could be a space

Python - While Loop 04

By Mr Peter

Python - While Loop 04

  • 191