Python loop

In python programming loop or iteration are used to repeat execution of single or set of statements. This iteration has some end point that is called loop condition. Loop will only execute until the provied condition will remain true. This condtion is basically composed using relation or logical operators. Similar to other high level programming languages python also provide two types of iterative control structures for implementation of loop construct.

Types of loops in python

There are two types of loop or looping statements in python programming language.

  • while loop
  • for loop

Python while:

While loop is the simplest iterative structure for repeating task in python. Particular condition is provided in while loop that is the part of syntax. Loop check condition after every execution of statements. When the result of condition will FALSE loop will be terminate and control transfer to the next instruction.

Syntax:

while condition / relational / logical expression :

     statements

     increment / decrement (if used counter variable)

Flowchart:

Python while loop syntax and flow chartExample:

The given example use python while loop for printing counting 1 to 10

# python script to print
# counting from 1 to 10
# using while loop

i =1
while i <= 10:
    print(i) # output statement
    i = i +1 # increment

Output:

1
2
3
4
5
6
7
8
9
10

Python for loop

for statement python is another loop construct that is more flexible and usable than while loop. This loop can be used with range function to define no of iterations instead of relational expression / condition. This loop can also be used to iterate over list or dictionary. When we use for loop statement with range function then there is no need of increment or decrement statement in body of loop.

for loop syntax in python

for loop_variable in range / list :

     statement

Where loop variable is used to assign value from range or list, further in body of for loop this loop variable is processed. in is keyword in python for loop statement.

Python for i in:

Python for i in is the most commonly used syntax form of for loop. According to for loop syntax here i is the loop variable, which will hold the value from given data set. This data set may be range, dictionary or lis. This python loop through list and print its values.

Example:

# python script to print
# data from list
# using for loop

a = ["banana","apple","graphs"]
for i in a:
    print(i) # output statement

Output:

banana
apple
graphs

Python for loop range

Python for loop with range is used for predefined no of iterations. This also used in keyword and loop variable for iterations.

Example:

# python script to print
# counting from 0 to 4
# using for in range syntax

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Python while else:

Python while also has an optional else part. This else will execute its statemens only if loop completed its iterations successfully. Means loop condition become false and then loop terminate.If we exit from loop using break or return statement then else part of while loop will not execute.

Example while else:

# python while loop
# example with else part

i = 1
while i <= 5:
    print(i)
    i = i + 1
else:
    print("While Loop End")

Output:

1
2
3
4
5
Loop End

Nested for loop python:

for loop statement having another for loop in its body is called nested for loop. Nested loop structure is used to process two / multi dimension data sets. Nested loop are also helpful in complex calculations. One is called inner and other is called outer loop. In nested loop total no of iterations are the multiplication of total iterations of each loop.

Example:

# python nested for loop
# to process two dimensional
# data list

a = [["Garlic","Onion","Potato"],
     ["Banana","Mango","Apple"]]

for i in a: # outer loop
    for j in i: #inner loop
        print(j)

Output:

Garlic
Onion
Potato
Banana
Mango
Apple
 
Comments
Login to TRACK of Comments.