Loops
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.
While Loop
# Python example for while loop
count = 0
while count < 3:
count = count + 1
print("Hello World")
For Loop
s1 = ["hello", "python", "world"]
s2 = "hello"
# using for loop with string
for i in s1:
print(i)
# loop through letters in a string
for i in s2:
print(i)
for x in range(5):
print(x)
Break
Break is used to break out of a loop
for i in range(10):
if i == 5:
break
else:
print(i)
Continue
Continue jumps to the next iteration for the loop
for i in range(3,10):
if i == 5:
continue
else:
print(i)
Practise Programs
- input a number and print factorial
- print 1 to 10 numbers except 5 using continue and break the loop at 7
- print fibonacci series until 7 posittion