Condition (if
else) Output Assignment 1
Que. 1 .
x=2
y=1
if x>12:
x=x*2
y=x+10
print("x=
",x,", y= ",y)
print("-"*30)
Que. 2 .
x=12
y=11
if x>=12:
x=x*2
y=x+10
print("x=
",x,", y= ",y)
print("-"*30)
Que. 3 .
x=11
y=11
if x<12:
x=x+2
y=x*3
print("x=
",x,", y= ",y)
print("-"*30)
Que. 4 .
x=11
y=11
if x!=y:
x=x+3
y=x+4
print("x=
",x,", y= ",y)
print("-"*30)
Que. 5 .
x=2
y=4
if x==y:
x=x*3
y=x*4
print("x=
",x,", y= ",y)
print("-"*30)
Que. 6 .
x=2
y=4
if x>y:
x=x*3
y=x*4
else:
x=x+3
y=x+4
print("x=
",x,", y= ",y)
print("-"*30)
Que. 7 .
x=12
y=4
if x>y:
x=x*3
y=x*4
else:
x=x+3
y=x+4
print("x=
",x,", y= ",y)
print("-"*30)
Que. 8 .
x=2
y=4
if x!=y:
x=x-3
y=x*4
else:
x=x-3
y=x+4
print("x=
",x,", y= ",y)
print("-"*30)
Que. 9 .
x=2
y=4
if x!=y:
if x>y:
x=x-3
else:
y=x+4
print("x=
",x,", y= ",y)
print("-"*30)
Que. 10 .
x=2
y=4
if x>=y:
if x>y:
x=x*3
else: y=x+9
print("x=
",x,", y= ",y)
print("-"*30)
Que. 11 .
x=2
y=4
if x>=y:
if x>y:
x=x*3
print("x=
",x,", y= ",y)
print("-"*30)
Que. 12 .
x=4
y=4
if x>y:
if x<y:
x=y*3
print("x=
",x,", y= ",y)
print("-"*30)
Que. 13 .
x=4
y=4
if x>y:
if x>y:
x=y*3
print("x=
",x,", y= ",y)
print("-"*30)
Que. 14 .
x=4
y=4
if x<y:
if x>y:
x=y*3
print("x=
",x,", y= ",y)
print("-"*30)
Que. 15 .
x=4
y=4
if x==y:
if x>y:
x=y*3
print("x=
",x,", y= ",y)
print("-"*30)
Que. 16 .
x=4
y=4
if x==y:
if x>y:
x=y*3
y+=6
print("x=
",x,", y= ",y)
print("-"*30)
Condition (if
else) Output Assignment 2
Que. 1 .
x=False
y=True
if not x and not
y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 2 .
x=False
y=True
if x and not y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 3 .
x=False
y=True
if not x
and y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 4 .
x=False
y=True
if not x or not
y:
print("> if Block Working *")
else:
print("> else Block Working
*")
print("**Always
Working **")
print("-"*30)
Que. 5 .
x=False
y=True
if not x or y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 6 .
x=False
y=True
if x and y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 7 .
x=False
y=True
if not x or not
y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 8 .
x=False
y=True
if x and y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 9 .
x=False
y=True
if x or y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 10 .
x=True
y=True
if x and y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 12 .
x=False
y=False
if x and y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 13 .
x=False
y=False
if x or y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 14 .
x=True
y=True
if x or y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 15 .
x=True
y=True
if x and y:
print("> if Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Condition (if
else) Output Assignment 3
Que. 1 .
x=True
y=True
z=True
if x and y and z:
print("> if Block Working *")
elif x or y or z:
print("> elif Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 2 .
x=True
y=True
z=True
if x or y or z:
print("> if Block Working *")
elif x and y and z:
print("> elif Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 3 .
x=True
y=True
z=True
if not x and
not y and not z:
print("> if Block Working *")
elif x or y or z:
print("> elif Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 4 .
x=True
y=True
z=True
if not x and
not y and not z:
print("> if Block Working *")
print("> elif Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 4 .
x=False
y=False
z=False
if x and
y and not z:
print("> if Block Working *")
elif not x
or not y or not z:
print("> elif Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Que. 5 .
x=False
y=False
z=False
if not x and
not y and not z:
print("> if Block Working *")
elif not x
or not y or not z:
print("> elif Block Working *")
else:
print("> else Block Working *")
print("**Always
Working **")
print("-"*30)
Loop Output Assignment
1
Que. 1 .
x="DOTPYEDU"
for i in x:
print(i, end="")
print("-"*30)
Que. 2 .
x="DOTPYEDU"
for i in x:
for i in x:
print(i, end="")
print("*")
print("-"*30)
Que. 3 .
x="DOTPYEDU"
for i in x:
for i in x:
print(i,end="")
print()
print("-"*30)
Que. 5 .
x="DOTPYEDU"
for i in
range(len(x)):
for i in x:
print(i,end="")
print()
Que. 6 .
x="DOTPYEDU"
for i in
range(len(x)):
for i in range(len(x)-1):
print(i,end="")
print()
print("-"*30)
Que. 7 .
x="Education"
for i in
range(len(x)):
for i in range(len(x)-1):
print(x[i],end="")
print()
print("-"*30)
Que. 8 .
x="EducAtiOn"
v=[]
l=['a','e','i','o','u',]
for i in
range(len(x)):
if x[i] in l:
v.append(x[i])
print(v)
print("-"*30)
Que. 9 .
x="EducAtiOn"
l=['a','e','i','o','u']
v=[]
for i in
range(len(x)):
if x[i].lower() in l:
v.append(x[i])
print(v)
print("-"*30)
Que. 10 .
x="Education
For Everyone"
l=['a','e','i','o','u','A','E','I','O','U']
v=[]
for i in
range(len(x)):
if x[i] in l:
v.append(x[i])
print(v)
print("-"*30)
Que. 11 .
x="Education
For Everyone"
l=['a','e','i','o','u','A','E','I','O','U']
v=[]
for i in
range(len(x)):
if x[i] in l:
v.append(x[i])
print(v)
print("-"*30)
Que. 12 .
x="DOTPYEDU"
for i in x:
print(x,end=" $ ")
print("-"*30)
Que. 13 .
x="DOTPYEDU"
for i in x:
print(x.lower(),end=" # ")
print("-"*30)
Que. 14 .
x="DOTPYEDU"
l=['a','e','i','o','u','A','E','I','O','U']
for i in x:
if i in l:
print(x.lower(),end="")
else:
print(x.upper(),end="")
print()
print("-"*30)
Que. 15 .
x="DOTPYEDU"
l=['a','e','i','o','u','A','E','I','O','U']
for i in x:
if i in l:
print(i.lower(),end="")
else:
print(i.upper(),end="")
print("-"*30)
Que. 16 .
x="DOTPYEDU"
l=['a','e','i','o','u','A','E','I','O','U']
newlist=[]
for i in x:
if i in l:
newlist.append(i.lower())
else:
newlist.append(i.upper())
print(newlist)
print("-"*30)
Que. 17.
x="DOTPYEDU"
l=['a','e','i','o','u','A','E','I','O','U']
newlist=[]
for i in x:
if i in l:
newlist.append(i.lower())
else:
newlist.append(i.upper())
print(newlist)
old=""
old=old.join(newlist)
print(old)
print("-"*30)
"""
0
INTRODUCTION AND TYPE AND CALLING OF FUNCTIUONS
print("1.Default
function used in : \nDisplay Static Data/Menu Display/Choice Display")
def FUN(): #definartion
print("Python")
print("is")
print("FUN!")
FUN() #calling
print("-"*25)
print("2.
Parameterize/ Argumenta function used in : \nperform operation's")
def FUN(a): #definartion
print("a is %d"%a)
print("a*a is %d"%(a*a))
print("a**a is %d"%(a**a))
FUN(5) #calling
print("-"*25)
print("3.
Return Single Argument 1")
def FUN(a): #definartion
return a*a
print(FUN(5)) #calling
print("-"*25)
print("4.
Return Single Argument 2")
def FUN(a): #definartion
return a*a
r=FUN(5) #calling
print(r)
print("-"*25)
print("4.
Return Multiple Argument 1")
def FUN(a): #definartion
return a*a,a**3
print(FUN(5)) #calling
print("-"*25)
print("6.
Return Multiple Argument 2 ")
def FUN(a): #definartion
return a*a,a**3
r1,r2=FUN(5)
print(r1,",
",r2) #calling
print("-"*25)
print("7.
Return Multiple Argument 2 using index")
def FUN(a): #definartion
return a*a,a**3
r1=FUN(5)[0]
r1=FUN(5)[1]
print(r1,",
",r2) #calling
print("-"*25)
print("8.
Function Arguments LIST")
def FUN(a): #definartion
print(a)
D=[1,2,3,5]
FUN(D) #calling
print("-"*25)
print("9.
Function Arguments Tuple")
def FUN(a): #definartion
print(a)
D=(1,2,3,5)
FUN(D) #calling
print("-"*25)
print("10.
Function Arguments String")
def FUN(a): #definartion
print(a)
D="DotPyEdu"
FUN(D) #calling
print("-"*25)
print("11.
Function Arguments Dictionary")
def FUN(a): #definartion
print(a)
D={1:"PYTHON",2:"is",3:"Fun",5:"Function's"}
FUN(D) #calling
print("12.
Function with de
fult Argument")
def
FUN(a=10): #definartion
print(a)
a=4
print(a)
FUN(a) #calling
print(a)
print("13.
Function with defult Argument")
def
FUN(a,b=10): #definartion
print(a,",",b)
a,b=4,13
print(a,",",b)
FUN(a,b) #calling
FUN(a) #calling
FUN(b) #calling
FUN(b,a) #calling
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif)
ASSIGNMENT OF FUNCTION
print("Q1.
")
def
switchover(A,N, split):
K=0
while K<N:
if K<split :
A[K]+= K
else:
A[K]*= K
K+=1
def
display(A,N):
K=0
while K<N:
if K%2== 0:
print(A[K],end="%")
else:
print(A[K],"\n")
K+=1
H=
[30,40,50,20,10,5]
switchover(H,6,3)
print(H)
display(H,6)
print("="*30)
print("Q2.
")
def s(a, b):
a+=2
b*=5
print(a,":",b,"\n")
a,b=4,8
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")
print("="*30)
print("Q3.
")
def s(a, b):
a+=2
b[0]=b[0]+5
print(a,":",b,"\n")
a,b=4,[8]
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")
print("="*30)
print("Q4.
")
def s(a, b):
a[0]+=2
b[0]+=5
print(a,":",b,"\n")
a,b=[4],[8]
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")
print("="*30)
print("Q5.
")
def s(a, b=7):
a+=2
b*=5
print(a,":",b,"\n")
a,b=4,8
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")
s(a)
print(a,":",b,"\n")
print("="*30)
print("Q6.
")
def s(a=6, b=7):
a+=2
b*=5
print(a,":",b,"\n")
a,b=4,8
print(a,":",b,"\n")
s(a,b)
print(a,":",b,"\n")
s(a)
print(a,":",b,"\n")
s()
print(a,":",b,"\n")
print("="*30)
print("Q7.
")
def func(x,y):
if x[0]%y[0]==0 :
return x[0]+2
else:
return y[0]-1
p,q=[20],[23];
print(p,",",q)
q=func(p,q)
print(p,",",q)
print("="*30)
print("Q8.
")
def
func(x,y=10):
if x%y==0 :
return x+2
else:
return y-1
p,q=20,23;
print(p,",",q)
q=func(p,q)
print(p,",",q)
print("="*30)
print("Q9.
")
def func(x,y=10):
if x%y==0 :
return x+2
else:
return y-1
p,q=20,23;
print(p,",",q)
q=func(p,q)
print(p,",",q)
p=func(q)
print(p,",",q)
print("="*30)
print("Q10.
")
def
func(x,y=10):
if x%y==0 :
return x+2
else:
return y-1
p,q=20,23;
print(p,",",q)
q=func(p,q)
print(p,",",q)
p=func(q)
print(p,",",q)
q=func(p)
print(p,",",q)
print("="*30)
print("Q11.")
def
execute(x,y=200):
temp=x+y
x+=temp
if(y!=200):
print(temp,",",x,",",y)
a,b =50,20
execute(b)
print(a,",",b)
execute(a,b)
print(a,",",b)
print("="*30)
print("Q12.")
def
execute(x,y=15):
temp=x+y
x+=temp
if(y<=20):
print(temp,",",x,",",y)
a,b =50,20
execute(b)
print(a,",",b)
execute(b)
print(a,",",b)
print("="*30)
print("Q13.")
def
execute(x,y=20):
temp=x*y
x+=temp
if(y>=20):
print(temp,",",x,",",y)
a,b =50,20
execute(b)
print(a,",",b)
execute(a)
print(a,",",b)
Randomize Assignment
import random
Que. 1 .
N=20
for i in range(4):
Guessnum=random.randrange(0,N-10)+10
print(Guessnum,end=' ')
print()
# Output Options :
#10 to 19 , 4 numbers
Que.2
Guess=65
for I in range(1,5):
New=Guess+random.randrange(0,I)
print(chr(New),end=' ')
# Output Options :
i. A B B C ii. A C B A
iii. B C DA iv. C A B D
print()
Que.3.
n=1
VAL=12
Rnd=8 +
random.randrange(0,VAL) * 1
while(n<=Rnd):
print(n,end=" ")
n+=1
print()
# Output Options :
i.
1 2 3
4 5 6
7 8 9
10 11 12
13 ii. 0
1 2 3
# iii. 1 2
3 4 5
iv. 1 2 3 4 5
6 7 8
Que.4.
RN=random.randrange(0,4)+5;
for I in range(1,RN):
print(i,end=' ')
#
Output Options:
i. 0 1 2
ii. 1 2 3 4 5 6 7 8
iii. 4 5 6 7 8 9 iv. 5 6 7 8 9 10 11 12
print()
Que.5.
Score=[ 25,20,34,56,
72, 63]
Myscore = Score[2 +
random.randrange(0,2)]
print(Myscore)
# Output Options :
i. 25 ii. 34
iii. 20 iv. None of the above
Que.6.
Marks = [99, 92, 94,
96, 93, 95]
MyMarks = Marks [1 +
random.randrange(0,2) ]
print(MyMarks)
# Output Options :
i. 99 ii. 94 iii. 96
iv. None of the above
Que.7.
Disp=22
Rnd=random.randrange(0,Disp)+15
N=1
for I in
range(3,Rnd,4):
print(N,end=" ")
N+=1
print()
#Output Options:
i. 1 ii. 1 2
3 4 iii. 1
2 iv. 1
2 3
Que.8.
N=37
Guess_num=random.randrange(0,N-8)+38
print(Guess_num)
#what maximum and
minimum values the program could possibly display?
Que.9.
city= [ "DEL", "CHN",
"KOL", "BOM", "BNG"]
for I in range(1,4):
Fly = random.randrange(0,2) + 1 ;
print( city[Fly] ,end=":")
print()
#Output Options:
i. DEL: CHN : KOL : ii. CHN : KOL : CHN :
iii. KOL : BOM : BNG : iv. KOL : CHN : KOL :
Que.10.
Area=["NORTH","SOUTH","EAST","WEST"]
for I in range(3):
ToGo=random.randrange(0,2) + 1
print(Area[ToGo],end=":")
print()
#Output Options:
i. SOUTH : EAST : SOUTH
:ii. NORTH : SOUTH : EAST :
iii. SOUTH : EAST :
WEST : iv. SOUTH : EAST : EAST :
Que.11.
Num=13
RndNum =
random.randrange(0,Num) +13;
for N in
range(1,RndNum):
print(N,end=" ")
print()
Que.12.
r=random.randrange(0,20)+random.randrange(0,2)
print(r)
Que.13.
#fill in the blanks for
the statement1 with the help of random
function
#if the number
generated by the random number is supposed to be between the range of 25-2025.
#r=_______________//statement
1
print(r)
Que.14.
#fill in the blanks for
the statement1 with the help of random
function
#if the number
generated by the random number is supposed to be between the range of 20-2000.
#r=_______________//statement
1
print(r)
Que.14.1.
#fill in the blanks for
the statement1 with the help of random
function
#if the number
generated by the random number is supposed to be between the range of 41-441.
#r=_______________//statement
1
print(r)
Que.15.
low=25
point =5
for i in range (1,5):
Number=low + random.randrange(0,point);
print(Number,end="")
point-=1;
print()
#Output Options:
i. 29: 26:25 :28 : ii. 24: 28:25 :26 :
iii. 29: 26:24 :28
: iv. 29: 26:25 :26 :
Que.16.
Max=3
Div=1+random.randrange(0,Max)
for N in range(1,5):
print(100%Div,end="#")
print()
#Output Options:
i. 0#0#0#0#
ii. 1#1#1#1# iii. 2#2#2#2# iv.
3#3#3#3#
Que.17.
MIN = 25
SCORE = 10
for i in range (1,5):
Num = MIN + random.randrange(0,SCORE)
print(Num,end=":")
SCORE-=1;
print()
#Output Options:
i. 34:31:30:33: ii. 29:33:30:31:
iii. 34:31:30:31: iv. 34:31:29:33:
Que.18.
Max=3;
Number=50+random.randrange(0,Max)
for P in
range(Number,50-1,-1):
print(P,end="#")
print()
#Output Options:
i. 53#52#51#50# ii. 50#51#52#
iii. 50#51# iv. 51#50#
Que.19. ")
MAX = 3;
digit =80 +
random.randrange(0,MAX )
for R in range
(digit,80-1,-1):
print(R,end="$")
print()
#Output Options:
i. 83$82$81$80$ ii.
80$81$82$ iii. 80$81$ iv. 81$80$
Que.20.
p=99
q=999
x=random.randrange(0,3)+4
y=random.randrange(0,2)+2
for i in range(x):
print("#",end="")
print(p,"-",end="")
for i in range(x):
print("@",end="")
print(q)
#Output Options:
i. ##99-@999 ii. ##99-@@999
iii. ######99-@@999 iv.
####99-@@@
Que.21.
LIMIT = 4
Points = 100
+random.randrange(0,LIMIT);
for p in
range(Points,99,-1):
print(p,end="#")
print()
#Output Options:
i. 103#102#101#100# ii.
100#101#102#103#
iii.
100#101#102#103#104# iv.
104#103#102#101#100#
Que.22.
#if the value of N
given by the user is 20,
#what maximum and
minimum values the program could possibly display?
N=20
Guessnum=random.randrange(0,N-10)+10;
print(Guessnum)
Que.23.
High=4
Guess=random.randrange(0,High)
+50
for C in range(Guess,
56):
print(C,end="#")
#Output Options:
i 50 # 51 # 52 # 53 #
54 # 55 # ii 52 # 53 # 54 # 55 #
iii 53 # 54 # iv 51 # 52 # 53 # 54 # 55
print()
Que.24.
LOW = 15
POINT =5
for I in range (1,5):
Number = LOW + random.randrange(0,POINT)
print(Number ,end=":")
POINT-=1
print()
#Output Options:
i.19:16:15:18: (ii. 14:18: 15:16:
iii. 19:16:14:18: (iv. 19:16:15:16:
Que.25.
Max=4;
Mynum=20+random.randrange(0,Max);
for N in range (Mynum,26):
print(N,end="*")
print()
#Output Options:
i. 20*21*22*23*24*25* ii. 22*23*24*25*
iii. 23*24* iv. 21*22*23*24*25
Que. 26.
Score=[25,20,34,56, 72,
63]
n=2
Myscore = Score[1 +
random.randrange(0,n)]+4;
print(Myscore)
#Output Options:
i. 25 ii. 72 iii.
24 iv. None of the above
Que.27.
serial=['E', 'X', 'A',
'M']
number=[ 69, 66, 67,
68]
print(number[random.randrange(0,3)])
for i in range (4):
print(serial[2+ random.randrange(0,2) - 1
])
#Output Options:
i. 66AAXA ii.
67AAAM iii. 67XXAX iv. 69AXXA
Que.28.
p=["Computer","Mouse","Keyboard","Pen
Drive"]
for i in range (1, 4):
print(p[random.randrange(i)],end="
")
print()
#Output Options:
# i. Mouse Keyboard
Pen drive ii. Computer Computer Keyboard
iii. Computer
Computer Mouse iv. None of the above
Que.29.
serial=['A', 'B', 'C',
'D']
number=[2, 6, 7, 3 ]
print(" The DATA
is : ",end="")
print(serial
[random.randrange(0,3)],end="")
for i in range ( 4):
print(number[2+ random.randrange(0,2) - 1
],end="")
#Output Options:
i. The winner is :
A2776 ii. The winner is : D6766
#iii. The winner is :
B6767 iv. The winner is : C3672
Que.30.
Arr=[ 9,6]
Chance=random.randrange(0,2)+10
for I in range (2):
N=random.randrange(0,2)
print(Arr[N]+Chance,end="*")
print()
#Output Options:
i. 9*6* ii. 19*17* iii. 19*16* iv. 20*16*
Que. 31.
A="WELCOME"
ToGo=2
for I in range
(len(A)):
ToGo=random.randrange(0,2*2) +1;
print(A[ToGo],end=":")
print()
#Output Options:
i. W: E: L: C: O: M:
E: ii. E: C: E: E: C: C: E:#
#iii.E: C: E: E: C: C:
O: iv. C: C: C: E: E: C: C:
print("-"*31)
Que.32.
X =[100,75,10,125]
Go
=random.randrange(0,2)+2
for i in range (Go,4):
print(X[i],end="$$")
#Output Options:
i. 100$$75 ii. 75$$10$$125$$
iii. 75$$10$$ iv.10$$125$
String
Assignment 1
Que. 1 .
x="DOT"
y="PY"0
z="EDU"
r=x+y+z
print(r)
Que. 2 .
x="DOT"
y="PY"
z="EDU"
r=x*3+y*1+z*2
print(r)
Que. 3 .
str="GENERATORS"
r=str*3
print(r)
Que. 4.
str="GENERATORS"
for i in str:
print(str)
Que. 5.
str="GENERATORS"
for i in str:
print(i)
Que. 6.
str="GENERATORS"
for i in
range(len(str)):
print(str)
Que. 7.
str="GENERATORS"
for i in
range(len(str)):
print(str*i)
Que. 8.
str="GENERATORS"
for i in
range(len(str)):
print(str[:])
Que. 9.
str="GENERATORS"
for i in range(len(str)):
print(str[::2])
Que. 10.
str="GENERATORS"
for i in
range(len(str)):
print(str[i::])
Que. 11.
str="GENERATORS"
for i in
range(len(str)):
print(str[i::i+1])
Que. 12.
str="GENERATORS"
for i in
range(len(str)):
print(str[i*-1::])
Que. 13.
str="GENERATORS"
for i in
range(len(str)):
print(str[i*-1::-2])
Que. 14.
str="GENERATORS"
for i in
range(len(str)):
if i%2==0:
print(str[i:])
Que. 15.
str="GENERATORS"
for i in
range(len(str)):
if i%3==0:
print(str[i:])
Que. 16.
str="GENERATORS"
for i in
range(len(str)):
if i%3!=0:
print(str[i:])
Que. 17.
str="GENERATORS"
for i in
range(len(str)):
print(str[i:len(str)])
Que. 18.
str="GENERATORS"
for i in
range(len(str)):
print(str[i:len(str)-i])
Que. 19.
str="GENERATORS"
for i in
range(len(str)):
print(str[i:len(str)-i%2])
Que. 20.
str="GENERATORS"
for i in
range(len(str)):
print(str[i:len(str)-i%2])
String
Assignment 2
Que. 1 .
s="CBSE
Exam 2019-2020"
print(s)
r=""
size=len(s)
for i in
range(size):
if i%2==0:
r+=s[i].upper()
else:
r+=s[i]
print(r)
Que. 2 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
size=len(s)
for i in
range(size):
if i%2==0:
r+=s[i].upper()
else:
r+=s[i].lower()
print(r)
Que. 3 .
s="Cbse Exam
2019-2020"
print(s)
r=""
size=len(s)
for i in
range(size):
if i%2!=0:
r+=s[i].upper()
else:
r+=s[i].lower()
print(r)
Que. 4 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
size=len(s)
for i in
range(size):
if i%3==0:
r+=s[i].upper()
else:
r+=s[i].lower()
print(r)
Que. 5 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
size=len(s)
for i in
range(size):
if i%3==0:
r+=s[i].upper()
elif i%2==0:
r+=s[i].lower()
else:
r=r+s[i]
print(r)
Que. 6 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
r+=i.lower()
elif i.islower():
r+=i.upper()
else:
r=r+i
print(r)
Que. 7 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper()== False:
r+=i.lower()
elif i.islower()== False:
r+=i.upper()
else:
r=r+i
print(r)
Que. 8 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper()== True:
r+=i.lower()
elif i.islower()== False:
r+=i.upper()
else:
r=r+i
print(r)
Que. 9 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
r+=i.lower()
elif i.islower():
r+=i.upper()
elif i.isdigit():
r+=str(int(i)+1)
else:
r=r+i
print(r)
Que. 10 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
r+=(chr(ord(i)+1)).upper()
elif i.islower():
r+=(chr(ord(i)+2)).upper()
elif i.isdigit():
r+=str(int(i)+1)
else:
r=r+i
print(r)
Que. 11 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
r+=(chr(ord(i)-1)).upper()
elif i.islower():
r+=(chr(ord(i)-2)).upper()
elif i.isdigit():
r+=str(int(i)+1)
else:
r=r+i
print(r)
Que. 12 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
r+=(chr(ord(i)-3)).upper()
elif i.islower():
r+=(chr(ord(i)-1)).upper()
elif i.isdigit():
p=int(i)
if p>8:
p-=2
else:
p+=1
r+=str(p)
else:
r=r+i
print(r)
Que. 13 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
r+=(chr(ord(i)+1)).upper()
elif i.islower():
r+=(chr(ord(i)+1)).upper()
elif i.isdigit():
p=int(i)
if p%2==0:
p=00
else:
p=1
r+=str(p)
else:
r=r+i
print(r)
Que. 14 .
s="Cbse
Exam 2019-2020"
print(s)
r=""
for i in s:
if i.isupper():
if s.find(i)%2==0:
r+=i.upper()
if s.find(i)%2!=0:
r+=i.upper()
elif i.islower():
if s.find(i)%3==0:
r+=i.upper()
if s.find(i)%3!=0:
r+=i.upper()
elif i.isdigit():
p=int(i)
if p%2==0:
p=0
else:
p=1
r+=str(p)
else:
r=r+i
print(r)
Que. 15.
s="Cbse
Exam 2019-2020"
l=['a','e','i','o','u','A','E','I','O','U']
print(s)
r=""
for i in s:
p=s.find(i)
if i in l:
r+=str(l[p: ])
elif i
in l:
r+=str(l[p: ])
elif i.isdigit():
r+=str(l[0])
else:
r=r+i
print(r)
Que. 16.
s="Cbse
Exam 2019-2020"
l=['a','e','i','o','u','A','E','I','O','U']
print(s)
r=""
x=0
while x
<len(s):
i=s[x]
p=s.find(i)
if i in l:
r+=str(l[p: ])
elif i
in l:
r+=str(l[p: ])
elif i.isdigit():
r+=str(l[0])
else:
r=r+i
x+=1
print(r)
Que. 17.
s="Cbse
Exam 2019-2020"
l=s.split()
print(l)
print(len(l))
Que. 18.
s="Cbse
Exam 2019-2020"
l=s.split('a')
print(l)
print(len(l))
Que. 19.
s="Cbse
Exam 2019-2020"
l=s.split('a')
for i in l:
print(i," : ",len(i))
Que. 20.
s="Cbse
Exam 2019-2020"
l=s.split('a')
for i in l:
if len(i)>5:
print(i," : ",len(i))
******************
String Assignment
Que: 01.
T= "Mind@work!"
R=""
l=len(T)
print("Orignal : ",T)
for i in range(l):
if T[i].isalpha()==False:
R+='*'
elif T[i].isupper()==True:
R+=chr(ord(T[i])+1)
else:
R+= T[i+1]
print("Final : ",R)
Que: 02.
Mystring= "what@OUTPUT!"
l=len(Mystring)
R=""
print("Orignal :
",Mystring)
for i in range(l):
if Mystring[i].isalpha()==False:
R+="*";
elif Mystring[i].isupper():
R+=chr(ord(Mystring[i])+1)
else:
R+=Mystring[i+1]
print("Final :",Mystring)
Que: 03.
SMS="rEPorTmE";
l=len(SMS)
R=""
print("Orignal : ",SMS)
N=2
for c in range(l):
if c%2==0:
R+= chr(ord(SMS[c])+N)
elif SMS[c].isupper()==True:
R+=SMS[c].lower()
else:
R+=chr(ord(SMS[c])-N)
print("Final :",R)
Que: 04.
MyText="ApEACeDriVE"
ch="@"
l=len(MyText)
Msg=""
print("\n Orignal :
",MyText)
for cnt in range (l):
if MyText[cnt]>='B' and
MyText[cnt]<='G':
Msg+=MyText[cnt].lower()
elif MyText[cnt]=='A' or
MyText[cnt]=='a':
Msg+=ch;
else:
if(cnt%2==0):
Msg+=MyText[cnt].upper()
else:
Msg+=MyText[cnt-1]
print("Final :",Msg)
Que: 05.
Message="ArabSagar";
SMS=[ i for i in Message]
print("Orignal :
",Message)
l=len(SMS)
R=""
for L in range(l):
for C in range(0, int(L/2)):
if ch=='A' or ch=='E':
SMS[C]='#';
else:
Temp=SMS[C]
SMS[C]=SMS[L-C-1]
SMS[L-C-1]=Temp;
Message =''.join(SMS)
print("Final :",Message)
Que: 06.
s= "a ProFile"
print("Orignal : ",s)
l=len(s)
r=""
for x in range(l):
if s[x].islower():
r+=s[x].upper()
elif s[x].isupper():
if (x%2!=0):
r+=s[x-1].lower()
else:
r+=chr(ord(s[x])-1)
print(" Final : ",r)
Que: 07.
s = "admiNStrAtiOn"
print("Orignal : ",s)
l=len(s)
r=""
for x in range(l):
if s[x].islower():
r+=s[x].upper()
elif s[x].isupper():
if (x%2==0):
r+=s[x-1].lower()
else:
r+=chr(ord(s[x])-2)
print(" Final : ",r)
Que: 08.
poet= "SakESpHerE" ;
l=len(poet)
r=""
print("Orignal :
",poet)
for i in range(l):
if poet[i].islower():
r+=poet[i-1]
elif poet[i].isupper():
if poet[i]=='S':
r+='X';
elif(poet[i]=='E'):
r+=poet[i-1].upper()
else:
r+=chr(ord(poet[i])-1)
print(" Final : ",r)
Que: 09.
poet= "SakESpHerE" ;
l=len(poet)
r=""
print("Orignal :
",poet)
for i in range(l):
if poet[i].islower():
r+=poet[i-1]
elif poet[i].isupper():
if poet[i]=='S':
r+='X';
elif(poet[i]=='E'):
r+=poet[i-1].upper()
else:
r+=chr(ord(poet[i])-1)
print(" Final : ",r)
Que: 10.
poet= "SakESpHerE" ;
l=len(poet)
r=""
print("Orignal :
",poet)
for i in range(l):
if poet[i].islower():
r+=poet[i-2]
elif poet[i].isupper():
if poet[i]=='S':
r+='X';
elif(poet[i]=='E'):
r+=poet[i-2].upper()
else:
r+=chr(ord(poet[i])-2)
print(" Final : ",r)
Que: 11.
T="SaVE EArtH";
l=len(T)
r=""
print("Orignal : ",T)
for i in range(0,l,2):
if (T[i]=='A' or T[i]=='E'):
r+='#';
elif T[i].islower():
r+=T[i].upper();
else:
r+='@'
print("Final : ",r)
Que: 12.
T="SaVE EArtH";
l=len(T)
r=""
print("Orignal : ",T)
for i in range(0,l):
if (T[i]=='A' or T[i]=='E'):
r+='#';
elif T[i].islower():
r+=T[i].upper();
else:
r+='@'
print("Final : ",r)
Que: 13.
T="SaVE EArtH";
l=len(T)
r=""
print("Orignal : ",T)
for i in range(l):
if (T[i]=='A' or T[i]=='H'):
r+='@';
elif T[i].isupper():
r+=T[i].lower();
else:
r+='#'
print("Final : ",r)
Que: 14.
Note = "Butterfly";
NUM=2
l=len(Note )
r=""
print("Orignal : ",Note
)
for K in range(l):
if ( K % 2 == 0):
r+=chr(ord(Note[K])+NUM-1)
elif Note[K].islower():
r+=Note[K].upper()
else:
r+=chr(ord(Note[K])+NUM)
print("Final : ",r)
Que: 15.
Note = "Butterfly";
NUM=2
l=len(Note )
r=""
print("Orignal : ",Note
)
for K in range(l):
if ( K % 2 != 0):
r+=chr(ord(Note[K])+NUM-1)
elif Note[K].islower():
r+=Note[K].upper()
else:
r+=chr(ord(Note[K])+NUM)
print("Final : ",r)
Que: 16.
Note = "Butterfly";
NUM=2
l=len(Note )
r=""
print("Orignal : ",Note
)
for K in range(l):
if ( K % 2 != 0):
r+=chr(ord(Note[K])-NUM)
elif Note[K].islower():
r+=Note[K].upper()
else:
r+=chr(ord(Note[K])+NUM)
print("Final : ",r)
Que: 17.
N="ComPUteR";
l=len(N)
r=""
print("Orignal : ",N)
for x in range(l):
if N[x].islower():
r+=N[x].upper()
elif N[x].isupper():
if x%2==0:
r+=N[x].lower()
else:
r+=N[x-1]
print("Final : ",r)
Que: 18.
N="ComPUteR";
l=len(N)
r=""
print("Orignal : ",N)
for x in range(l):
if N[x].islower():
r+=N[x].upper()
else:
if N[x].isupper():
if x%2==0:
r+=N[x].lower()
else:
r+=N[x-1]
print("Final : ",r)
Que: 19.
N="ComPUteR";
l=len(N)
r=""
print("Orignal : ",N)
for x in range(l):
if N[x].islower():
r+=N[x].upper()
elif N[x].isupper():
if x%2!=0:
r+=N[x].lower()
else:
r+=N[x-2]
print("Final : ",r)
Que: 20.
name= "teAmIndia"
l=len(name)
r=""
print("Orignal :
",name)
for i in range(0,l,2):
if name[i].islower():
r+=name[i].upper()
else:
r+=name[i].lower()
print("Final : ",r)
Que: 21.
name= "teAmIndia"
l=len(name)
r=""
print("Orignal :
",name)
for i in range(0,l,3):
if name[i].islower():
r+=name[i].upper()
else:
r+=name[i].lower()
print("Final : ",r)
Que: 22.
name= "ThE bESt mEN wIN";
l=len(name)
r=""
print("Orignal :
",name)
for i in range(l):
if name[i].islower():
r+= name[i].upper()
elif name[i].isupper():
if i%2==0:
r+=chr(ord(name[i])-1)
else:
r+= name[i - 1].lower();
print("Final : ",r)
Que: 23.
name= "ThE bESt mEN wIN";
l=len(name)
r=""
print("Orignal :
",name)
for i in range(l):
if name[i].islower():
r+= name[i].upper()
elif name[i].isupper():
if i%2==0:
r+=chr(ord(name[i])-1)
else:
r+= name[i -
1].lower();
print("Final : ",r)
Que: 24.
name= "ThE bESt mEN wIN";
l=len(name)
r=""
print("Orignal :
",name)
for i in range(l):
if name[i].islower():
r+= name[i].upper()
else:
if name[i].isupper():
if i%2==0:
r+=chr(ord(name[i])-1)
else:
r+= name[i -
1].lower();
print("Final : ",r)
FILE
HANDLING BASIC PROGRAMS Set A Solution
1. WAP to read
and count character from data.txt
file :
i.
Character Upper/ Lower Character
ii.
Digit Character Characters/Symbol/ Spaces
iii.
Words/ Lines
iv.
Vowel /Consonant Character
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image003.jpg)
ANS:
i/ii:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
for ch in s:
if ch.isupper()==True: #ch.islower()/ch.isdigit()/ch.isspace/ch==’\n\
for line
count+=1 print(count)
words=count+1 #
After Space Count
print(words)
Symbol (Special Characters):
f1=open("data.txt","r")
s=f1.read()
count=0
for ch in s:
if ch.isalnum()==False:
count+=1
print(count)
iii:
Words
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
Line
f1=open("data.txt","r")
s=f1.readlines()
print(s)
print(s,”, ”,len(s))
iv:
f1=open("data.txt","r")
s=f1.read()
countV=0
countC=0
for ch in s:
if ch.isalnum()==True:
if ch=='a' or
ch=='e' or ch=='i' or ch=='o' or ch=='u':
countV+=1
else:
countC+=1
print("V: ",countV,", C= ", countC)
2.
WAF to read and count Words start
with from text file : Upper /Lower / Digits / Special / Vowel / Consonant Character
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word[0].isupper()==True: #same Q1 part 1st
count+=1
print(count)
3.
WAF to read
and count Words end with from text file : Upper /Lower / Digits /
Special / Vowel / Consonant/User Define
Character
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg)
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word[-1].isupper()==True: #same Q1 part 1st
count+=1
print(count)
4.
WAF to read and count Words start
and end with from text file : Upper /Lower /
Digits / Special / Vowel / Consonant/User Define Character
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word[-1].isupper()==True and word[0].isupper()==True:
#same
Q1 part 1st
count+=1
print(count)
5.WAF to count the word present in a text file DATA.TXT. Word: - like this/This, My, Me, He, She, to, the, do, Mr. and Mrs.
ANS:
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image006.jpg)
s=f1.read()
print(s)
sword=input(“Enter Your search Word: ”)
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word==sword:
count+=1
print(count)
FILE HANDLING FOR BOARD
EXAM
Q1.
WAF to count the number of character present data.txt as : ‘A’,
‘t’, ‘T’, ‘E’, ‘I’, ‘i’.
ANS:
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image007.jpg)
s=f1.read()
print(s)
count=0
sch=input(“Enter Any Character: ”)
for ch in s:
if ch==sch[0]: #sch[0]
read first character from input string
count+=1 print(count)
Q2.WAF to count the number of words in a text file ‘DATA.TXT’ which is started/ended with ‘A’, ‘t’, ‘T’, ‘E’, ‘I’, ‘i’.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words,”, ”,len(words))
for word in words:
if word[0]== ‘A’: # word[-1] for end
character/ endswith or startswith
method
count+=1
print(count)
Q3.WAF to count the number of lines in a text file ‘DATA.TXT’ which is started/ended with ‘A’, ‘t’, ‘T’, ‘E’, ‘I’, ‘i’.
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image008.jpg)
f1=open("data.txt","r")
lines=f1.readlines()
print("All Data of file in lines: \n",lines)
count=0
i=1
print("="*30)
for line in lines:
print("Line %d : %s
,length is : %d"%(i,line,len(line)))
i+=1
if line[0]=='t': # line[-1]=='t':
count+=1
print("Lines start with 't' is
",count)
Q4.WAF to count the number of words in
a text file ‘DATA.TXT’ which
is starting/ended with an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
ANS:
f1=open("data.txt","r")
s=f1.read()
print("All Data of file in
string : \n",s)
print("="*30)
count=0
words=s.split()
print("All Words:
",words,", length is
",len(words))
for word in words:
if
word.startswith("the")==True:
# word.endswith(“the”)
count+=1
print("Words start with 'the'
is ",count)
Q5.WAF to count the number of line in
a text file ‘DATA.TXT’ which
is starting/ended with an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
ANS:
f1=open("data.txt","r")
s=f1.read()
print("All Data of file in
string : \n",s)
print("="*30)
f1=open("data.txt","r")
lines=f1.readlines()
print("All Data of file in
lines: \n",lines)
count=0
i=1
print("="*30)
for line in lines:
print("Line %d : %s ,length is : %d"%(i,line,len(line)))
i+=1
if line.startswith("the")==True: # line.endswith(“the”)
count+=1
print("Lines start with 'the'
is ",count)
Q6.WAF to read data from a text
file DATA.TXT, and display those
words, which are less than 4 characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words,”, ”,len(words))
for word in words:
if len(word)==4:
print(word)
count+=1
print(count)
Q7.WAF to read data from a text
file DATA.TXT, and display words with number of characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
for word in words:
print(word,”, ”,len(word))
Q8.WAF to read data from a text
file DATA.TXT, and display each
words with number of vowels and consonants.
ANS:
![](file:///C:/Users/GURJEE~1/AppData/Local/Temp/msohtmlclip1/01/clip_image009.jpg)
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,”, ”,len(words))
for word in words:
countV=0
countC=0
for ch in word:
if ch.isalnum()==True:
if ch=='a' or ch=='e' or ch=='i' or
ch=='o' or ch=='u':
countV+=1
else:
countC+=1
print("Word : ",word,",
V: ",countV,", C= ", countC)
Q9.WAF to read data from a text
file DATA.TXT, and display word
which have maximum number of vowels characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,",
",len(words))
maxV=0
final=""
for word in words:
countV=0
for ch in word:
if ch.isalnum()==True:
if ch=='a' or ch=='e' or ch=='i' or
ch=='o' or ch=='u':
countV+=1
if maxV<countV:
maxV=countV
final=word
print("Final :
",final,", maxV: ",maxV)
Q10.
WAF to read data from a text file DATA.TXT, and display word which have
maximum/minimum characters.
Ans:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
if maxC<length:
maxC=length
maxfinal=word
if minC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word :
",minfinal,", minC: ",minC)
Data Structure
All Programs
Program:1:
linear_search.py
x
=[1,2,3,5,7,89,3,62,34,13,5,7,9]
data = 89
found =0
for i in x:
if i ==data:
found =1
if(found ==0):
print("Data not found")
else:
print("Data found")
Program:2: ls.py
print("#------------Direct-----------")
d=[2,3,2,4,5,6,7,1,1,11,3,4,9]
e=int(input("e=
"))
c=d.count(e)
print(c)
if c>0:
print("Yes Present")
else:
print("Not Present")
print("#------------LS-----Indirect----------")
d=[2,3,2,4,5,6,7,1,1,11,3,4,9]
e=int(input("E=
"))
c=0
for i in d:
if i==e:
c+=1
if c>0:
print("Yes Present")
else:
print("Not Present")
Program:3: merge sorted unsorted.py
x =
[6,3,4,12,5,22]
y =
[2,15,16,8,1,19]
z = []
i=j=0
k=len(x)
m=len(y)
p=0
print("Array
x : ",x)
print("Array
y : ",y)
while
p<=(k+m):
if i<k:
z.append(x[i])
i = i+1
else:
if j<m:
z.append(y[j])
j = j+1
p=p+1
print("Array
After Merging : ",z)
for i in
range(1,len(z)):
for j in range(0,len(z)-i):
if(z[j]>z[j+1]):
t=z[j+1]
z[j+1]=z[j]
z[j]=t
print("Array
After Sorting : ",z)
Program:4: merge sorted.py
x =
[1,3,4,12,15,22]
y =
[2,5,6,8,10,19]
z = []
i=j=k=0
p=0
while 1:
print("i: ",i," j:
",j,"p:
",p,",x[",i,"]",x[i],",y[",j,"]",y[j])
print("List after round
:",p+1,": ",z)
if x[i]<=y[j]:
z.append(x[i])
i
= i+1
else:
z.append(y[j])
j = j+1
p+=1
if p>=(len(x)+len(x))-1:break
while
i<len(x):
z.append(x[i])
i = i+1
while
j<len(y):
z.append(y[j])
j= j+1
#display
concatenated list
print("\n
Merged array : ")
for i in z:
print(i,end=" ")
Program:5: merge.py
x =
[1,3,4,12,15,22]
y =
[2,5,6,8,10,19]
z = []
i=j=k=0
p=0
while 1:
print("i: ",i," j:
",j,"p:
",p,",x[",i,"]",x[i],",y[",j,"]",y[j])
print("List after round
:",p+1,": ",z)
if x[i]<=y[j]:
z.append(x[i])
i = i+1
else:
z.append(y[j])
j = j+1
p+=1
if p>=(len(x)+len(x))-1:break
while
i<len(x):
z.append(x[i])
i = i+1
while
j<len(y):
z.append(y[j])
j= j+1
#display
concatenated list
print("\n
Merged array : ")
for i in z:
print(i,end=" ")
Program:6: stack multo rec.py
#function to add
element at the end of list
def
push(stack,x):
stack.append(x)
print(x)
#function to
remove last element from list
def pop(stack):
n = len(stack)
if(n<=0):
print("Stack empty....Pop not
possible")
else:
stack.pop()
#function to
display stack entry
def
display(stack):
if len(stack)<=0:
print("Stack
empty...........Nothing to display")
for i in stack:
print(i,end=" ")
#main program
starts from here
x=[]
Item_name=""
Item_code=0
choice=0
while
(choice!=4):
print("\n\t\t\t stack menu \n\t1. push
\n\t2. pop \n\t3. Display \n\t4. Exit")
choice = int(input("Enter your
choice"))
if(choice==1):
Item_name=input("Enter Item_name=
")
Item_code=int(input("Enter
Item_code= "))
value=[Item_name,Item_code]
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this
program")
Program:7: stack.py
def
push(stack,x): #function to add
element at the end of list
stack.append(x)
def
pop(stack): #function to remove
last element from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not
possible")
else:
stack.pop()
def
display(stack): #function to display
stack entry
if len(stack)<=0:
print("Stack
empty...........Nothing to display")
for i in stack:
print(i,end=" ")
#main program
starts from here
x=[]
choice=0
while
(choice!=4):
print("********Stack
Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice
:"))
if(choice==1):
value = int(input("Enter value
"))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this
program")
"""
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :1
Enter value 23
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :1
Enter value 43
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :1
Enter value 55
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :1
Enter value 11
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :3
23 43 55 11
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :2
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :3
23 43 55
********Stack
Menu***********
1. push(INSERT)
2. pop(DELETE)
3. Display
4. Exit
Enter your
choice :
"""
Program:8: queue multi rec.py
#function to add
element at the end of list
def
add_element(Queue,x):
Queue.append(x)
#function to
remove last element from list
def
delete_element(Queue):
n = len(Queue)
if(n<=0):
print("Queue empty....Deletion not
possible")
else:
del(Queue[0])
#function to
display Queue entry
def
display(Queue):
if
len(Queue)<=0:
print("Queue
empty...........Nothing to display")
for i in Queue:
print(i,end=" ")
#main program
starts from here
x=[]
Item_name=""
Item_code=0
choice=0
while
(choice!=4):
print("\n\tQueue menu \n\n\t1. Add
Element \n\t2. Delete Element \n\t3. Display \n\t4. Exit")
choice = int(input("\n Enter your
choice : "))
if(choice==1):
Item_name=input("Enter Item_name=
")
Item_code=int(input("Enter
Item_code= "))
value=[Item_name,Item_code]
add_element(x,value)
if(choice==2):
delete_element(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this
program")
Program:9: queue.py
def
add_element(Queue,x): #function to add element
at the end of list
Queue.append(x)
def
delete_element(Queue): #function to
remove last element from list
n = len(Queue)
if(n<=0):
print("Queue empty....Deletion not
possible")
else:
del(Queue[0])
def display(Queue): #function to display Queue entry
if len(Queue)<=0:
print("Queue
empty...........Nothing to display")
for i in Queue:
print(i,end=" ")
#main program
starts from here
x=[]
choice=0
while (choice!=4):
print(" ********Queue
menu***********")
print("1. Add Element ")
print("2. Delete Element")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice
: "))
if(choice==1):
value = int(input("Enter value :
"))
add_element(x,value)
if(choice==2):
delete_element(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this
program")
"""
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your
choice : 1
Enter value : 2
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your
choice : 1
Enter value : 33
********Queue menu***********
1. Add Element
2. Delete Element
3. Display
4. Exit
Enter your
choice : 1
Enter value : 41
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your
choice : 1
Enter value : 67
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your
choice : 3
2 33 41 67
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your
choice : 2
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your choice
: 3
33 41 67
********Queue menu***********
1. Add Element
2. Delete
Element
3. Display
4. Exit
Enter your
choice :
"""
Program:10: concate.py
x
=[3,56,2,56,78,56,34,23,4,78,8,123,45]
y = [23,2,3,4]
# z=[]
# for i in x:
# z.append(i)
# for i in y:
# z.append(i)
z= x+y
#display
concatenated list
for i in z:
print(i,end=" ")
Program:11:
binary_search.py
x
=[1,2,3,5,7,8,9,10,12,14,15,18,20,22]
data = 89
found =0
first=0
last =13
while
(first<=last and found ==0):
mid = int((first+last)/2)
# print(mid)
if(x[mid]==data):
found =1
if(x[mid]<data):
first=mid+1
if(x[mid]>data):
last = mid-1
if(found ==0):
print("Data not found")
else:
print("Data found")
Program:12: BS.py
#------------BS---------------
DATA=[1,2,3,4,5,6,7,8,9,11,13,14,19,45]
e=int(input("e=
")) #12
length=len(DATA)
B=0
E=length-1
Mid=0
f=0
while B<=E:
x=(B+E)/2
Mid=int(x)
print("Mid: ",Mid,", B:
",B,", E: ",E)
if DATA[Mid]==e:
f=1
break
if DATA[Mid]>e:E=Mid-1
if DATA[Mid]<e:B=Mid+1
if
f==1:print("Yes Present")
else: print("Not Present")
Program:13: insert item in
end.py
#----insert in
end---------------
DATA=[1,2,3,4,5,6,7]
print(DATA)
length=len(DATA)
print(length)
e=int(input("e=
")) #12
DATA.append(None)
length=len(DATA)
print(length)
print(DATA)
DATA[length-1]=e
print(DATA)
Program:14: insert item in
beg.py
#--------insert
in beg--------
DATA=[1,2,3,4,5,6,7]
print(DATA)
length=len(DATA)
print(length)
e=int(input("e=
")) #12
DATA.append(None)
length=len(DATA)
for i in
range(length-1,0,-1):
DATA[i]=DATA[i-1]
print(DATA)
DATA[0]=e
length=len(DATA)
print(length)
print(DATA)
Program:15: insert item in
pos.py
#--------insert
in POS--------
DATA=[1,2,3,4,5,6,7]
print(DATA)
le=len(DATA)
print(le)
e=int(input("e=
")) #12
pos=int(input("pos=
")) #4
DATA.append(None)
le=len(DATA)
for i in
range(le-1,pos,-1):
DATA[i]=DATA[i-1]
print(DATA)
DATA[pos]=e
le=len(DATA)
print(le)
print(DATA)
Program:16: insert item in
sorted list.py
#---insert in
sorted list--------
DATA=[1,2,3,4,6,7]
print(DATA)
le=len(DATA)
e=int(input("e=
")) #5
pos=0
DATA.append(None)
le=len(DATA)
for i in
range(le):
if DATA[i]>e:
pos=i
break
print(pos)
for i in
range(le-1,pos,-1):
DATA[i]=DATA[i-1]
DATA[pos]=e
length=len(DATA)
print(DATA)
Program:17: bubble_sort
sss.py
import time
#------------------method
1 without temp--------------
x
=[3,56,2,156,78,56,34,23,41,78,8,123,45]
print("Array
Before Sorted : ",x)
for i in
range(1,len(x)):
for j in range(0,13-i):
if(x[j]>x[j+1]):
x[j],x[j+1] = x[j+1],x[j]
print("Array
After Sorted : ",x)
t=x[j+1]
x[j+1]=x[j]
x[j]=t
Program:18: bubble_sort.py
import time
#------------------method
1 without temp--------------
x
=[3,56,2,156,78,56,34,23,41,78,8,123,45]
print("Array
Before Sorted : ",x)
for i in
range(1,len(x)):
print("*****************(%d)*************************"%i)
c=1
for j in range(0,13-i):
if(x[j]>x[j+1]):
x[j],x[j+1] = x[j+1],x[j]
time.sleep(0.200)
print("%2d"%c,":",x)
c+=1
print("%2d"%i,":",x)
time.sleep(0.900)
print("Array
After Sorted : ",x)
#------------------method
2 using TEMP--------------
x
=[3,56,2,156,78,56,34,23,41,78,8,123,45]
print("Array
Before Sorted : ",x)
for i in
range(1,len(x)):
for j in range(0,len(x)-i):
if(x[j]>x[j+1]):
t=x[j+1]
x[j+1]=x[j]
x[j]=t
print("Process :%2d"%j,":",x)
print("%2d"%i,":",x)
print("******************************************")
time.sleep(0.900)
time.sleep(0.900)
print("Process
:%2d"%i,":",x)
print("Array
After Sorted : ",x)
Program:19:
selection_sort.py
#--------------method
1 wethout temp-----------
x
=[3,5,2,17,78,58,34,23,4,18,8,123,45]
print("Array
Before Sorted : ",x)
for i in
range(0,12):
pos = i
low = x[pos]
for j in range(i+1,13):
if(low>x[j]):
pos = j
low = x[j]
x[i],x[pos] = x[pos],x[i]
print("Process
:%2d"%i,":",x)
print("Array
After Sorted : ",x)
#--------------method 2 with temp--------------
x
=[3,5,2,17,78,58,34,23,4,18,8,123,45]
print("Array
Before Sorted : ",x)
for i in
range(0,12):
pos = i
low = x[pos]
for j in range(i+1,13):
if(low>x[j]):
pos = j
low = x[j]
t=x[i]
x[i]=x[pos]
x[pos]=t
print("Process
:%2d"%i,":",x)
print("Array
After Sorted : ",x)
Program:20: insertion_sort.py
x
=[3,56,2,56,78,56,34,23,4,78,8,123,45]
print("Array
Before Sorted : ",x)
for i in
range(1,13):
j= i-1
temp = x[i]
while(temp<x[j] and j>=0):
x[j+1]=x[j]
j = j-1
x[j+1] = temp
print("Process
:%2d"%i,":",x)
print("Array
After Sorted : ",x)