loop
loop
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
130
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
131
The name of the subject is: English
The name of the subject is: Urdu
Activity 3:
Python program to illustrate a nested loop.
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
132
3) Graded Lab Tasks
Note: The instructor can design graded lab activities according to the level of difficult and complexity
of the solved lab activities. The lab tasks assigned by the instructor should be evaluated in the same lab.
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
133