Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
Unit 2: Loop
Why Looping?
o Loops are important in Python or in any other programming language as they help you
to execute a block of code repeatedly.
o You will often come face to face with situations where you would need to use a piece
of code over and over but you don't want to write the same line of code multiple times.
What is Looping?
o In computer Programming, a Loop is used to execute a group of instructions or a block
of code multiple times, without writing it repeatedly.
o The block of code is executed based on a certain condition.
o Loops are the control structures of a program.
Two Types of Loop in Python
1. while loop
2. for loop
3. listener loop
while Loop:
o Python While Loop is used to execute a block of statements repeatedly until a
given condition is satisfied.
o And when the condition becomes false, the line immediately after the loop in
the program is executed.
Syntax:
while condition:
Statement
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
When to use while loop?
While loop falls under the category of indefinite iteration. Indefinite iteration means that the
number of times the loop is executed isn’t specified explicitly in advance.
Working of while loop:
Statements represent all the statements indented by the same number of character spaces after
a programming construct are considered to be part of a single block of code. Python uses
indentation as its method of grouping statements. When a while loop is executed, expr is first
evaluated in a Boolean context and if it is true, the loop body is executed. Then the expr is
checked again, if it is still true then the body is executed again and this continues until the
expression becomes false.
Example: Display Hello message three times
i=1
while i<=3:
print("Hello")
i+=1
Output:
Hello
Hello
Hello
While loop with else:
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
Syntax while with else:
while condition:
Statements
else:
Statements
while loop executes the block until a condition is satisfied. When the condition becomes false,
the statement immediately after the loop is executed. The else clause is only executed when
your while condition becomes false. If you break out of the loop, or if an exception is raised, it
won’t be executed.
Note: The else block just after for/while is executed only when the loop is NOT terminated
by a break statement.
Example:
i=0
while i<4:
i+=1
print(i)
else:
print("End while")
Output:
1
2
3
End While
Listener Loop
o One very common pattern is called a listener loop. Inside the while loop there is a
function call to get user input. The loop repeats indefinitely, until a particular
input is received.
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
o Here user decides that how many times he wants to execute the loop. For this, we
use a sentinel value. A sentinel value is a value that is used to terminate a loop
whenever a user enters it, generally, the sentinel value is -1.
Example: Python while loop with user input
a=1
while a != -1:
a = int(input('Enter a number (-1 to quit): '))
Output:
Explanation:
First, it asks the user to input a number. if the user enters -1 then the loop will not execute
User enter 6 and the body of the loop executes and again ask for input
Here user can input many times until he enters -1 to stop the loop
User can decide how many times he wants to enter input
For Loop
Python For loop is used for sequential traversal i.e. it is used for iterating over an
iterable like String, Tuple, List, Set or Dictionary.
For Loops Syntax
for var in
iterable: #
statements
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
Here the iterable is a collection of objects like lists, tuples. The indented statements inside the
for loops are executed once for each item in an iterable. The variable var takes the value of the
next item of the iterable each time through the loop.
Example 1: Using For Loops in Python List
l = ["Mango", "Apple", "Banana"]
for i in l:
print(i)
Output:
Mango
Apple
Banana
Example 2: Using For Loops in Python String
str="Swapnali"
for i in str:
print(i)
Output:
S
w
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
range() function in Python
Python range() is a built-in function that is used when a user needs to perform an action
a specific number of times.
The range() function is used to generate a sequence of numbers. Depending on how many
arguments the user is passing to the function, user can decide where that series of numbers will
begin and end as well as how big the difference will be between one number and the
next.range() takes mainly three arguments.
start: integer starting from which the sequence of integers is to be returned
stop: integer before which the sequence of integers is to be
returned. The range of integers end at stop – 1.
step: integer value which determines the increment between each integer in the sequence
Example:
#Display 0 to 5 numbers
for i in range(6):
print(i)
Output:
0
1
2
3
4
5
For loop in Python with else
Python also allows us to use the else condition with for loops.
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
Note: The else block just after for/while is executed only when the loop is NOT terminated
by a break statement
Example: print 1 to 5 numbers
for i in range(1,6):
print(i)
else:
print(for i in range(6):
print(i)
else:
print('no more iteration')
Output:
0
1
2
3
4
5
No more iteration
Example: Odd numbers between 1 to 10
for i in range(1,6,2):
print(i)
else:
print('no more iteration')
Output:
1
3
5
7
9
No more iteration
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
break Statement: The break statement is used inside the loop to exit out of the loop.
Example of break Statement Output
for i in range(6): 0
if (i=3): 1
break 2
print(i)
In this code, loop is exit when i becomes 3.
continue Statement: The continue statement skip the current iteration and move to the next
iteration.
Example of continue statement Output
for i in range(6): 0
if (i==3): 1
continue 2
print(i) 4
5
In this code, skip the iteration when i becomes 3.
pass statement in python:
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
The Python pass statement is a null statement. But the difference between pass and comment is
that comment is ignored by the interpreter whereas pass is not ignored.
The Syntax of the pass statement
pass
What is pass statement in Python?
When the user does not know what code to write, So user simply places a pass at that line.
Sometimes, the pass is used when the user doesn’t want any code to execute. So users can simply
place a pass where empty code is not allowed, like in loops, function definitions, class
definitions, or in if statements. So, using a pass statement user avoids this error.
Why Python Needs “pass” Statement?
If we do not use pass or simply enter a comment or a blank here, we will receive
an IndentationError error message.
Example:
a=10
if a > 10:
#nothing to print
print("Hello")
Output:
IndentationError: expected an indented block after 'if' statement
Correct code is:
a=10
if a > 10:
pass
print("Hello")
Output:
Hello
Prepared By: Prof. S. S. Gawali
Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025
Prepared By: Prof. S. S. Gawali