0% found this document useful (0 votes)
12 views

Class 11 - Presentation # 5 - Iterative Statements - for loop

The document provides an overview of Python's range() function and for loops, explaining their syntax and usage with examples. It includes various programming exercises that demonstrate how to utilize these concepts to solve problems such as printing natural numbers, even numbers, and calculating sums of series. Additionally, it covers nested loops and pattern printing using for loops.

Uploaded by

Mr.Saksham Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Class 11 - Presentation # 5 - Iterative Statements - for loop

The document provides an overview of Python's range() function and for loops, explaining their syntax and usage with examples. It includes various programming exercises that demonstrate how to utilize these concepts to solve problems such as printing natural numbers, even numbers, and calculating sums of series. Additionally, it covers nested loops and pattern printing using for loops.

Uploaded by

Mr.Saksham Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Computational Thinking

© CS-DEPT DPS MATHURA ROAD


and Programming - 1
Python - Iterative Statements
(for loop)

XI
range() function
The range() function of Python generates a list which is a special sequence type.

© CS-DEPT DPS MATHURA ROAD


A sequence in Python is a succession of values bound together by a single name.

Syntax : range (<lower limit>, <upper limit>, <step>)

The range () function will produce a list of values starting from lower limit till
upper limit - 1. By default step is 1.

For example : range(0,5)

will produce a list of values as : [0,1,2,3,4 ]

2
XI
range() function

© CS-DEPT DPS MATHURA ROAD


For example : range(5, 0)

will produce an empty list

range(12, 18)

will produce a list [ 12, 13, 14, 15, 16, 17 ]

range(27, 30)

will produce a list [ 27, 28, 29 ]

3
XI
range() function
To produce decreasing values, we can use step value in range() function as :

© CS-DEPT DPS MATHURA ROAD


range (<lower limit>, <upper limit>, <step value>)

For example : range(5, 0, -1)

will produce a list of values as : [5, 4, 3, 2, 1]

range(0, 10, 2)

will produce a list of values as : [0, 2, 4, 6, 8]

range(15, 1, -4)

will produce a list of values as : [15, 11, 7, 3]


4
XI
for loop
The for loop in Python is a counter controlled loop designed to process the items

© CS-DEPT DPS MATHURA ROAD


of any sequence, such as a list or a string one by one.

Syntax: for <variable> in <sequence >:


body of the loop

For example : Output:


for a in range (0,5) : 0-1-2-3-4-
print(a , end =”-”)

for ch in ‘calm’ : c-a-l-m-


print(ch , end =”-”)
5
XI
Using operators in and not in
The membership operator “in” returns true if value is present in the sequence

© CS-DEPT DPS MATHURA ROAD


and returns false if not present.

The membership operator “not in” returns true if value is not present in the
sequence and returns false if present.

>>> 5 in [1, 2, 3, 4, 5] >>> 5 not in [1, 2, 3, 4, 5]

True False

>>> 5 in [1, 2, 3, 4] >>> 5 not in [1, 2, 3, 4]

False True

6
XI
Example : Code to print first 10 natural numbers
print("First 10 Natural Numbers") Output :

© CS-DEPT DPS MATHURA ROAD


for i in range(1,11):
print(i) First 10 Natural Numbers
1
2
3
.
.
.
10

7
XI
Question 1
Write a program to print all even numbers from 1 to 10 and find their sum too.

© CS-DEPT DPS MATHURA ROAD


s=0 Output :
print("Even Numbers are :")
for i in range(2,11,2): Even numbers are:
print(i) 2
s=s+i 4
print("Sum of even numbers :",s) 6
8
10
Sum of even numbers :40

8
XI
Question 2
Program to print all multiples of 5 till n using for loop

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the number:")) Output :
print("Multiples of 5 are :")
for i in range(1,n+1): Enter the number: 20
if i%5==0: 5
print(i) 10
15
20

9
XI
Question 3
Program to find sum of series till n: s=1+2+3+4+5+....................n

© CS-DEPT DPS MATHURA ROAD


num1=int(input("Enter the number:")) Output :
s=0
for i in range(1,n+1): Enter the number: 10
s=s+i Sum of series :55
print("Sum of series :",s)

10
XI
Question 4
Program to find the sum of the series : s=1+x+x2+x3+.........xn

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the value of n:")) Output :
x=int(input("Enter the value of x:"))
Enter the value of n: 2
s=1
Enter value of x: 5
for i in range(1,n+1):
s=s+x**i Sum of series :30
print("Sum of series :",s)

11
XI
Programs
1. Print all the natural numbers from m to n using for loop.

© CS-DEPT DPS MATHURA ROAD


2. Input a number n and then print all natural numbers from n to 1.(in reverse
order) using for loop.
3. Input a number n and then print all the odd numbers from 1 to n along with
their sum using for loop.
4. Input a number n and then print all its factors using for loop.
5. Program to find the sum of the series : s=1+x2+x3+x4+.........+ xn
2 3 4 n
6. Program to find the sum of the series : s=1-x +x -x +.........+ xn
2 3 4

2 3 4 n

12
XI
Question 1
Write a program to print all the natural numbers from m to n.

© CS-DEPT DPS MATHURA ROAD


m=int(input("Enter the number m:")) Output :
n=int(input("Enter the number n:"))
Enter the number m: 10
print("Natural numbers are:")
Enter the number n: 16
for i in range(m,n+1): Natural numbers are:
print(i)
10
11
12
13
14
15
16
13
XI
Question 2
Write a program to print all the natural numbers from n to 1.(Reverse Order)

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the number n:")) Output :
print("Natural numbers are:")
Enter the number m: 7
for i in range(n,0,-1):
Natural numbers are:
print(i)
7
6
5
4
3
2
1

14
XI
Question 3
Write a program to input a number m and then print all the odd numbers from 1 to m

© CS-DEPT DPS MATHURA ROAD


along with their sum.
m=int(input("Enter the number m:")) Output :
print("Natural numbers are:") Enter the number m: 5
sum=0 Natural numbers are:
for i in range(m+1): 1
print(m) 3
sum=sum+m 5
print("Sum :",sum) Sum : 9

15
XI
Question 4
Write a program to input a number m and then print the sum of all its

© CS-DEPT DPS MATHURA ROAD


factors.
m=int(input("Enter the number m:")) Output :
print("Factors are:") Enter the number m: 15
sum=0 Factors are:
for i in range(1,m+1):
1
if m%i==0: 3
print(i) 5
sum+=i
15
print("The sum of the factors is :", sum)
The sum of the factors is
28

16
XI
Question 5
Program to find the sum of the series : s=1+x2+x3+x4+.........+ xn

© CS-DEPT DPS MATHURA ROAD


2 3 4 n
n=int(input("Enter the number n:")) Output :
x=int(input("Enter the number x:")) Enter the number n:5
s=1 Enter the number x:2
for i in range(2,n+1): Sum of series :
term=(x**i)/i 16.066666666666666
s=s+term
print("Sum of series :",s)

17
XI
Question 6
Program to find the sum of the series : s=1-x2+x3-x4+.........+ xn

© CS-DEPT DPS MATHURA ROAD


2 3 4 n
n=int(input("Enter the number n:")) Output :
x=int(input("Enter the number x:"))
Enter the number n: 4
sum=1 Enter the number x: 2
sign=-1
for i in range(2,n+1):
Sum of series
term=sign*(x**i)/i :-2.3333333
sum=sum+term
sign=sign*-1
print("Sum of series :",sum)

18
XI
Nested for loop
Nested loop is a loop inside another loop.

© CS-DEPT DPS MATHURA ROAD


Example :

n=int(input('Enter the no of lines:')) Output :


for i in range(1,n+1):
Enter the no of lines:5
for j in range(1,i+1): 1
print(j,end='')
print( ) 12
123
1234
12345

19
XI
Nested for loop
Example :

© CS-DEPT DPS MATHURA ROAD


n=int(input('Enter the no of lines:')) Output :
for i in range(1,n+1):
Enter the no of lines:5
for j in range(1,i+1): A
print(chr(64+j),end='')
print( ) AB
ABC
ABCD
ABCDE

20
XI
Nested for loop
Example :

© CS-DEPT DPS MATHURA ROAD


n=int(input('Enter the no of lines:')) Output :
for i in range(1,n+1): Enter the number of rows: 5
for j in range(1,i+1):
print('*',end='') *
* *
print( ) * * *
* * * *
* * * * *

21
XI
Question 5
Program to find the sum of the series : s=1+x2+x3+x4+.........+ xn

© CS-DEPT DPS MATHURA ROAD


2! 3! 4! n!
n=int(input("Enter the value of n : ")) Output :
x=int(input("Enter the value of x : "))
s=1 Enter the value of n :4
for i in range(2,n+1): Enter the value of x :2
fact=1 Sum of series : 5.0
for j in range(1, i+1):
fact=fact*j
term=x**i/fact
s=s+term
print("Sum of series : ",s)

22
XI
Question 6
Program to find the sum of the series : s=1+x2+x4+x6+.........+ x2n

© CS-DEPT DPS MATHURA ROAD


1! 2! 3! n!
n=int(input("Enter the value of n : ")) Output :
x=int(input("Enter the value of x : "))
s=1
for i in range(2,2*n+1,2): Enter the value of n :2
fact=1 Enter the value of x :3
for j in range(1, i//2+1): Sum of series : 50.5
fact=fact*j
term=x**i/fact
s=s+term
print("Sum of series : ",s)

23
XI
Program to print patterns using for loop
a) A c) 1

© CS-DEPT DPS MATHURA ROAD


BB 12
CCC 123
DDDD 1234
EEEEE
b) A d) *
BC * *
* * *
DEF * * * *
GHIJ * * * * *
* * * * *
KLMNO * * * *
* * *
* *
*
24
XI
Question 1

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the value of n : ")) Output :
for i in range(1,n+1):
ch=’A’ Enter the value of n : 5
for j in range(1,i+1):
print(ch,end="")
A
ch=chr(ch+1) BB
print()
CCC
DDDD
EEEEE

25
XI
Question 2

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the value of n : ")) Output :
num=65
for i in range(1,n+1): Enter the value of n : 5
for j in range(1,i+1):
print(chr(num),end="")
A
num=num+1 BC
print()
DEF
GHIJ
KLMNO

26
XI
Question 3
rows =int(input("Enter the value of n:")) Output :

© CS-DEPT DPS MATHURA ROAD


for i in range(1, rows+1):
num = 1 Enter the value of n: 5
for j in range(rows, 0, -1): 1
if j > i: 12
print(" ", end=' ')
else: 123
print(num, end=' ') 1234
num += 1 12345
print("")

27
XI
Question 4
rows =int(input("Enter the value of n:")) Output :

© CS-DEPT DPS MATHURA ROAD


for i in range(1, rows+1):
num = 1 Enter the value of n:5
for j in range(rows, 0, -1): 1
if j > i: 1 2
print(" ", end=' ')
else: 12 3
print(num, end=' ') 1234
num += 1
print("")
12345

28
XI
Programs
Write programs based on for loops :

© CS-DEPT DPS MATHURA ROAD


1. 1+x+x2+x3+x4+............................xn
2. 1-x+x2-x3+x4+............................xn
3. x+x2+x3+x4+............................xn
4. x+x2-x3+x4-x5............................xn
5. 12+22+32+42+...................................n2

29
XI
Question 1
Program to input a number n and then print the factorial of all numbers from 1 till n.

© CS-DEPT DPS MATHURA ROAD


For example : if n =5 then program should display

1! = 1

2!=2

3!=6

4!=24

5!= 120

30
XI
Solution
n=int(input("Enter the value of n:")) Output :

© CS-DEPT DPS MATHURA ROAD


for i in range(1,n+1): Enter the value of n:5
fact=1 1 != 1
for j in range(1,i+1): 2 != 2
3 != 6
fact=fact*j
4 != 24
print(i,"!=",fact) 5 != 120

31
XI
Question 2
Program to input numbers m and n and then print the all prime numbers between m

© CS-DEPT DPS MATHURA ROAD


to n.

For example : if m=1 and n =10 then program should display

Prime numbers from 1 to 10 are: 2,3,5,7

32
XI
Solution
m=int(input("Enter the value of m:")) Output :

© CS-DEPT DPS MATHURA ROAD


n=int(input("Enter the value of n:")) Enter the value of n:10
print("Prime numbers are:") Enter the value of n:20
for i in range(m,n+1): Prime numbers are:
11
flag=0
13
for j in range(2,i): 17
if (i%j==0): 19
flag=1
break
if flag==0:
print(i)

33
XI
Find the output :
a=4

© CS-DEPT DPS MATHURA ROAD


b=0
c=0
for b in range(a,a*10,a):
c+=b
print(a,b,c)

34
XI
Solution :
a=4 4 44

© CS-DEPT DPS MATHURA ROAD


b=0 4 8 12
for b in range(a,a*10,a): 4 12 24
c+=b 4 16 40
print(a,b,c) 4 20 60
4 24 84
4 28 112
4 32 144
4 36 180

35
XI
Find the output :
a=5

© CS-DEPT DPS MATHURA ROAD


b=10
for b in range(1,3):
print("Line1",a,"&",b-2)
a=a+1
b=b+1
print("Line2",b,"&",a+3)

36
XI
Solution :
a=5 Line1 5 & -1

© CS-DEPT DPS MATHURA ROAD


b=10 Line2 2 & 9
for b in range(1,3): Line1 6 & 0
print("Line1",a,"&",b-2) Line2 3 & 10
a=a+1
b=b+1
print("Line2",b,"&",a+3)

37
XI
Find the output :
x=0

© CS-DEPT DPS MATHURA ROAD


for j in range(2):
for i in range(-2,-5,-1):
x+=1
print(x)

38
XI
Solution :
x=0 1

© CS-DEPT DPS MATHURA ROAD


for j in range(2): 2
for i in range(-2,-5,-1): 3
x+=1 4
print(x) 5
6

39
XI
Find the output :
for num in range(10,14):

© CS-DEPT DPS MATHURA ROAD


for i in range(2,num):
if num%i!=0:
print(num,"-",i)
break

40
XI
Solution :
for num in range(10,14): 10 - 3

© CS-DEPT DPS MATHURA ROAD


for i in range(2,num): 11 - 2
if num%i!=0: 12 - 5
print(num,"-",i) 13 - 2
break

41
XI
Find the output :
Sum=0

© CS-DEPT DPS MATHURA ROAD


for I in range(1,10,2):
Sum+=I
if I==9:
print(I,end="=")
else:
print(I,end="+")
print(Sum)

42
XI
Find the output :
Sum=0 1+3+5+7+9=25

© CS-DEPT DPS MATHURA ROAD


for I in range(1,10,2):
Sum+=I
if I==9:
print(I,end="=")
else:
print(I,end="+")
print(Sum)

43
XI
Find the output :
m=1000

© CS-DEPT DPS MATHURA ROAD


for I in range(5):
print(I+1,m,sep="#",end=",")
m-=100*(I+1)

44
XI
Find the output :
m=1000 1#1000,2#900,3#700,4#400,5#0,

© CS-DEPT DPS MATHURA ROAD


for I in range(5):
print(I+1,m,sep="#",end=",")
m-=100*(I+1)

45
XI
Find the output :
for I in range(5):

© CS-DEPT DPS MATHURA ROAD


A=2*I
B=8-2*I
print(A,B,sep="*",end="=")
print(A*B)

46
XI
Find the output :
for I in range(5): 0*8=0

© CS-DEPT DPS MATHURA ROAD


A=2*I
B=8-2*I 2*6=12
print(A,B,sep="*",end="=") 4*4=16
print(A*B) 6*2=12
8*0=0

47
XI
Find the errors in the following code:
30=To

© CS-DEPT DPS MATHURA ROAD


for k in range(0,To)
IF K%4==0:
print(k*4)
Else:
print(k+3)

48
XI
Solution:
Wrong
30=To

© CS-DEPT DPS MATHURA ROAD


assignment
for k in range(0,To) No colon at
Wrong IF the end
IF K%4==0:
statement
print(k*4)
Wrong Else Else:
statement
print(k+3)

49
XI
Find the errors in the following code:
val=int(input(“Value”)

© CS-DEPT DPS MATHURA ROAD


Adder=0
for c in range(1,val,3):
Adder+=c
If c%2=0:
Print(c*10)
Else:
print(c*)
print(Adder)
50
XI
Find the errors in the following code:
val=int(input(“Value”))

© CS-DEPT DPS MATHURA ROAD


Wrong input()
Adder=0 function
for c in range(1,val,3)
No colon at the end
Wrong if Adder+=c of for statement
statement and If c%2=0:
operator =
used here Print(c*10) Wrong “Print()”
function
Wrong else Else:
statement print(c*) One more operand
required
print(Adder)
51
XI
Rewrite the code using for loop:
num=10

© CS-DEPT DPS MATHURA ROAD


sum=0
count=0
while num > 0 :
count += 1
sum += num
num –= 2
if count == 10 :
print (sum/float(count))
break
52
XI
Rewrite the code using for loop:
num=10 num=10

© CS-DEPT DPS MATHURA ROAD


sum=0 sum=0
count=0 count=0
while num > 0 :
for i in range(num,0,-2):
count += 1
count+=1
sum += num
sum+=i
num –= 2
if count==10:
if count == 10 :
print(sum/float(count))
print (sum/float(count))
break
break
53
XI
Rewrite the code using for loop:
i = 100

© CS-DEPT DPS MATHURA ROAD


while (i > 0) :

print (i)

i -= 3

54
XI
Rewrite the code using for loop:
i = 100 for i in range(100, 0, -3) :

© CS-DEPT DPS MATHURA ROAD


while (i > 0) : print (i)

print (i)

i -= 3

55
XI
Rewrite the code using while loop:
for i in range(4) :

© CS-DEPT DPS MATHURA ROAD


for j in range(5):
if i + 1 == j or j + 1 == 4 :
print ("+", end = ' ')
else :
print ("o", end = ' ')
print()

56
XI
Rewrite the code using while loop:
for i in range(4) : i=0

© CS-DEPT DPS MATHURA ROAD


for j in range(5): while i < 4:
j=0
if i + 1 == j or j + 1 == 4 :
while j < 5:
print ("+", end = ' ')
if i + 1 == j or j + 1 == 4 :
else :
print ("+", end = ' ')
print ("o", end = ' ') else :
print() print ("o", end = ' ')
j += 1
i += 1
print()

57
XI
Rewrite the code using while loop:
num=5

© CS-DEPT DPS MATHURA ROAD


min = 0
max = num
if num < 0 :
min = num
max = 0
for i in range(min, max + 1):
sum += i

58
XI
Rewrite the code using while loop:
num=5 num=5

© CS-DEPT DPS MATHURA ROAD


min = 0 min = 0
max = num
max = num
if num < 0 :
if num < 0 :
min = num
min = num
max = 0
max = 0 i = min
for i in range(min, max + 1): while i <= max:
sum += i sum += i
i += 1

59
XI
© CS-DEPT DPS MATHURA ROAD
Happy Learning
Thank you!!!

60
XI

You might also like