Lab-11-Loops in Python
Lab-11-Loops in Python
lOMoARcPSD|375 320 00
Lab 13
Loops + Nested Loops
Objective:
The objective of this lab will be to learn about loops and nested loops with the help of examples
and learning tasks.
Activity Outcomes:
The activities provide hands - on practice with the following topics
● Implement a while loop
● Implement a for-in loop
● Implement nested loops
Instructor Note:
As a pre-lab activity, read Chapter 6 from the textbook <Python Basics: A Practical Introduction
to Python 3, 2021=.
1) Useful Concepts
A loop is a block of code that gets repeated over and over again either a specified number of times or until
some condition is met which is why a loop runs only a finite number of times. If we specify a condition that
does not get fulfilled ever during loop iterations then it would go on forever and the program would freeze.
1. One kind of a loop is a while loop. The syntax is given below. We specify a condition in
the round brackets after a while just like we do for the if statement. This loop keeps on
running while the statement passed is true(hence the name while).
while(<condition>):
// some code to repeat
2. Another kind of a loop is a for-in loop. The syntax is given below. A for-in loop iterates
over a sequence(list, string or range). The variable we use after for, gets the value of the
next item in the sequence in each iteration while the variable after in is a sequence which
we want iterated on.
for <item_name> in <sequence_name>:
// some code to repeat
132
lOMoARcPSD|375 320 00
3. Sometimes we would want to nest loops. This can be done as well. This will look
something like this
for <item_name> in <sequence_name>:
// some code to repeat
while(<some_condition>):
// some nested code to repeat
Activity 1:
Python program to illustrate an while loop
Solution:
x = 0
while(x<4):
print("The value of x is:",x)
x = x + 1
Output
The value of x is: 0
The value of x is: 1
The value of x is: 2
The value of x is: 3
Activity 2:
Python program to illustrate an for in loop.
subjects = ['Maths','English','Urdu']
for subject in subjects:
print("The name of the subject is:",subject)
Output
The name of the subject is: Maths
133
lOMoARcPSD|375 320 00
subjects = ['Maths','English','Urdu']
grades = ['A','B']
for subject in subjects:
print("The name of the subject is:",subject)
for grade in grades:
print("The name of the grade is:", grade)
Output
The name of the subject is: Maths
The name of the grade is: A
The name of the grade is: B
The name of the subject is: English
The name of the grade is: A
The name of the grade is: B
The name of the subject is: Urdu
The name of the grade is: A
The name of the grade is: B
134
lOMoARcPSD|375 320 00
Lab Task 1
Write a program to print all natural numbers from 1 to n.
Lab Task 2
Write a program to print all even numbers between 1 to 100.
Lab Task 3
Write a program to ask user input for a number. Check if it is a prime number. If it is, end the program
but if it is not ask for the user input again till the user enters a prime number.
Lab Task 4
Write a program to print a table of numbers from 1 to 8.
Lab Task 5
Write a nested for loop that prints the following output:
1
121
12421
1248421
135