“While” and “For” loop in python
>>> a = 5
>>> while a>0:
print(“the number is”, a)
a--
“break” statement
Use to stop execution as per given condition.
>>> a = 5
>>> while a>0:
if a==4:
break
print(“the number is”, a)
a--
“continue” statement
It uses to return to loop again without executing the next statements.
>>> a = 5
>>> while a>0:
a--
if a==4:
continue
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:
print(a)
If you pass a single string then it executes all characters of string because string contains inerrable objects.
>>> string = "codersarts"
>>> for a in string:
print(a)
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):
print(a)
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 x 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.