0% found this document useful (0 votes)
3 views21 pages

Unit 2

The document covers conditional statements and loops in Python, detailing the use of if, if-else, and if-elif-else statements along with examples. It also explains loop structures such as for and while loops, including the use of break and continue statements. Additionally, it discusses the pass statement, deletion of variables, and the concept of nested loops.

Uploaded by

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

Unit 2

The document covers conditional statements and loops in Python, detailing the use of if, if-else, and if-elif-else statements along with examples. It also explains loop structures such as for and while loops, including the use of break and continue statements. Additionally, it discusses the pass statement, deletion of variables, and the concept of nested loops.

Uploaded by

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

UNIT II

Conditionals: Conditional statement in Python (if-else statement, its working


and execution), Nested-if statement and Elif statement in Python, Expression
Evaluation & Float Representation. Loops: Purpose and working of loops, while
loop including its working, For Loop, Nested Loops, Break and Continue.
I. Conditional Statements
1) if: If condition is evaluated to True, the code inside the body of if is executed.
If condition is evaluated to False, the code inside the body of if is skipped.

if condition : statement
OR
if condition :
statement-1
statement-2
statement-3

If condition is true then statements will be executed.

Eg: 1

name= input("enter your name : ")


if(name=="abc"):
print("hello how are you:" , name)
print("i dont know")
Eg: 2
name= input("enter your name : ")
if(name=="abc"):
print("hello how are you:" , name)
print("i dont know")
Eg: 3
num=int(input("enter a number: "))
if num>0:
print(‘number is positive:’)
print(‘the if statement is easy’)
2) if-else: if condition is true then Action-1 will be executed otherwise Action-2 will be executed

if condition:

Action-1

else:

Action-2

Eg:1

name= input("enter your name : ")


if(name=="abc"):
print("hello how are you:" , name)
else:
print("i dont know")

Eg:2

name= input("enter your name : ")


if(name=="abc"):
print("hello how are you:" , name)
print(name,"good morning")
else:
print("i dont know")
print(name, 'good morning')
Eg: 3
num=int(input("enter a number: "))
if num>0:
print("the number is not zero")
print("its not zero")
else:
print("the number is zero")
print("hello")
print("the acutual no. is: ", num)
print(‘this statement is always excuted”)

3) if-elif-else: Based condition the corresponding action will be executed.

if condition1:
Action-1
elif condition2:
Action-2
elif condition3:
Action-3
elif condition4:
Action-4
...
else:
Default Action
Eg:

num=int(input("enter a number: "))


if num>0:
print("the number is postivite integer")
print("the number is :", num)
elif num==0:
print("the number is zero")
else:
print("the number is negative integer")
print("the Number is:", num)
print("this statement is always excuted")

Note: There is no switch statement in Python

Q)1 Write a Program to find Biggest of given 2 Numbers?

n1=int(input("Enter First Number"))


n2=int(input("Enter Second Number"))
if n1>n2:
print("The greater number is :", n1)
else:
print("the Greater number is :", n2)

Q2) Write a Program to find Biggest of given 3 Numbers?

n1=int(input("Enter First Number"))


n2=int(input("Enter Second Number"))
n3=int(input("enter Third Number"))
if n1>n2 and n1>n3:
print("The greater number is :", n1)
elif n2>n3:
print("the Greater number is :", n2)
else:
print("the greater number:", n3)
Q3) Write a program to find smallest of given 2 numbers?

n1=int(input('enter first number:'))


n2=int(input('enter second number:'))
if(a<b):
print(n1)
else:
print(n2)

Q) Write a program to check whether the given number is even or odd?

n1=int(input("enter a number:"))
if(n1%2==0):
print(n1,"the number is even no")
else:
print(n1,"The number is odd")

Q) Write a Program to check whether the given Number is in between 1 and 100?

n2=int(input("enter a number"))
if n2>=1 and n2<=100:
print(n2,"The number lies between 1 to 100")
else:
print(n2,"the number is out of range")

Q) Write a Program to take a Single Digit Number from the Keyboard and Print is Value in English
Word?

n=int(input("enter a number:"))
if n==0:
print("zero")
elif n==1:
print("one")
elif n==2:
print("two")
elif n==3:
print("three")
elif n==4:
print("four")
elif n==5:
print("five")
elif n==6:
print("sex")
elif n==7:
print("seven")
elif n==8:
print("eight")
elif n==9:
print("nine")
else:
print(n, "is out of range")
II. Transfer Statements
1) break: We can use break statement inside loops to break loop execution based on some
condition.

for i in range(10):
if i==7:
print("processing is enough..plz break")
break
print(i)
print(i)
D:\Python_classes>py test.py
0 1 2 3 4 5 6
processing is enough..plz break
Eg:
cart=[10,20,600,60,70]
for item in cart:
if item>500:
print("To place this order insurance must be required")
break
print(item)
D:\Python_classes>py test.py
10 20
To place this order insurance must be required

for i in range(5):

if(i==4):
break
print(i)
print("hello")

Output: 0
1
2
3
hello
2) Continue:
We can use continue statement to skip current iteration and continue next iteration.

Eg 1: To print odd numbers in the range 0 to 20

for i in range(1,20):
if i%2==0:
continue
print(i)
output: 1
3
5
7
9
11
13
15
17
19

Eg 2:

cart=[10,20,500,700,50,60]
for item in cart:
if item>=500:
print("We cannot process this item :",item)
continue
print(item)
10
20
We cannot process this item : 500
We cannot process this item : 700
50
60

Eg 3:
x=[10,20,0,30,0,40,50]
for i in x:
if(i==0):
print("how we can divide zero.... just skip")
continue
print("100/{} = {}".format(i,100/i))
Output

100/10 = 10.0
100/20 = 5.0
hoe we can divide zero.... just skip
100/30 = 3.3333333333333335
hoe we can divide zero.... just skip
100/40 = 2.5
100/50 = 2.0
In [ ]:

Loops with else Block:


Inside loop execution, if break statement not executed, then only else part will be executed.
else means loop without break.
cart=[10,20,30,600,50]
for item in cart:
if item>=500:
print("We cannot process this order")
break
print(item)
else:
print(item, "Congrats ...all items processed successfully")
print("hello")
Output
10 Congrats ...all items processed successfully
20 Congrats ...all items processed successfully
30 Congrats ...all items processed successfully
We cannot process this order
hello

Eg:
cart=[10,20,30,40,50]
for item in cart:
if item>=500:
print("We cannot process this order")
break
print(item)
else:
print(item,"Congrats ...all items processed successfully")
print("hello")
Output
10 Congrats ...all items processed successfully
20 Congrats ...all items processed successfully
30 Congrats ...all items processed successfully
40 Congrats ...all items processed successfully
50 Congrats ...all items processed successfully
hello

Q) What is the difference between for loop and while loop in Python?
֍ We can use loops to repeat code execution
֍ Repeat code for every item in sequence

֍ Repeat code as long as condition is true

֍ Repeat code as long as condition is false


Q) How to exit from the loop?
By using break statement
Q)How to skip some iterations inside loop?
By using continue statement.
Q)When else part will be executed wrt loops?
If loop executed without break

3) pass statement:

we can define that empty block with pass keyword.

pass
|- It is an empty statement
|- It is null statement
|- It won't do anything

def m1():
SyntaxError: unexpected EOF while parsing
def m1():
pass

Eg:
for i in range(5):
if i==5:

Output:
File "<ipython-input-8-25904615d822>", line 3

^
SyntaxError: unexpected EOF while parsing

Eg:
for i in range(5):
if i==5:
pass

Eg:
for i in range(5):
if i==5:
pass
print(i)

Output:
4

Use Case of pass:


Sometimes in the parent class we have to declare a function with empty body and child
class responsible to provide proper implementation. Such type of empty body we can define
by using pass keyword. (It is something like abstract method in Java)
Eg:
def m1():
pass
for i in range(100):
if i%9==0:
print(i)
else:
pass

OutPut:
0
9
18
27
36
45
54
63
72
81
90
99
del Statement:
thon.

required, so that the corresponding object is eligible for Garbage Collection.

x=10
print(x)
del(x)
print(x)
After deleting a variable we cannot access that variable otherwise we will get NameError.
x=10
print(x)
del(x)
print(x)
Note:
We can delete variables which are pointing to immutable objects. But we cannot delete the
elements present inside immutable object.
s="united"
print(s)
del s[1]
output:

united
-------------------------------------------------------------------
--------
TypeError Traceback (most recent ca
ll last)
<ipython-input-19-d570f2083b28> in <module>
1 s="united"
2 print(s)
----> 3 del s[1]

TypeError: 'str' object doesn't support item deletion


III. Iterative Statements
If we want to execute a group of statements multiple times then we should go for Iterative
statements.

Python supports 2 types of iterative statements: for loop, while loop

1) for loop: If we want to execute some action for every element present in some sequence (it may
be string or collection) then we should go for for loop.

Syntax: for x in sequence:


Body

Where sequence can be string or any collection.

Body will be executed for every element present in the sequence.


Eg 1: To print characters present in the given string

s="abcde"

for x in s :

print(x)

Output
a
b
c
d
e

Eg 2: To print characters present in string index wise:

v=input("enter a string")
x=0
for i in v:
print("The Character present is",x,"index is ", i)
x=x+1

The Character present is 0 index is a


The Character present is 1 index is k
The Character present is 2 index is a
The Character present is 3 index is n
The Character present is 4 index is s
The Character present is 5 index is h
The Character present is 6 index is a

Eg 3: To print Hello 10 times

for x in range(10) :
print("Hello")

hello
hello
hello
hello
hello
hello
hello
hello
hello
hello

Eg 4: To display numbers from 0 to 10

for x in range(11) :
print(x)

Eg 5: To display even numbers from 0 to 20

for i in range(20):

if i%2==0:

print(i,"is even number")

0 is even number
2 is even number
4 is even number
6 is even number
8 is even number
10 is even number
12 is even number
14 is even number
16 is even number
18 is even number

Eg 6: To display numbers from 10 to 1 in descending order

for i in range(20,0,-1):

print(i,)
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
2) while loop: If we want to execute a group of statements iteratively until some condition false,
then we should go for while loop.

Syntax: while condition:

body

Eg: To print numbers from 1 to 10 by using while loop

x=1
while x<10:
print(x )
x=x+1

1
2
3
4
5
6
7
8
9

Eg: To display the sum of first n numbers

n=int(input("Enter number:"))
sum=0
i=1
while i<=n:
sum=sum+i
i=i+1
print("The sum of first" ,n, "numbers is :",sum)

enter a number5
15
Nested Loops:
Sometimes we can take a loop inside another loop, which are also known as nested loops.

for i in range(4):

for j in range(4):

print("i=",i," j=",j)

Output
D:\Python_classes>py test.py
i= 0
j= 0
i= 0
j= 1
i= 0
j= 2
i= 0
j= 3
i= 1
j= 0
i= 1
j= 1
i= 1
j= 2
i= 1
j= 3
i= 2
j= 0
i= 2
j= 1
i= 2
j= 2
i= 2
j= 3
i= 3
j= 0
i= 3
j= 1
i= 3
j= 2
i= 3
j= 3

You might also like