4. The for loop in python is
used to iterate
statements or part
the
of
the program several
times.
used
It
to
is frequently
traverse the
data structures like list,
tuple, or dictionary.
For Loop
7. Write a
program to
print 1 to 10
series
Example
i=1
For i in range(1,11):
Print(i)
Output
1 2 3 4 5 6 7 8 9 10
8. To print
multiplication table
Output
enter no 6
6 X 0 = 0
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60
i=0
n=int(input("enter no"))
for i in range(0,11):
print("%d X %d = %d" %
(n,i,n*i))
10. Repeats a statement or
group of statements while
a given condition is TRUE. It
tests the condition before
executing the loop body.
While loop syntax
while (
test_expression):
Body of while
13. Example
a = ['student1', 'student2',
'student3']
while a:
item = a.pop(-1)
print(item)
Output
student3
student2
Student1
Program to print list in reverse order
14. n=10
while n > 0:
n -= 1;
if True:
print(n)
Output
Example
Using if Statement with While Loop
10
9
8
7
6
5
4
3
2
1
15. Python supports to have
an else statement associated with a
loop statement.
If
a
the else statement is used with
while loop, the else statement is
executed when the condition becomes
false.
Using else Statement with While Loop