Loops in python

                                                 Loops  in python                   


 One of the main components of the control flow in any language is loops.

Like any other language, Python provides two main looping techniques which are 


While Loop:-

For Loop:-


Uses of Loops


* Using loops provide a shortcut to minimize code.

* Increase the readability of code.

* Reduce complexity.

* In loops, a group of instructions tends to repeat itself till the termination condition.

* The loops provide the opportunity of bringing similar executable instructions together, This technique makes the code reusable for similar tasks.


Advantages of Loops in Python


1. Loops play a vital role in the simplification of complex problems.

2. Loops save the developers from manually writing and executing even the most straightforward and shortest instructions repeatedly.

3. Loops help increase the reusability of code.

4. These provide easy traversal for the elements like arrays and linked lists.


*While Loop


With the while loop we can execute a set of statements as long as a condition is True.The utilization of the concept of while loop is the most helpful in cases with no clarity of the iteration numbers.


Syntax -:


while expression:

    execute block of code


Example -:


i=0


while (i<=3):

    print(i)

    i=i+1


Output -:


0

1

2

3

>>> 

Explanation -:


This example meant to increment the value of " i " and print numbers which is less than and equal to 3.


Example of String with Else statement -:


i=0


while (i<=3):

    print("Hello Python")

    i=i+1

else:

    print("Loop End")


Output -:



Hello Python

Hello Python

Hello Python

Hello Python

Loop End

>>> 


Explanation -:


This example meant to increment the value of " i " and print "Hello Python" as long as the value of i is less than and equal to 3. In the addition we have output of the Else statement i.e. "Loop End".


The Break Statement -:


In break statement we can stop the loop even if the while condition is True.


Example -: 


i = 5

while i > 0:

    i = i - 1

    if i == 2:

               break

    print(i)


print("break")


Output -:


4

3

break

>>> 


Explanation -:


The loop keeps running until the desired condition is satisfied therefore the loops is terminated and jumps to the next print statement.


The Continue Statement -: 


In the continue statement we can stop the current iteration, and continue with the next.


Example -: 


i = 5

while i > 0:

    i = i - 1

    if i == 2:

               continue

    print(i)


print("continue")


Output -:


4

3

1

0

continue

>>> 


Explanation -:


The loop keeps running and printing the values of “i” until the condition is satisfied and the continue statement is inserted, the loop will skip this iteration and continue with the next when until the loop statement is no longer true.


The Pass Statement -:


Pass statement can do nothing.


Example -: 


i = 5

while i > 0:

    i = i - 1

    if i == 2:

               pass

    print(i)

 

print("pass")


Output -:


4

3

2

1

0

pass

>>> 


Explanation -:


The loop will act normally as we inserted the pass statement after the condition. It’s counted here as a placeholder for further changes in the code.


For Loop -:


A for loop is used for iterating over a sequential input (that is either a list, a tuple, a dictionary, a set, or a string).

It is beneficial to combine a set of instructions and execute them till all the conditions completely satisfy. 

In the for loop we can execute a set of statements, Once for each item in a list, tuple, set etc.


Syntax -:


for expression:

    execute block of code


Example -: 


for i in range (1,5):

    print (i)


Output -:


1

2

3

4

>>> 


Explanation -:


As we can see the iterator keeps iterating in the range of 5 till it’s no longer within the range.


Example of String with Else statement -:


x = ["Thane", "Mumbai", "Delhi"]

 

for i in x:

     print(i)


else:

    print ("Loop End")


Output -:


Thane

Mumbai

Delhi

Loop End

>>> 


Explanation -:


As we define a list of strings we need to print each string. So we provided the iterator which will iterate through the size of the list and print each value of it. In the addition we have output of the Else statement i.e. "Loop End".


The Break Statement -:


In the break statement we can stop the loop before it has looped through all the items.


Example -: 


Colors = ["Red", "Yellow", "Blue", "Green"]

for x in Colors:

  if x == "Blue":

    break

  print(x)



Output -:


Red

Yellow

>>> 


Explanation -:



As we can see our iterator x is iterating in range of Colors list and keep printing the strings till the if the condition is satisfied therefore the break statement executes and the loop is terminated.


The Continue Statement -: 


In the continue statement we can stop the current iteration, and continue with the next.


Example -: 



Colors = ["Red", "Yellow", "Blue", "Green"]

for x in Colors:

  if x == "Blue":

    continue

  print(x)


Output -:


Red

Yellow

Green

>>> 



Explanation -:  


As we can see our iterator is iterating in a range of Colors list and keep printing the strings till the IF condition is satisfied therefore it skips the current iteration which is supposed to print out “Blue” and continue printing the following strings. 


The Pass Statement -:


Pass statement can do nothing.


Example -: 



Colors = ["Red", "Yellow", "Blue", "Green"]

for x in Colors:

  if x == "Blue":

    pass

  print(x)


Output -:


Red

Yellow

Blue

Green

>>> 


Explanation -:  


As we can see the whole list is presented in the output as the condition inserted within the loop is followed by a pass statement.




Comments

Popular posts from this blog