Python has two primitive loop commands.
For Loop
While Loop
## syntax for - for loop
for i in range (value):
(code)
## syntax for - while loop
while (condition):
(code)
Loops are important in Python or in any other programming language as they help you to execute a block of code repeatedly.
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.
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.
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.
## looping through a string
a = "rguktweb"
for i in a:
print (i)
## looping through a list
b = [12 ,5 ,4 ,20 ]
for j in b:
print (j)
## looping through a tuple
c = ("hello" ,"world" ,"rguktweb" )
for k in c:
print (k)
## looping through a dictionary
d = {1 :"hello" ,2 :"world" }
for m in d:
print (m,d[m])
Python range() is a built-in function that is used when a user needs to perform an action a specific number of times.
range() in Python(3.x) is just a renamed version of a function called xrange() in Python(2.x).
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.
The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6).
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3).
for i in range (10 ):
print (i,end ="" )
for j in range (1 ,10 ):
print (j,end ="" )
With the break statement we can stop the loop before it has looped through all the items.
for i in range (23 ):
print (i)
if i == 10 :
break
With the continue statement we can stop the current iteration of the loop, and continue with the next.
for i in range (23 ):
if i == 10 :
continue
print (i)
for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.
for i in range (23 ):
pass
The else keyword in a for loop specifies a block of code to be executed when the loop is finished.
If the loop breaks, the else block is not executed.
for i in range (23 ):
print (i)
else :
print ("loop completed." )
for i in range (23 ):
print (i)
if i == 10 :
break
else :
print ("loop broken." )
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop".
for i in range (5 ):
for j in range (i ):
print (i)