[go: up one dir, main page]

0% found this document useful (0 votes)
13 views2 pages

Looping

The document explains iterations in programming, specifically focusing on three types of loops: For Loop, While Loop, and Nested Loop. It provides examples of how to use each loop type in Python, including iterating over sequences and using the range() function. Additionally, it discusses the use of the else statement with while loops and the conditions for nested loops.
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)
13 views2 pages

Looping

The document explains iterations in programming, specifically focusing on three types of loops: For Loop, While Loop, and Nested Loop. It provides examples of how to use each loop type in Python, including iterating over sequences and using the range() function. Additionally, it discusses the use of the else statement with while loops and the conditions for nested loops.
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/ 2

Iteration (Loop)

Iterations are used to repeat a set of blocks of code as long as condition as to.
There are three types of loops:
1. For Loop
2. While Loop
3. Nested Loop

1. For Loop
for loops can iterate over a sequence of iterable objects in python. Iterating over a sequence is nothing but
iterating over strings, lists, tuples, sets and dictionaries.
Example: iterating over a string:
>>> name = 'Abhishek'
>>> for i in name:
>>> print(i, end=", ")
Output:
>>> A, b, h, i, s, h, e, k,

Example: iterating over a list:


>>> colors = ["Red", "Green", "Blue", "Yellow"]
>>> for x in colors:
>>> print(x)
Output:
>>> Red
>>> Green
>>> Blue
>>> Yellow
Note: Similarly, we can use loops for lists, sets and dictionaries.

range():
What if we do not want to iterate over a sequence? What if we want to use for loop for a specific number of
times?
Here, we can use the range() function.
>>> for k in range(5):
>>> print(k)
Output:
>>> 0
>>> 1
>>> 2
>>> 3
>>> 4
Note: Here, we can see that the loop starts from 0 by default and increments at each iteration.
But we can also loop over a specific range.

Example:
>>> for k in range(4,9):
>>> print(k)
Output:
>>> 4
>>> 5
>>> 6
>>> 7
>>> 8

2. while Loop
As the name suggests, while loops execute statements while the condition is True. As soon as the condition
becomes False, the interpreter comes out of the while loop.
Example:
>>> count = 5
>>> while (count > 0):
>>> print(count)
>>> count = count - 1
Output:
>>> 5
>>> 4
>>> 3
>>> 2
>>> 1
Note: Here, the count variable is set to 5 which decrements after each iteration. Depending upon the while
loop condition, we need to either increment or decrement the counter variable (the variable count, in our
case) or the loop will continue forever.

Else with While Loop


We can even use the else statement with the while loop. Essentially what the else statement does is that as
soon as the while loop condition becomes False, the interpreter comes out of the while loop and the else
statement is executed.
Example:
>>> x = 5
>>> while (x > 0):
>>> print(x)
>>> x=x-1
>>> else:
>>> print('counter is 0')
Output:
>>> 5
>>> 4
>>> 3
>>> 2
>>> 1
>>> counter is 0
3. Nested Loop
A loop within a loop is called nested Loop.
There must be following conditions for nested Loop:
1. Outer Loop and Inner Loop, Loop controlled variable must be different.
2. When inner loop is terminated then only outer loop is updated.

You might also like