Lab 3
Lab 3
OBJECTIVES
LOOPS IN PYTHON
A loop can be used to tell a program to execute statements repeatedly. In other words, A loop
statement allows us to execute a statement or block of statements multiple times.
Python provides two types of loop statements: while loops and for loops.
The WHILE Loop
A WHILE loop executes statements repeatedly if a condition remains true.
WHILE loop is a condition-controlled loop, it is controlled by a true/false condition. It
tests the condition before executing the loop body.
Task 1
count =0
while count < 5:
print ("Hi Python!")
count= count+1
print ("Done")
Page 1 of 7
CS-143L Programming Fundamentals Lab Lab 3
While the condition is tested and the result is false, the loop body will be skipped and the first
statement after the while loop will be executed.
Task 2
Page 2 of 7
CS-143L Programming Fundamentals Lab Lab 3
With the break statement we can stop the loop even if the while condition is true:
Task 3
Exit the loop when i is 3:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
With the continue statement we can stop the current iteration, and continue with the next:
Task 4
Write a Python program that takes an integer n as input and prints a right-angled triangle pattern
of asterisks (*) as follows:
Page 3 of 7
CS-143L Programming Fundamentals Lab Lab 3
col += 1
# Move to the next row and start a new line
row += 1
print()
# End of the program
How it works:
1. The program takes user input for the size of the right-angled triangle (n).
2. It uses two while loops: the outer loop (row) controls the rows, and the inner loop (col)
controls the number of asterisks to print in each row.
3. Inside the inner loop, the program prints an asterisk and increments the column variable.
4. After printing each row, the outer loop increments the row variable and starts a new line.
5. The program continues this process until it completes the right-angled triangle pattern.
Task 5
Write a Python program using a for loop to print the multiplication table for a given number. The
program should take the number as input from the user and print its multiplication table from 1
to 10.
Page 4 of 7
CS-143L Programming Fundamentals Lab Lab 3
result = number * i
How it works:
1. The program first takes a number as input from the user using the input function and
converts it to an integer using int().
2. It then uses a for loop to iterate over the range from 1 to 10 (inclusive).
3. Inside the loop, it calculates the result of multiplying the input number by the current
loop variable (i) and prints the result in the format "number x i = result".
4. The loop will execute 10 times, printing the multiplication table from 1 to 10 for the
given input number.
Page 5 of 7
CS-143L Programming Fundamentals Lab Lab 3
LAB ASSIGNMENT
1- Write the program that asks an integer from user and return the cumulative sum of that
integer starting from 1.
2- Write a program to display the sum of all even numbers from 0 to the number N, entered by
the user.
3- Write a Python program (Using Nested While) that takes an integer `n` as input and prints a
right-angled triangle pattern of numbers as shown below.
Page 6 of 7
CS-143L Programming Fundamentals Lab Lab 3
4- Rewrite the Python Code of Question No. 3 using Nested For Loop.
5- Write a Python program that takes an integer `n` as input and prints a square shape pattern
of numbers. Write this code twice by using both approaches (using Nested While and Nested
For ).
Page 7 of 7