You are on page 1of 15

P1 : PRINT FUNCTION

print("welcome") P4:ASSIGNMENT OPERATORS


a=10 a=10
print("Area = ",a*a) b=3
print("AECS\nJADUGUDA") a+=b
print("Addition = ",a)
OUTPUT a-=b
welcome print("Subtraction = ",a)
Area = 100 a*=b
AECS print("Multiplication = ",a)
JADUGUDA a/=b
print("Division = ",a)
a//=b
P2:ARITHMETIC OPERATORS print("Floor Division = ",a)
a=10 a%=b
b=3 print("Modulus = ",a)
print("Addition = ",a+b) a**=b
print("Subtraction = ",a-b) print("Exponentiation = ",a)
print("Multiplication = ",a*b)
print("Division = ",a/b) OUTPUT
print("Floor Division = ",a//b)
print("Modulus = ",a%b) Addition = 13
print("Exponentiation = ",a**b) Subtraction = 10
Multiplication = 30
OUTPUT Division = 10.0
Addition = 13 Floor Division = 3.0
Subtraction = 7 Modulus = 0.0
Multiplication = 30 Exponentiation = 0.0
Division = 3.3333333333333335
Floor Division = 3
Modulus = 1 P5:RELATIONAL OPERATORS
Exponentiation = 1000
a=10
P3:DATA TYPES b=3
print(type(10)) print("Greater than = ",a>b)
print(type(10.5)) print("Greater than or equal= ",a>=b)
print(type("AECS-1")) print("Less than = ",a<b)
print(type(True)) print("Less than or equal = ",a<=b)
print(type(None)) print("Equality = ",a==b)
print("Inequality = ",a!=b)
OUTPUT
<class 'int'> OUTPUT
<class 'float'>
<class 'str'> Greater than = True
<class 'bool'> Greater than or equal= True
<class 'NoneType'> Less than = False
Less than or equal = False
Equality = False
Inequality = True

Pawan Kumar,PGT,AECS-1 Jaduguda Page 1


LOG 2.302585092994046
LOG 10 1.0
P6:LOGICAL OPERATORS POWER 32.0
P9: BUILT IN FUNCTIONS
a=10
b=3 print("ABSOLUTE VALUE ",abs(10),abs(-10))
print(" AND operator = ",a and b) print("BINARY VALUE ",bin(15))
print(" OR Operator = ",a or b) print("OCTAL VALUE ",oct(15))
print(" NOT Operator = ",not a) print("HEXA VALUE ",hex(15))
print("BOOLEAN VALUE ",bool('0'),bool(0))
OUTPUT print("CHARECTER ",chr(65),chr(97),chr(48))
print("ORDINAL VALUE ",ord('A'),ord('a'),ord('0'))
AND operator = 3 print("DIVISIOIN MODULUS ",divmod(5,2))
OR Operator = 10 print("EVALUATE ",eval('10+6'))
NOT Operator = False print("MAXIMUM ",max(10,20,5))
print("MINIMUM ",min(10,20,5))
P7: BITWISE OPERATORS print("ROUND ",round(10.54657,2))

a=10
b=3 OUTPUT
print(" bitwise AND operator = ",a & b)
print(" bitwise OR Operator = ",a | b) ABSOLUTE VALUE 10 10
print(" bitwise XOR Operator = ",a ^ a) BINARY VALUE 0b1111
print(" bitwise complement = ",~a) OCTAL VALUE 0o17
HEXA VALUE 0xf
OUTPUT BOOLEAN VALUE True False
CHARECTER Aa0
bitwise AND operator = 2 ORDINAL VALUE 65 97 48
bitwise OR Operator = 11 DIVISIOIN MODULUS (2, 1)
bitwise XOR Operator = 0 EVALUATE 16
bitwise complement = -11 MAXIMUM 20
MINIMUM 5
P8: MATH FUNCTIONS ROUND 10.55

import math P10: ESCAPE SEQUENCE


print("CEIL ",math.ceil(10.3))
print("FLOOR ",math.floor(10.3)) print("TC\\BIN")
print("SQRT ",math.sqrt(25)) print("Adam\'s Apple")
print("EXPONENT ",math.exp(2)) print("He said\"hai\"")
print("FLOAT ABS ",math.fabs(-1.4)) print("HELLO\n FRIENDS")
print("LOG ",math.log(10))
print("LOG 10 ",math.log10(10)) OUTPUT
print("POWER ",math.pow(2,5))
TC\BIN
OUTPUT Adam's Apple
CEIL 11 He said"hai"
FLOOR 10 HELLO
SQRT 5.0 FRIENDS
EXPONENT 7.38905609893065
FLOAT ABS 1.4

Pawan Kumar,PGT,AECS-1 Jaduguda Page 2


P11: AREA AND PERIMETER OF A RECTANGLE
OUTPUT
l=int(input("Enter length "))
b=int(input("Enter bread ")) Enter five nos
print("Area ",l*b) 12
print("Perimeter ",2*(l+b)) 34
98
OUTPUT 7
35
Enter length 10 sum = 186 Average 37.2
Enter bread 4
Area 40 P15: SWAP USING THIRD VARIABLE
Perimeter 28
a=int(input("Enter a no"))
P12: FAHRENHEIT TO CELSIUS b=int(input("Enter another no"))
print("Before swap :",a,b)
f=float(input("Enter Fahrenheit : ")) t=a
print("Celsius :",(f-32)*5/9) a=b
b=t
OUTPUT print("After swap :",a,b)
Enter Fahrenheit : 104
Celsius : 40.0 OUTPUT

Enter a no10
P13: SIMPLE INTEREST Enter another no20
Before swap : 10 20
p=int(input("Enter Principal : ")) After swap : 20 10
n=int(input("No of years : "))
r=float(input("Rate of interest :")) P16: SWAP WITHOUT USING THIRD VARIABLE
print("Simple interest :",p*n*r/100)
a=int(input("Enter a no"))
OUTPUT b=int(input("Enter another no"))
print("Before swap :",a,b)
Enter Principal : 10000 a=a+b
No of years : 2 b=a-b
Rate of interest :12 a=a-b
Simple interest : 2400.0 print("After swap :",a,b)

P14: SUM AND AVERAGE OF FIVE NOS print("Before swap :",a,b)


a=a^b
print("Enter five nos") b=a^b
s=0 a=a^b
for i in range(5): print("After swap :",a,b)
a=int(input())
s+=a OUTPUT
print("sum =",s,"Average ",s/5)

Pawan Kumar,PGT,AECS-1 Jaduguda Page 3


Enter a no10 Divisible
Enter another no20 Enter a no21
Before swap : 10 20 Not Divisible
After swap : 20 10
Before swap : 20 10
After swap : 10 20
P17: ODD OR EVEN
P20: BIGGER OF TWO NUMBERS
n=int(input("Enter a no"))
if(n%2): a=int(input("Enter a no"))
print("Odd") b=int(input("Enter another no"))
else: if(a>b):
print("Even") print(a," is Bigger")
elif(a<b):
print(b," is Bigger")
OUTPUT else:
print("Both are equal")
Enter a no20
Even OUTPUT

P18: POSITIVE ,NEGATIVE OR ZERO Enter a no30


Enter another no40
n=int(input("Enter a no")) 40 is Bigger
if(n>0):
print("positive") P21: BIGGEST OF THREE NUMBERS
elif(n<0):
print("Negative") a=int(input("Enter a no"))
else: b=int(input("Enter another no"))
print("Zero") c=int(input("Enter third no"))
if(a>b and a>c):
print(a," is the Biggest")
OUTPUT elif(b>c):
print(b," is the Biggest")
Enter a no20 else:
positive print(c," is the Biggest")
Enter a no-4
Negative OUTPUT
Enter a no10
Enter another no30
P19: DIVISIBLE BY 5 OR NOT Enter third no20
30 is the Biggest
n=int(input("Enter a no"))
if(n%5==0):
print("Divisible") P22: SMALLER OF FIVE NUMBERS
else:
print("Not Divisible") print("Enter five nos")
small=99999
OUTPUT for i in range(5):
a=int(input())
Enter a no20 if a<small:

Pawan Kumar,PGT,AECS-1 Jaduguda Page 4


small=a LOWER CASE
print("smallest = ",small) Enter a charecter:6
DIGIT
Enter a charecter:%
OTHER CHARECTER

OUTPUT
P25: QUADRATIC EQUATION
Enter five nos
23 import math
9 a=int(input("Enter a no"))
99 b=int(input("Enter another no"))
20 c=int(input("Enter third no"))
18 d=b*b-4*a*c
smallest = 9 if d>0:
r1=(-b+math.sqrt(d))/(2*a)
P23: SORT THREE NUMBERS IN ASCENDING ORDER r2=(-b-math.sqrt(d))/(2*a)
print("The real and different roots are ",r1,r2)
a=int(input("Enter a no")) elif d==0:
b=int(input("Enter another no")) r1=r2=(-b+math.sqrt(d))/(2*a)
c=int(input("Enter third no")) print("The real and equal roots are ",r1,r2)
print(min(a,b,c),a+b+c-min(a,b,c)- else:
max(a,b,c),max(a,b,c)) print("imaginary roots")

OUTPUT OUTPUT

Enter a no30 Enter a no2


Enter another no10 Enter another no4
Enter third no20 Enter third no2
10 20 30 The real and equal roots are -1.0 -1.0

P24: LOWER CASE UPPER CASE , DIGIT P26: LEAP YEAR

a=input("Enter a charecter:") y=int(input("Enter year"))


if a.isupper(): f=0
print("UPPER CASE") if y%4==0:
elif a.islower(): f=1
print("LOWER CASE") if y%100==0:
elif a.isdigit(): f=0
print("DIGIT") if y%400==0:
else: f=1
print("OTHER CHARECTER") if f==1:
print("Leap year")
OUTPUT else:
print("Not a Leap Year")
Enter a charecter:E
UPPER CASE
Enter a charecter:e OUTPUT

Pawan Kumar,PGT,AECS-1 Jaduguda Page 5


Enter year1900 a=int(input("Enter a no"))
Not a Leap Year for i in range(2,a):
if a%i==0:
Enter year2000 print("Not a prime ")
Leap year break
else:
Enter year2001 print("Prime ")
Not a Leap Year

P27: FACTORS OF A NUMBER


OUTPUT
a=int(input("Enter a no"))
for i in range(1,a//2+1): Enter a no13
if a%i==0: Prime
print(i,"is a factor") Enter a no15
Not a prime
OUTPUT
P30: SUM OF DIGITS OF A NUMBER
Enter a no100
1 is a factor n=int(input("Enter a no"))
2 is a factor s=0
4 is a factor while n>0:
5 is a factor s=s+n%10
10 is a factor n//=10
20 is a factor print("sum = ",s)
25 is a factor
50 is a factor OUTPUT

P28: PERFECT OR NOT Enter a no1234


sum = 10
a=int(input("Enter a no"))
s=0 P31: REVERSE A NUMBER
for i in range(1,a):
if a%i==0: n=int(input("Enter a no"))
s=s+i s=0
if s==a: while n>0:
print("Perfect no") s=s*10+n%10
else: n//=10
print("Not a perfect no") print("Reversed Number = ",s)

OUTPUT OUTPUT

Enter a no28 Enter a no1234


Perfect no Reversed Number = 4321
Enter a no30
Not a perfect no P32: GCD & LCM OF TWO NUMBERS

P29: PRIME OR NOT def gcd(a,b):


for i in range(1,min(a,b)+1):

Pawan Kumar,PGT,AECS-1 Jaduguda Page 6


if a%i==0 and b%i==0: print()
gcd=i
return gcd OUTPUT
*****
a=int(input("Enter a no")) *****
b=int(input("Enter another no")) *****
print("Gcd = ",gcd(a,b)) *****
print("Lcm = ",a*b//gcd(a,b)) *****

OUTPUT
P35: PRINT PETTERN 2
Enter a no25
Enter another no35 for i in range(5):
Gcd = 5 for j in range(i+1):
Lcm = 175 print('*',end="")
print()
P33: GCD & LCM OF THREE NUMBERS OUTPUT
*
def gcd(a,b): **
for i in range(1,min(a,b)+1): ***
if a%i==0 and b%i==0: ****
gcd=i *****
return gcd
P36: PRINT PETTERN 3
a=int(input("Enter a no"))
b=int(input("Enter another no")) for i in range(5):
c=int(input("Enter third no")) for k in range(5,i,-1):
gcd2=gcd(a,b) print(' ',end="")
gcd3=gcd(gcd2,c) for j in range(i+1):
lcm2=a*b//gcd2 if j==0 or j==i:
lcm3=lcm2*c//gcd(c,lcm2) print('* ',end="")
print("Gcd = ",gcd3) else:
print("Lcm = ",lcm3) print(' ',end="")
print()
OUTPUT
OUTPUT
Enter a no25 *
Enter another no35 * *
Enter third no100 * *
Gcd = 5 * *
Lcm = 700 * *

P34:PRINT PETTERN 1 P37: PRINT PETTERN 4

for i in range(5): for i in range(1,6):


for j in range(5): for j in range(1,i+1):
print('*',end="") print(j,end="")

Pawan Kumar,PGT,AECS-1 Jaduguda Page 7


print()
17
OUTPUT aecs-1, jaduguda Welcomes you
1 aecs-1, jaduguda aecs-1, jaduguda
12 jadu
123 Aecs-1, jaduguda
1234 8
12345 False
True
True
True
False
True
aecs-1, jaduguda
P38: PRINT PETTERN 5 AECS-1, JADUGUDA
for i in range(5): jaduguda
c=65 aecs-1, jadug
for j in range(i+1):
print(chr(c),end="") P40: UPPER,LOWER,DIGIT ETC.
c=c+1
print() s=input('Enter a line of text')
u=l=d=c=0
OUTPUT for i in range(len(s)):
A if s[i].isupper():
AB u+=1
ABC elif s[i].islower():
ABCD l+=1
ABCDE elif s[i].isdigit():
d+=1
P39: STRING FUNCTIONS else:
c+=1
s='aecs-1, jaduguda ' print("Lower case =",l)
print(len(s)) print("Upper case =",u)
print(s+' Welcomes you') print("Digits =",d)
print(s*2) print("Other Charecters =",c)
print(s[8:12])
print(s.capitalize()) OUTPUT
print(s.find('jadu',0,20))
print(s.isalnum()) Enter a line of textAtomic Energy Central School-1,
print('school'.isalpha()) Jaduguda
print('123'.isdigit()) Lower case = 28
print(s.islower()) Upper case =5
print(s.isupper()) Digits =1
print(' '.isspace()) Other Charecters = 6
print(s.lower())
print(s.upper()) P41:NO OF OCCURANCES OF A STRING IN STRING
print(s.lstrip('aecs-1,'))
print(s.rstrip('uda ')) s=input('Enter a line of text :')
x=input('Enter text to be searched :')
OUTPUT start=0

Pawan Kumar,PGT,AECS-1 Jaduguda Page 8


end=len(s) continue
count=0 if i==0:
while start<=end: st=st+s[i].upper()
pos=s.find(x,start,end) elif s[i]!=' ':
if pos!=-1: st+=s[i]
count+=1 elif s[i]==' ':
start=pos+len(x) st=st+' '+s[i+1].upper()
else: flag=1
break print(st)
print("No of occurances = ",count)
OUTPUT
OUTPUT
Enter a line of text :Jingle Bell Jingle Bell Jingle all the Enter a line of Text :atomic energy central school
way Atomic Energy Central School
Enter text to be searched :Bell
No of occurances = 2
P42 : PALINDROME CHECK OF A STRING
P45: LONGEST SUBSTRING HAVING ONLY
s=input('Enter a string :') CONSONANTS
for i in range(len(s)//2+1):
if s[i]!=s[-i-1]: s=input('Enter a string :')
print("Not a Palindrome") max=0
break ss=''
else: flag=0
print("Palindrome") for i in range(len(s)):
if s[i]!=' ':
OUTPUT ss+=s[i]
if s[i]==' ' or i==len(s)-1:
Enter a string :malayalam for j in ss:
Palindrome if j in 'aeiou':
ss=''
P43 : NO OF VOWELS IN A STRING continue
s=input('Enter a string :') if len(ss)>=max:
v=0 max=len(ss)
for i in s: maxs=ss
if i in 'aeiou' or i in 'AEIOU': ss=''
v+=1 print(maxs)
print("Vowels = ",v)
OUTPUT
OUTPUT
Enter a string :Education Enter a string :the rhythm is melodious
Vowels = 5 Rhythm

P44: CAPITALISES FIRST CHARACTER OF EACH WORD P46:CAPITALISE ALTERNATE CHARECTERS


ss=input('Enter a line of Text :')
st='' s=input('Enter a string :')
flag=0 ss=''
for i in range(len(s)): for i in range(len(s)):
if flag==1: if i%2==0:
flag=0 ss=ss+s[i].upper()

Pawan Kumar,PGT,AECS-1 Jaduguda Page 9


else: [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
ss=ss+s[i] [10, 20]
print(ss) 2
[10, 20, 30, 40, 50, 60]
OUTPUT [10, 20, 30, 40, 50, 60, 5, 15, 20, 25, 30]
[10, 20, 25, 30, 40, 50, 60, 5, 15, 20, 25, 30]
Enter a string :jaduguda [10, 20, 25, 40, 50, 60, 5, 15, 20, 25, 30]
JaDuGuDa [10, 20, 25, 40, 60, 5, 15, 20, 25, 30]
[]
2
[30, 25, 20, 15, 5, 60, 40, 25, 20, 10]
Maximum = 60
Minumum = 5
[5, 10, 15, 20, 20, 25, 25, 30, 40, 60]

P47: DEMONSTRATION OF LIST


P48: MAX MIN FROM LIST
list1=[10,20,30,40,50]
list2=[5,15,20,25,30] l=[10,-89,67,-19,4,44,15]
print(list1+list2) max=0
print(list1*2) min=99999
print(list1[:2]) for i in l:
print(list1.index(30)) if i>=max:
list1.append(60) max=i
print(list1) if i<min:
list1.extend(list2) min=i
print(list1) print(l)
list1.insert(2,25) print('Maximum ',max)
print(list1) print('Minimum ',min)
list1.pop(3)
print(list1) OUTPUT
list1.remove(50)
print(list1) [10,-89,67,-19,4,44,15]
list2.clear() Maximum 67
print(list2) Minimum -89
print(list1.count(20))
list1.reverse() P49: MEAN FROM A LIST
print(list1)
print("Maximum = ",max(list1)) l=eval(input('Enter a list :'))
print("Minumum = ",min(list1)) s=0
list1.sort() for i in l:
print(list1) s+=i
print(l)
OUTPUT print('Mean = ',s/len(l))

[10, 20, 30, 40, 50, 5, 15, 20, 25, 30] OUTPUT

Pawan Kumar,PGT,AECS-1 Jaduguda Page 10


cnt=l.count(i)
Enter a list :[43,49,35,29,41,38,43] if cnt==1:
[43, 49, 35, 29, 41, 38, 43] u.append(i)
Mean = 39.714285714285715 else:
if i not in d:
P50: SEARCH AN ELEMENT IN A LIST d.append(i)

l=eval(input('Enter a list :')) print(l)


x=int(input('Enter an element to be searched:')) print(u)
if x in l: print(d)
print('Found')
else: OUTPUT
print('Not Found')
print('ANOTHER WAY') [2, 5, 2, 6, 1, 3, 2, 5, 2, 2, 7, 9]
for i in l: [6, 1, 3, 7, 9]
if x==i: [2, 5]
print('Found')
break
else:
print('Not found')
OUTPUT
P53: SECOND MAX FROM A LIST
Enter a list :[12,56,98,34,81]
Enter an element to be searched:98 l=[21,52,32,-6,13,38,15,61,25,17]
Found max=l[0]
ANOTHER WAY smax=0
Found for i in range(1,len(l)):
if l[i]>max:
P51: FREQUENCY OF AN ELEMENT IN A LIST smax=max
max=l[i]
l=eval(input('Enter a list :')) elif l[i]>smax:
x=int(input('Enter an element to be searched:')) smax=l[i]
count=0 print(l)
for i in l: print("Second max :",smax)
if x==i:
count+=1 OUTPUT
print("Frequency ",count)
[21, 52, 32, -6, 13, 38, 15, 61, 25, 17]
OUTPUT Second max : 52

Enter a list :[2,5,2,6,1,3,2,5,2,2,7,9] P54: SHIFT ZEROES TO RIGHT OF LIST


Enter an element to be searched:2
Frequency 5 l=[2,1,0,5,0,0,3,6,1,8,4,2,0,7]
print(l)
P52: UNIQUE AND DUPLICATION ELEMENTS IN LIST n=len(l)
z=l.count(0)
l=[2,5,2,6,1,3,2,5,2,2,7,9] count=0
u=[] while True:
d=[] l.remove(0)
for i in l: count+=1

Pawan Kumar,PGT,AECS-1 Jaduguda Page 11


if count==z: Count = 2
break
for i in range(count): P56 : SECOND MAX IN A TUPLE
l.append(0)
t=(21,52,32,-6,13,38,15,61,25,17)
print(l) max=t[0]
smax=0
OUTPUT for i in range(1,len(t)):
if t[i]>max:
[2, 1, 0, 5, 0, 0, 3, 6, 1, 8, 4, 2, 0, 7] smax=max
[2, 1, 5, 3, 6, 1, 8, 4, 2, 7, 0, 0, 0, 0] max=t[i]
print(t)
print("Second max :",smax)

OUTPUT

(21, 52, 32, -6, 13, 38, 15, 61, 25, 17)
Second max : 52

P55 : DEMONSTRATE TUPLE P57: DEMONSTRATE DICTIONARY

tuple1=(10,20,30,20,50) import json


tuple2=(5,15,20,25,30) d={"python":500,"C++":400,"Java":450,"Oracle":800}
print(tuple1) print(d)
print(tuple2) print("Length :",len(d))
print(tuple1+tuple2) for i in d:
print(tuple1*2) print(i,"Book costs Rs.",d[i])
print(tuple1[:2]) l=list(d.keys())
print(tuple1==tuple2) print(l)
print("index = ",tuple1.index(30)) t=tuple(d.values())
print("Length = ",len(tuple1)) print(t)
print("Maximum = ",max(tuple1)) d['Java']=550
print("Minumum = ",min(tuple1)) print(d)
print("Count = ",tuple1.count(20)) d['HTML']=250
print(d)
OUTPUT del d['Oracle']
print(d)
(10, 20, 30, 20, 50) d.pop('C++')
(5, 15, 20, 25, 30) print(d)
(10, 20, 30, 20, 50, 5, 15, 20, 25, 30) print(d.pop('Spider','Book Not found'))
(10, 20, 30, 20, 50, 10, 20, 30, 20, 50) print(json.dumps(d,indent=2))
(10, 20) print(d.get('python'))
False seq=d.items()
index = 2 for k,v in seq:
Length = 5 print(k,v)
Maximum = 50 d1={"FORTRAN":300,"COBOL":275}
Minumum = 10 d.update(d1)

Pawan Kumar,PGT,AECS-1 Jaduguda Page 12


print(d)
Name : LENIN
OUTPUT subject(key) Marks(value)
1 49
{'python': 500, 'C++': 400, 'Java': 450, 'Oracle': 800} 2 39
Length : 4 3 38
python Book costs Rs. 500
C++ Book costs Rs. 400 Name : CHE
Java Book costs Rs. 450 subject(key) Marks(value)
Oracle Book costs Rs. 800 1 35
['python', 'C++', 'Java', 'Oracle'] 2 48
(500, 400, 450, 800) 3 41
{'python': 500, 'C++': 400, 'Java': 550, 'Oracle': 800}
{'python': 500, 'C++': 400, 'Java': 550, 'Oracle': 800, P59: BUBBLE SORT
'HTML': 250}
{'python': 500, 'C++': 400, 'Java': 550, 'HTML': 250} l=[12,34,-78,4,31,-9,29,45,11,-32,30]
{'python': 500, 'Java': 550, 'HTML': 250} print(l)
Book Not found last=len(l)
{ for i in range(last):
"python": 500, for j in range(last-i-1):
"Java": 550, if l[j]>l[j+1]:
"HTML": 250 l[j],l[j+1]=l[j+1],l[j]
} print(l)
500
python 500 OUTPUT
Java 550
HTML 250 [12, 34, -78, 4, 31, -9, 29, 45, 11, -32, 30]
{'python': 500, 'Java': 550, 'HTML': 250, 'FORTRAN': [-78, -32, -9, 4, 11, 12, 29, 30, 31, 34, 45]
300, 'COBOL': 275}
P60: SELECTION SORT
P58:NESTED DICTIONARY
l=[12,34,-78,4,31,-9,29,45,11,-32,30]
d1={1:40,2:38,3:42} print(l)
d2={1:49,2:39,3:38} last=len(l)
d3={1:35,2:48,3:41} for i in range(last):
d4={'KARL ':d1,'LENIN ':d2,'CHE ':d3} for j in range(i+1,last):
for i in d4.keys(): if l[i]>l[j]:
print("Name ",i) l[i],l[j]=l[j],l[i]
print("subject(key)",'\t',"Marks(value)") print(l)
for j in d4[i].keys():
print(" ",j,'\t\t',d4[i][j]) OUTPUT
print()
[12, 34, -78, 4, 31, -9, 29, 45, 11, -32, 30]
OUTPUT [-78, -32, -9, 4, 11, 12, 29, 30, 31, 34, 45]

Name : KARL P60: INSERTION SORT


subject(key) Marks(value)
1 40 l=[12,34,-78,4,31,-9,29,45,11,-32,30]
2 38 print(l)
3 42 last=len(l)

Pawan Kumar,PGT,AECS-1 Jaduguda Page 13


for i in range(1,last): count+=1
key=l[i] print(l)
j=i-1 count+=1
while j>=0 and key<l[j]: last=len(l)
l[j+1]=l[j] count+=1
j=j-1 for i in range(last):
else: count+=1
l[j+1]=key for j in range(i+1,last):
print(l) count+=1
if l[i]>l[j]:
OUTPUT count+=1
l[i],l[j]=l[j],l[i]
[12, 34, -78, 4, 31, -9, 29, 45, 11, -32, 30] count+=1
[-78, -32, -9, 4, 11, 12, 29, 30, 31, 34, 45]
print(l)
count+=1
print("No of operations",count)

#INSERTION SORT
count=0
l=[34,88,56,9,-3,11,-19,71,39,21]
count+=1
print(l)
count+=1
last=len(l)
P62: NO OF OPERATIONS IN SORTING PGMS count+=1
for i in range(1,last):
#BUBBLE SORT count+=1
count=0 key=l[i]
l=[34,88,56,9,-3,11,-19,71,39,21] count+=1
count+=1 j=i-1
print(l) count+=1
count+=1 while j>=0 and key<l[j]:
last=len(l) count+=1
count+=1 l[j+1]=l[j]
for i in range(last): count+=1
count+=1 j=j-1
for j in range(last-i-1): else:
count+=1 count+=1
if l[j]>l[j+1]: l[j+1]=key
count+=1 print(l)
l[j],l[j+1]=l[j+1],l[j] count+=1
count+=1 print("No of operations",count)
print(l)
count+=1 OUTPUT
print("No of operations",count)
[34, 88, 56, 9, -3, 11, -19, 71, 39, 21]
#SELECTION SORT [-19, -3, 9, 11, 21, 34, 39, 56, 71, 88]
count=0 No of operations 111
l=[34,88,56,9,-3,11,-19,71,39,21] [34, 88, 56, 9, -3, 11, -19, 71, 39, 21]

Pawan Kumar,PGT,AECS-1 Jaduguda Page 14


[-19, -3, 9, 11, 21, 34, 39, 56, 71, 88]
No of operations 111
[34, 88, 56, 9, -3, 11, -19, 71, 39, 21]
[-19, -3, 9, 11, 21, 34, 39, 56, 71, 88]
No of operations 92

Pawan Kumar,PGT,AECS-1 Jaduguda Page 15

You might also like