“While” and “For” loop in python

Python has two types of loop, “while” and “for” loop.

While loop

It executes statement until condition is true, when condition became false it goes to the out of the loop.

>>> a = 5

>>> while a>0:

          print(“the number is”, a)


“break” statement

Use to stop execution as per given condition.

>>> a = 5

>>> while a>0:

          if a==4:


          print(“the number is”, a)


“continue” statement

It uses to return to loop again without executing the next statements.

>>> a = 5

>>> while a>0:


          if a==4:


          print(“the number is”, a)

It does not print value 4.

For loop

It uses to execute the iterative value of any sequences like – dic, list, sets, etc.

>>> name = [“naveen”, “jitendra”, “sachin”, “arvind”]

>>> for a in name:


If you pass a single string then it executes all characters of string because string contains inerrable objects.

>>> string = "codersarts"

>>> for a in string:


It displays the result c, o, d, e …

Range() in loop

It executes statements as per given range and it starts from 0 indexes, see in below:

>>> for a in range(5):


It print values from 0 to 4 not 0 to 5.

>>> range(2, 5)         #use to select range between this range.

>>> range(2, 22, 5)         #increment sequence by 5(by default 1)

Loop inside another loop(nested loop)

>>> name = [“naveen”, “jitendra”, “sachin”]

>>> name = [“etah”, “ptna”, “hrdoi”]

          for in name:

                for y in address:

                      print(x, y)       

Now it finishes in the next tutorial we will learn "python function" which is also is important

for learning the python loop concept.

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

Terms and Conditions        Privacy Policy    Pricing Policy       Refund Policy         COPYRIGHT © 2021

Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram