0% found this document useful (0 votes)
20 views74 pages

Loop

Uploaded by

shrinithibuzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views74 pages

Loop

Uploaded by

shrinithibuzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

Loop – repetition of

task

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

What is
Loop?
⚫ As we have already studied there are
many situation where we need to
repeat same set of tasks again and
again. Like while writing table of any
number we multiply same number
from 1 to 10, multiplying number
from n to 1 to find the factorial, In
real life, to prepare parathas, suppose
to prepare your bag with all items,
daily coming to school again and
again, etc. Please explore more real
life conditions where repetition of
VINOD KUMAR VERMA, PGT(CS), KV OEF
task was done by you.
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Pseudocode
Reference
A. start
B. Input number(n) to print
table
C. Let count=1
D. Display n * count
E. Add 1 to count
F. if count==10:
A. Stop
G. else:
A. Repeat from Step C
H. Stop VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Python Loop
Statements
⚫ To carry out repetition of
statements Python provide 2
loop statements
◦ Counting loop (for)
◦ Conditional loop (while)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

range()
function
⚫ Before weproceed to for loop let
us understand range() function
which we will use in for loop to
repeat the statement to n
number of times.
⚫ Syntax:
◦ range(lower_limit, upper_limit)
⚫ The range function generate set of
values from lower_limit to
upper_limit-1 VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

range()
function
⚫ For e.g.
⚫ range(1,10) will generate
set of values from 1-9
⚫ range(0,7) will generate [0-6]
⚫ Default step value will be +1 i.e.
⚫ range(1,10) means
(1,2,3,4,5,6,7,8,9)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

range()
function
⚫ To change the step value we can
use third parameter in range()
which is step value
⚫ For e.g.
⚫ range(1,10,2) now this will
generate
value [1,3,5,7,9]
⚫ Step value can be in –ve also to
generate set of numbers in reverse
order. VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

range()
function
⚫ To create list from 0 we
can use generat
⚫ range(10) it will e
[0,1,2,3,4,5,6,7,8,9]
consider the following statement and
predict the values generate:
(i)range(10)
(ii)range(5,10,2)
(iii) range(10,1,-3)
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Operators in and
not
⚫ The in
operator in and not in is
used in for loop to check whether
the value is in the range / list or
not
⚫ For e.g.
>>> 5 in [1,2,3,4,5]
True
>>> 5 in [1,2,3,4]
False
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

>>>‟a‟ in
"apple‟
True
>>>‟national‟ in
"international‟
True

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:

Program to check whether


www.python4csip.com

any word is a part of


sentence or not any
line = input("Enter
statement") word =
input("Enter any word")
if word in line:
print("Yes ", word, "is a part of
",line) else:
print("No", word ," is not
part of ",line)
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

for
loop
⚫ for loop in python is used to
create a loop to process items of
any sequence like List,Tuple,
Dictionary, String
⚫ It can also be used to create loop of
fixed number of steps like 5 times,
10 times, n times etc using
range() function.

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example – for loop with


List
School=["Principal","PGT","TGT","
PRT"] for sm in School:
print(sm)

Example – for loop


with Tuple
Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
LIST - COMPREHENSION
(i) L=[i for i in range(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(L)

(ii) L=[i for i in range(10) if i%2==0] [0, 2, 4, 6, 8]


print(L)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us understand how for loop


works
Code=(10,20,30,40,50,
60)
for cd in Code:
print(cd)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

First it will create Tuple


"Code‟
Code=(10,20,30,40,50,
60)
for cd in Code:
print(cd)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Then for loop starts, it will pick
values one by one from Code and
assign it to cd
Code=(10,20,30,40,50,
60)
for cd in Code:
print(cd)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Then for loop starts, it will pick
values one by one from Code and
assign it to cd
Code=(10,20,30,40,5
0,60) cd =
for cd in Code: 10
print(cd)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Then for loop starts, it will pick
values one by one from Code and
assign it to cd
Code=(10,20,30,40,5
0,60) cd =
for cd in Code: 10
print(cd)
OUTPUT

10

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Then for loop starts, it will pick
values one by one from Code and
assign it to cd
Code=(10,20,30,40,5
0,60) cd =
for cd in Code: 20
print(cd)
OUTPUT

10

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Then for loop starts, it will pick
values one by one from Code and
assign it to cd
Code=(10,20,30,40,5
0,60) cd =
for cd in Code: 20
print(cd)
OUTPUT

10 FOR LOOP WILL


20 AUTOMATICALLY ENDS
WHEN LAST ITEM OF
AND SO O N … LIST IS PROCESSED

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

for loop with


string
for ch in " Plan":
print(ch)

The above loop product


output P
l
a

n VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

for with
range()
Let us create a loop to print all the natural
number from 1 to 100

for i in
range(1,10
1):
print(i,end=
'\t')

** here end=„\t‟ will cause output to


appear withoutVINOD
changing line
KUMAR VERMA, and
PGT(CS), give
KV OEF
one tab space between KANPUR
next& output.
SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Program to print table of any


number
num = int(input("Enter any
number ")) for i in range(1,11):
print(num,'x',i,'=',num*i)

Program to find the sum of all


number divisible by 7 between 1
to 100
sum=0
for i in
range(1,101)
: if i % 7
== 0:
VINOD KUMAR VERMA, PGT(CS), KV OEF
sum+=i KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
Fill up the blanks in the following program
according to the given output:
for i in range(1,___,___)_
print(i,end=____)
Output: 1 3 5 7 9 11 13 15 17 19

for i in range(1,21,2):
print(i,end=’ ‘)
Consider the loop given below and predict the final value of i after
this loop:
for i in range(7, 4, -2): OUTPUT:
break 7

Rewrite the following code fragment using while loop:


for i in range(1,16):
if i %3==0:
print(i,end=’ ‘) OUTPUT:
i=1
while i<16:
if i%3==0:
print(i, end=' ')
i+=1
Give the step by step working of the following loop:
for a in [1, 4, 7]:
print(a) a=1
print(a*a) all statements are executed with a value 1
print(1)  1
print(1*1)  1
a=4
all statements are executed with a value 4
print(4)  4
print(4*4)  16
a=7
all statements are executed with a value 7
print(7) 7
print(7*7) 49
No more values
for more updates visit:
www.python4csip.com

Lab
work
1. WAP to enter any number and
find its factorial
2. WAP to print the following fibonacci
series 0, 1, 1, 2, 3, 5, 8,… …..n
terms
3. WAP to enter 10 number and find
the sum and average.
4. WAP to enter Lower_Limit,
Upper_Limit and find the sum of
all odds and evens number
VINOD KUMAR VERMA, PGT(CS), KV OEF
between the range separately
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

while
loop
⚫ While loop in python is conditional
loop which repeat the instruction
as long as condition remains true.
⚫ It is entry-controlled loop i.e. it
first check the condition and if it is
true then allows to enter in loop.
⚫ while loop contains various loop
elements: initialization, test
condition, body of loop and
update statementVINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

while loop
elements
1. Initialization : it is used to give
starting value in a loop variable
from where to start the loop
2. Test condition : it is the
condition or last value up to which
loop will be executed.
3. Body of loop : it specifies the
action/statement to repeat in the
loop
4. Update statement : it is the
increase or decrease in loop variable
to reach the test condition.
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example of simple while


loop
i=1
while i<=10:
print(i)
i+=1

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example of simple while


loop
i=1 initializati
on
while i<=10: Test
condition
print(i) Body of
i+=1 loop
U
p

t
VINOD KUMAR VERMA, PGT(CS), KV OEF
e KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i+=1

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=1
1<=1
print(i) 0?
i+=1 True

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=1
1<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i=
OUTPUT
---------- i+=1 2
1

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=2
2<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=2
2<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i=
OUTPUT
---------- i+=1 3
1
2

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=3
3<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=3
3<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2
3

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i=
OUTPUT
---------- i+=1 4
1
2
3

In this way loop will


execute for the values 4 to
10, let us see from the
value 9
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i=
OUTPUT
---------- i+=1 9
1
2
3
4
5
6 In this way loop will
7
8
execute for the values 4 to
10, let us see from the
value 9
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=9
9<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2
3
4
5
6
7
8

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i=9
9<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2
3
4
5
6
7
8
9

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i=
OUTPUT
---------- i+=1 10
1
2
3
4
5
6
7
8
9

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i= 10
10<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2
3
4
5
6
7
8
9

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i = 10
10<=1
print(i) 0?
OUTPUT
---------- i+=1 True
1
2
3
4
5
6
7
8
9
10
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10:
print(i)
i=
OUTPUT
---------- i+=1 11
1
2
3
4
5
6
7
8
9
10
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Let us see the flow of


loop
i=1
while i<=10: i= 11
11<=1
print(i) 0?
OUTPUT
---------- i+=1 False
1
2
3
4 Now the
5 value of i is
6
7 11 and
8 condition will
9 return False so
10
loop will
terminate
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Draw a flow chart to find the sum of
squares of first ‘N’ natural numbers.

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Programs of while
loop
⚫ Convert all "for‟ loop program with while
loop
⚫ WAP to enter any number and find its
reverse
⚫ WAP to enter any number and a digit
and count how many times digit is in
the number
⚫ WAP to enter any number and
check it is armstrong or not
⚫ WAP to enter any number and
check it is palindrome or not
⚫ WAP to enter numbers as long as user
wishes to enter and find the sumPGT(CS),highest
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, KV
for more updates visit:
www.python4csip.com

Predict the output:

i=10
while i>0:
print(i,end=’$’)
i-=3

OUTPUT:
10$7$4$1$

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
Predict the output:
count=0
while count<5:
print(“Hello”) OUTPUT:
count+=1 Hello Hello Hello Hello Hello

x=10 OUTPUT:
y=0 10 0
while x>y: 91
print(x,y) 82
x-=1 73
y+=1 64
for more updates visit:
www.python4csip.com

WAP to enter any number and find its


reverse

num = int(input("Enter any


number ")) rev = 0
num2=nu
m while
num>0:
rem =
num
% 10
rev = rev * 10 +
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
WA P to enter numberswww.python4csip.com
as long as user wishes to
enter and find the sum highest and lowest
number
choice=" entered by user
y" i=1
while
choice=
="y":
n
u
m
=
in
t(i
n
p
ut
("
E
nt
er
an VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Jump Statements – break &


continue
break statement in python is used to
terminate the containing loop
for any given condition. Program
resumes from the statement
immediately after the loop

Continue statement in python is


used to skip the statements
below continue statement
inside loop and forces the loop to
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example –
forbreak
i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop
Over”)

The
above code
produces
output
1
2
3
4
SACHIN BHARDWAJ, PGT(CS), KV NO.1
5
for more updates visit:
www.python4csip.com

Example –
continue
for i in
range(1,20):
if i % 6 == 0:
contin
ue print(i,
end = „ „)
print(“Loop
Over”)

The above
code produces
output
123457
VINOD KUMAR VERMA, PGT(CS), KV OEF
8 9 10 11 13 14 KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Loop .. else ..
Statement
⚫ Loop in python provides else clause
with loop also which will execute
when the loop terminates
normally i.e. when the test
condition fails in while loop or when
last value is executed in for loop
but not when break
terminates the loop

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:

Syntax of Python loop along


www.python4csip.com

with
„else‟ clause
for with “else” while with “else”
for <var> in <seq>: while <test
statement 1 condition>:
statement 2 statement 1
else: statement 2
statement(s) else:
statement(s)

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example (“else” with


while)
i=1
Output
while i<=10: 1
2
print 3
4
(i) 5
6
i+=1 7
8
9
else: 10
Loop
print Over

("Loo
p
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example (“else” with


for)
names=["allahabad","lucknow","varanasi","kanpur","agra","
ghaziabad"
,"mathura","meerut"]
city = input("Enter city to
search: ") for c in names:
if c == city:
print(“City
brea
Found")
Output
els k Enter city to search :
e: varanasi
print("Not City Found
found") Enter city to search :
unnao
Not found
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
Example- program to
enterofrnsum.
their m o e
r u pd a e
u mThe r st rsevpsi e
b e program :ti w w w
ends d .lpyytathe
a te when ho n 4c si p. c o
nduser
p r says
int
no
m
more to enter(normal termination) or
program aborts when the number entered is less
than zero
count = sum = 0
ans = 'y'
while ans=='y':
num = int(input("Enter
number :")) if num < 0:
print("You entered
number below zero,
Aborting...")
brea
k sum +=
num
count+=1
ans
VINOD KUMAR VERMA, PGT(CS), KV OEF
=input("Co KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
Example- program to
enterofrnsum.
their m o e
r u pd a e
u mThe r st rsevpsi e
b e program :ti w w w
ends d .lpyytathe
a te when ho n 4c si p. c o
nduser
p r says
int
no
m
more to enter(normal termination) or
program aborts when the number entered is less
than zero - O U T P U T
Enter Enter
number :3 number :10
Continue(y/n)y Continue(y/n)y
Enter number
Enter
:20
number :6 Continue(y/n)y
Continue(y/n)y Enter number
Enter :50
number :8 Continue(y/n)n
Continue(y/n)y You entered 3 numbers
Enter so far Sum of entered
number :-10 number is : 80
You entered number
below zero,
Aborting...
VINOD KUMAR VERMA, PGT(CS), KV OEF
Sum of entered KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
Example- program to input
www.python4csip.com
number and tests if it is a
prime number or not
num = int(input("Enter any
number :")) lim = num//2 + 1
for i in
range(2,lim):
rem = num
% i if rem
== 0:
print(num,"
is not a
prime
number ") VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
Example- program to input
www.python4csip.com
number and tests if it is a
prime number or not - O U T P U T

Enter any number :23


23 is a prime
number

Enter any number :36


23 is not a prime
number

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Generating Random
numbers
⚫ Python allows us to generate
random number between any
range
⚫ To generate random number we
need to import random library
⚫ Syntax: (to generate random
number)
◦ random.randint(x,y)
◦ it will generate random number
between x to y
◦ For e.g. : VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Infinite loop and


„break‟
⚫ We can also execute infinite loop
purposely by specifying an expression
which always remains True.
⚫ However in loop we can mention any
condition to exit or terminate loop
a=2
while True:
print(
a)
a*=2
if
a>10 VINOD KUMAR VERMA, PGT(CS), KV OEF
0: KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Nested Loop (loop within


loop)
⚫ A loop may contain another loop in
its body. This form of loop is called
nested loop.
⚫ In Nested loop, the inner loop will
execute for each value of outer
loop.
⚫ For e.g. if the outer loop is of 4
times and inner loop is of 5 times
then statement will execute 4 x 5
= 20 times VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Syntax of Nested
LoopLoop – FOR
Nested Nested Loop – WHILE

for i in sequence: while condition:


for j in while
sequence: condition:
Statem Statem
ent1 ent1
Statement2 Statement2

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example – to print table of 2


to 10

4
to
8

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com

Example – Program to print the following


patterns

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Write a program to print the
following pyramid pattern

VINOD KUMAR VERMA, PGT(CS), KV OEF


KANPUR & SACHIN BHARDWAJ, PGT(CS), KV

You might also like