0% found this document useful (0 votes)
20 views14 pages

Loopsinpython 2 230520083813 9a86fc17

Uploaded by

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

Loopsinpython 2 230520083813 9a86fc17

Uploaded by

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

Loops in Python

Programming Language

Submitted By:
Maghfoor Ahmad
MCA TYC 2nd Sem
Roll No: 27512100052
Introduction
Sometimes, there is a need to tell a computer
to repeat an action without writing separate
code for each repetition. In programming, this
concept is called loops.

Loops
A loop can be used to tell a program to execute
statements repeatedly. Or we can say that a loop
repeatedly executes the same set of instructions
until a termination condition is met.
Loops changes the control flow of program
that’s why they are also called Control Structure.
Why is it important?
Suppose that you need to display a
string (e.g., Good Morning! ) a
hundred times.
print(“Good Morning!“)

It would be tedious to write this


statement a hundred times:
print(“Good Morning!“)
print(“Good Morning!“)
.
.
.
.
print(“Good Morning!“)
Fortunately, We have loops. Using a loop control statement, you simply tell the
computer to display a string a hundred times without having to code the print
statement a hundred times, as follows:
for i in range (0,100):
print(“Good Morning!")

This will write the Good Morning! hundred times as follows:

Output: Good Morning!


Good Morning!
Good Morning!
Good Morning!
Good Morning!
.
.
.
Good Morning!
Good Morning!
Type of Loops
Python programming language provides
following types of loops to handle looping
requirements. They are:
● While Loop
● For Loop
● Nested Loop

Let’s understand each of the loops


mentioned above.
While Loop
A while loop statement in Python programming language repeatedly executes a
block of statement as long as a given condition is true.

The syntax of a while loop in Python


programming language is:
while expression:
statement(s)

Here, statement(s) may be a single statement


or a block of statements. The expression may
be any condition.

The loop iterates while the condition is true.


When the condition becomes false, program
control passes to the line immediately
following the loop.
Source: Tutorials Point
Example of while Loop
Program to print 1 to 10 using while loop
i=1
#The while loop will iterate until condition becomes false.
While(i<=10):
print(i)
i=i+1

Output:
1
2
3
4
5
6
7
8
9
10
for Loop
A for loop is used for iterating over a sequence (that is either a range, a list, a tuple, a
dictionary, a set, or a string)
The syntax of a for loop in Python is:

for iterating_var in sequence:


statement(s)

Here, the iterating_var can be either an iterable


or a sequence item. If it is an index, we use
the range() function instead of the sequence.
Otherwise, we can mention the sequence.

For each item in sequence, the statements block


will be executed until there are no item. After no
items left in sequence the control will be
transferred to code following the loop. Source: Tutorials Point
Example of for Loop
LIST: Program to print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

Output:
apple
banana
cherry

Range: Program to print “Hello” 3 times:


for i in range(0,3):
print("Hello")

Output:
Hello
Hello
Hello
Nested Loops
A nested loop is a loop inside the body of a loop. It can be for loop inside a for loop or
while inside a while loop. It can also be for inside a while loop and vice versa.
The syntax of a nested loop in Python programming language is:

#Outer Loop #Outer Loop


for iterating_var1 in sequence1: while expression:
#Inner Loop #Inner Loop
for iterating_var2 in sequence2: while expression:
statement(s) statement(s)
statement(s)

For each iteration, the outer loop will executes once and the inner loop will
executes till completion.

The nested loops can be understood by following examples:


● Printing multiplication table of numbers (1 to 3)
● Printing a matrix of 3 column and 2 rows
Example of Nested while Loop
Printing multiplication table up to 5 of numbers (1 to 3)
i=1
while i<=3: #Outer Loop
j=1
while j<=5: #Inner Loop
print i,"*",j,'=',i*j
j+=1 #Inner Loop Ends
i+=1
print "\n" #Outer Loop Ends

Output:

1*1=1 2*1=2 3*1=3


1*2=2 2*2=4 3*2=6
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3 * 4 = 12
1*5=5 2 * 5 = 10 3 * 5 = 15
Example of Nested for Loop
Printing a matrix of 3 column and 2 rows Output:

for row in range(2): 111


for col in range(3): 111
print("1", end= " ")
print("\n")

Key Differences
while Loop for Loop
While loop is used when the For loop is used when the
1.
number of iteration is not known. number of iteration is known.
It shows an error if no condition is It iterates infinite time, if no
2.
given. condition is given.
Initialization, condition checking,
Only initialization and condition
3. and iteration statements are
checking is written at the top
written at the top.
Thankyou!
Bibliography
1. Reddy, Priyanka. “Introduction to Loops in Coding”. Codingal, 2021.
https://www.codingal.com/blog/coding-for-kids/loops-in-programming/

2. Ansari, Rumman. “C Programming Loop Introduction”. Atnyla, 2019.


https://www.atnyla.com/tutorial/loop-introduction-in-c/1/194

3. Gupta, Raghav, Nishant, Priyanka Gupta, and Naveen Sharma. Computational


Problem Solving With Programming In Python. Kalyani Publishers, 2019.

4. “Pyhton - Loops”. Tutorials Point.


https://www.tutorialspoint.com/python/python_loops.htm

5. “Pyhton while Loop Statements”. Tutorials Point.


https://www.tutorialspoint.com/python/python_loops.htm

6. “Python For Loops”. W3 Schools.


https://www.w3schools.com/python/python_for_loops.asp

7. Vishal. “Nested Loops in Python”. Pynative, 2021.


https://pynative.com/python-nested-loops/#h-what-is-a-nested-loop-in-python

You might also like