Python for
Lecture-5
Kids
Control Statements
(LOOPING)
Agenda
• Looping Statement
• While
• For
• Nested Loops
• Examples
While statement
• Syntax
while condition:
Statements
Example
• Program to print numbers from 1 to 10. 1
2
a=1 3
4
while a<=10: 5
6
print(a) 7
a+=1 8
9
10
Example
• Program to print numbers from 1 to 10.
a=1
while a<=10:
print(a, end=“ “)
1 2 3 4 5 6 7 8 9 10
a+=1
Example
• Program to print numbers from 10 to 1.
a = 10
while a>=1:
print(a, end=“ “)
10 9 8 7 6 5 4 3 2 1
a-=1
Example
• What is the output of this program??
a=1
ATS
while a<=5:
ATS
print(“ATS”) ATS
ATS
a+=1 ATS
Example
• What is the output of this program??
a=2
2 4 6 8 10 …… 100
while a<=100:
print(a, end=“ “)
a+=2
• Write a program to input a number and print its reverse number.
a = int(input(“Enter any number”))
Enter any number : 234
r=0 a = 234
while a!=0: b = 234%10 = 4
b=a%10 r = 0*10+4 = 4
a = 234//10 = 23
r=r*10+b
b = 23%10 = 3
a=a//10
r = 4*10+3 = 43
print(“ Reverse = “, r)
a = 23//10 = 2
b = 2%10 = 2
r = 43*10+2 = 432
a = 2//10 = 0
for statement
• Syntax
for variable in range:
Statements
Example
• Program to print numbers from 0 to 10.
for a in range(11):
print(a, end=‘ ‘)
0 1 2 3 4 5 6 7 8 9 10
Example
• Program to print numbers from 1 to 10.
for a in range(1,11):
print(a, end=‘ ‘)
1 2 3 4 5 6 7 8 9 10
Example
• Program to print even numbers from 1 to 100.
for a in range(2, 101, 2):
print(a, end=‘ ‘)
2 4 6 8 10 . . . . . . . 100
Example
• Program to print numbers from 10 to 1.
for a in range(10, 0, -1):
print(a, end=‘ ‘)
10 9 8 7 6 5 4 3 2 1
Nested Loop
a=1
while a<=3: 1111
b=1 2222
while b<=4: 3333
print(a, end=“ “)
b+=1
print()
a+=1
Nested Loop
a=1
while a<=3: 1234
b=1 1234
while b<=4: 1234
print(b, end=“ “)
b+=1
print()
a+=1
Nested Loop
a=1
1
while a<=4:
12
b=1 123
while b<=a: 1234
print(b, end=“ “)
b+=1
print()
a+=1
Nested Loop
for a in range(1,4): 1111
for b in range(1,5): 2222
print(a, end=“ “) 3333
print()
Nested Loop
for a in range(1,4): 1234
for b in range(1,5): 1234
print(b, end=“ “) 1234
print()
Nested Loop
1
for a in range(1,5):
12
for b in range(1,a+1): 123
print(b, end=“ “) 1234
print()
Assignment-5
1. Print numbers from 0 to 20 & also 20 to 0.
2. Print all even numbers from 1 to 100.
3. Input a no & display sum of all natural numbers upto that number.
4. Input a no & display factorial of this number.
5. Input a no & display table of this no.
6. Sum=1+3+5+7+9+…………n;
7. Sum=12+22+32+42+………..n;
8. Display Fibonacci series : 1,1,2,3,5,8,13……..n;
9. Display all hundreds from 1 to 10000.
10. 100 to 1 print all even numbers.
11. Input a no & display its reverse no.
12. Input a no & display no of digit.
13. Input a no & check it is duck no or not.
14. Write a program to input a number and check for Armstrong number.
15. Input a no & display the sum of digit for Example input is 123 & the output is 6.
16. Input a no & display all digits in vertical order .
17. Input a no & display in words for example if number:123, output: ONE TWO THREE.
Assignment-5