0% found this document useful (0 votes)
16 views9 pages

ICT LAB 12, EEE-018 - Merged

The document discusses loops and nested loops in Python. It provides examples of while, for-in, and nested loops. It then lists tasks for students to write programs that use loops and nested loops to print various number patterns.

Uploaded by

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

ICT LAB 12, EEE-018 - Merged

The document discusses loops and nested loops in Python. It provides examples of while, for-in, and nested loops. It then lists tasks for students to write programs that use loops and nested loops to print various number patterns.

Uploaded by

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

Applications of Information and Communication

Technologies
CSC101

Lab Report-12

Lab title: Python: Loops + Nested Loops

Student Name INAM FAZAL

FA23-EEE-018-ISB
Registration Number

EEE-02
Class/Section

Instructor’s Name SIR KHAN AFSAR

Lab Assessment Marks

Experiment setup /1
Follow procedures /1
In Lab /4
Experiment results /1
Q&A /1
Presentation /2 /6
Post-Lab Analysis /2
Writing /2

COMSATS University Islamabad (CUI), Islamabad Campus Page 1


lOMoAR cPSD| 37532000

lOMoAR cPSD| 3753200

Lab 12
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.

1) Useful Concepts
A loop is a block of code that gets repeated over and over again either a specified number of times or
untilsome 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 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.
3. Sometimes we would want to nest loops. This can be done as well. This will
looksomething like this.

for <item_name> in <sequence_name>:


// some code to repeat
while(<some_condition>):
// some nested code to repeat

for <item_name> in <sequence_name>:


// some code to repeat

Page 1 of 8
lOMoAR cPSD| 37532000

Activity 1:
Python program to illustrate a 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 = ['Math’s','English’,
‘Urdu']for subject in subjects:
print("The name of the subject is:",subject)

Output
The name of the subject is: Math’s.
The name of the subject is: English.
The name of the subject is: Urdu

Page 2 of 8
lOMoAR cPSD| 37532000

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

Page 3 of 8
lOMoAR cPSD| 37532000

INLAB

Lab Task 1
Write a program to print all natural numbers from 1 to n.

Page 4 of 8
lOMoAR cPSD| 37532000

Lab Task 2
Write a program to print all even numbers between 1 to 100.

Page 5 of 8
lOMoAR cPSD| 37532000

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 asked for the user input again till the user enters a prime
number.

Page 6 of 8
lOMoAR cPSD| 37532000

Lab Task 4

Write a program to print a table of numbers from 1 to 8.

Page 7 of 8
lOMoAR cPSD| 37532000

Lab Task 5
Write a nested for loop that prints the following output:

1
121
12421
1248421

Page 8 of 8

You might also like