Class 11 - Presentation # 5 - Iterative Statements - for loop
Class 11 - Presentation # 5 - Iterative Statements - for loop
XI
range() function
The range() function of Python generates a list which is a special sequence type.
The range () function will produce a list of values starting from lower limit till
upper limit - 1. By default step is 1.
2
XI
range() function
range(12, 18)
range(27, 30)
3
XI
range() function
To produce decreasing values, we can use step value in range() function as :
range(0, 10, 2)
range(15, 1, -4)
The membership operator “not in” returns true if value is not present in the
sequence and returns false if present.
True False
False True
6
XI
Example : Code to print first 10 natural numbers
print("First 10 Natural Numbers") Output :
7
XI
Question 1
Write a program to print all even numbers from 1 to 10 and find their sum too.
8
XI
Question 2
Program to print all multiples of 5 till n using for loop
9
XI
Question 3
Program to find sum of series till n: s=1+2+3+4+5+....................n
10
XI
Question 4
Program to find the sum of the series : s=1+x+x2+x3+.........xn
11
XI
Programs
1. Print all the natural numbers from m to n using for loop.
2 3 4 n
12
XI
Question 1
Write a program to print all the natural numbers from m to n.
14
XI
Question 3
Write a program to input a number m and then print all the odd numbers from 1 to m
15
XI
Question 4
Write a program to input a number m and then print the sum of all its
16
XI
Question 5
Program to find the sum of the series : s=1+x2+x3+x4+.........+ xn
17
XI
Question 6
Program to find the sum of the series : s=1-x2+x3-x4+.........+ xn
18
XI
Nested for loop
Nested loop is a loop inside another loop.
19
XI
Nested for loop
Example :
20
XI
Nested for loop
Example :
21
XI
Question 5
Program to find the sum of the series : s=1+x2+x3+x4+.........+ xn
22
XI
Question 6
Program to find the sum of the series : s=1+x2+x4+x6+.........+ x2n
23
XI
Program to print patterns using for loop
a) A c) 1
25
XI
Question 2
26
XI
Question 3
rows =int(input("Enter the value of n:")) Output :
27
XI
Question 4
rows =int(input("Enter the value of n:")) Output :
28
XI
Programs
Write programs based on for loops :
29
XI
Question 1
Program to input a number n and then print the factorial of all numbers from 1 till n.
1! = 1
2!=2
3!=6
4!=24
5!= 120
30
XI
Solution
n=int(input("Enter the value of n:")) Output :
31
XI
Question 2
Program to input numbers m and n and then print the all prime numbers between m
32
XI
Solution
m=int(input("Enter the value of m:")) Output :
33
XI
Find the output :
a=4
34
XI
Solution :
a=4 4 44
35
XI
Find the output :
a=5
36
XI
Solution :
a=5 Line1 5 & -1
37
XI
Find the output :
x=0
38
XI
Solution :
x=0 1
39
XI
Find the output :
for num in range(10,14):
40
XI
Solution :
for num in range(10,14): 10 - 3
41
XI
Find the output :
Sum=0
42
XI
Find the output :
Sum=0 1+3+5+7+9=25
43
XI
Find the output :
m=1000
44
XI
Find the output :
m=1000 1#1000,2#900,3#700,4#400,5#0,
45
XI
Find the output :
for I in range(5):
46
XI
Find the output :
for I in range(5): 0*8=0
47
XI
Find the errors in the following code:
30=To
48
XI
Solution:
Wrong
30=To
49
XI
Find the errors in the following code:
val=int(input(“Value”)
print (i)
i -= 3
54
XI
Rewrite the code using for loop:
i = 100 for i in range(100, 0, -3) :
print (i)
i -= 3
55
XI
Rewrite the code using while loop:
for i in range(4) :
56
XI
Rewrite the code using while loop:
for i in range(4) : i=0
57
XI
Rewrite the code using while loop:
num=5
58
XI
Rewrite the code using while loop:
num=5 num=5
59
XI
© CS-DEPT DPS MATHURA ROAD
Happy Learning
Thank you!!!
60
XI