Unit 2
Unit 2
if condition : statement
OR
if condition :
statement-1
statement-2
statement-3
Eg: 1
if condition:
Action-1
else:
Action-2
Eg:1
Eg:2
if condition1:
Action-1
elif condition2:
Action-2
elif condition3:
Action-3
elif condition4:
Action-4
...
else:
Default Action
Eg:
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.
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 [ ]:
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
3) pass statement:
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
OutPut:
0
9
18
27
36
45
54
63
72
81
90
99
del Statement:
thon.
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]
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.
s="abcde"
for x in s :
print(x)
Output
a
b
c
d
e
v=input("enter a string")
x=0
for i in v:
print("The Character present is",x,"index is ", i)
x=x+1
for x in range(10) :
print("Hello")
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
for x in range(11) :
print(x)
for i in range(20):
if i%2==0:
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
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.
body
x=1
while x<10:
print(x )
x=x+1
1
2
3
4
5
6
7
8
9
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