0% found this document useful (0 votes)
23 views

Lab 3

Uploaded by

meeqatjutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lab 3

Uploaded by

meeqatjutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CS-143L Programming Fundamentals Lab Lab 3

Name : ________________________________ ID: ___________________________

Lab # 2: Implementation of Simple / Nested WHILE & FOR Loops

OBJECTIVES

In this lab you will learn:


- the use of WHILE Loops and Nested WHILE Loop
- the use of FOR Loops and Nested FOR Loop

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.

The FOR Loop


 A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
 This is less like the for keyword in other programming languages and works more like an
iterator method as found in other object-orientated programming languages.
 With the for loop, we can execute a set of statements, once for each item in a list, tuple,
set etc.

Task 1

The following example prints “Hi Python” 5 times to the console.

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

Suppose the loop is mistakenly written as follows:


sum =0
i=1
while i<=5:
sum=sum+i
print (i)
i=i+1
print ("sum is", sum)

 Here is statement i=i+1 is not in the loop body.


 This loop is infinite because i is always 1 and 1<5 will always be true.
 Note that the entire loop body must be intended inside the loop.

The break Statement

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

The continue Statement

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:

# Get user input for the size of the triangle


n = int(input("Enter the size of the triangle (n): "))
# Initialize variables
row = 1
# Outer loop for rows
while row <= n:
# Inner loop for printing asterisks
col = 1
while col <= row:
print("*", end="")

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

number = int(input("Enter a number: "))

print(f"Multiplication Table for {number}:")

for i in range(1, 11):

result = number * i

print(f"{number} x {i} = {result}")

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

You might also like