0% found this document useful (0 votes)
7 views6 pages

Python 2

This document explains the use of for and while loops in Python, including their syntax and examples. It details how for loops iterate over sequences and can include an optional else block, while while loops continue as long as a condition is true. Additionally, it provides examples of both loop types and their behavior with the else statement.
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)
7 views6 pages

Python 2

This document explains the use of for and while loops in Python, including their syntax and examples. It details how for loops iterate over sequences and can include an optional else block, while while loops continue as long as a condition is true. Additionally, it provides examples of both loop types and their behavior with the else statement.
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/ 6

Looping in Python:

For Loop:
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other
iterable objects. Iterating over a sequence is called traversal.
Syntax of for Loop
for val in sequence:

Body of for

Here, val is the variable that takes the value of the item inside the sequence on
each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is
separated from the rest of the code using indentation.

Flowchart of for Loop

for each
item in
sequence

Last
Yes
item
reached?

No

Body of for

Exit loop

Fig: operation of for loop

19/40
Example: Python for Loop
# Program to find the sum of all numbers stored in a list

# List of numbers

numbers = [6, 5, 3, 8,4, 2, 5, 4, 11]

# variable to store the sum

Sum =0

# iterate over the list

for val in numbers:

sum = sum+val

#Output: The sum is 48

print("The sum is", sum)


Output:

The sum is 48
for loop with else
A for loop can have an optional else block as well. The else part is executed if the
items in the sequence used in for loop exhausts.
break statement can be used to stop a for loop. In such case, the else part is
ignored.

Hence, a for loop's else part runs if no break occurs.

Here is an example to illustrate this.


digits = [0, 1, 5]

for i in digits:

print(i)
else:

print("No items left.")

Output:

0
1
5
No items left.

Here, the for loop prints items of the list until the loop exhausts. When the for loop
exhausts, it executes the block of code in the else and prints.

22/40

Output:

No items left.
while loop:
The while loop in Python is used to iterate over a block of code as long as the test
expression (condition) is true.

We generally use this loop when we don't know beforehand., the number of times
to iterate.

Syntax of while Loop in Python

while test_expression:
Body of while

In while loop, test expression is checked first. The body of the loop is entered only
if the test expression evaluates to True. After one iteration, the test expression is
checked again. This process continues until the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.

Body starts with indentation and the first unindented line marks the end.

Python interprets any non-zero value as True. None and Oare interpreted as False.

23/40

Flowchart of while Loop

Enter while loop

Test False Vishal Chavre


Expression

True

Body of
while

Exit loop

Fig: operation of while loop


Example: Python while Loop
# Program to add natural

# numbers upto
# sum = 1+2+3+...+n

# To take input from the user,

#n= int(input("Enter n: ")

n= 10

# initialize sum and counter

sum = 0

i=l

24/40

while i <= n:

Sum = sum +i

i= i+l # update counter

#print the sum

print("The sum is", sum)

Output:

Enter n: 10
The sum is 55 K
while loop with else

Same as that of for loop, we can have an optional else block with while loop as
well.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement. In such case,
the else part is ignored. Hence, a while loop's else part runs if no break occurs and
the condition is false.

25/40

Example:
# Example to illustrate

# the use of else statement

# with the while loop


counter = 0

while counter <3:

print("Inside loop")
counter = counter + 1

else:

print("Inside else")

Output:

Inside loop
Inside loop
Inside loop
Inside else

Here, we use a counter variable to print the string Inside loop three times.
On the forth iteration, the condition in while becomes False. Hence, the else part is
executed.

You might also like