Merged PDF 1
Merged PDF 1
elif (s[i].isalpha()):
a=a+1
elif s[i] in sym:
sc=sc+1
print('No. of Digits =',d)
print('No. of Alphabets =',a)
print('No. of Special Characters =',sc)
def palincheck(s):
if s==(s[::-1]):
print('Its a Palindrome String')
else :
print('Its not a Palindrome String')
def capati(s):
print(s.title())
def replacer(s):
ow=input('You want to replace : ')
rw=input('New word you want : ')
print('')
print(s.replace(ow,rw))
def countnonvowel(s):
sen=s.split(' ')
vow=0
vowc=['a','e','i','o','u','A','E','I','O','U']
for i in sen:
nvow=0
for a in i:
if a in vowc:
nvow=nvow+1
else:
continue
if nvow>0:
Page |3
continue
else:
vow=vow+1
print('No. of words which have NO vowel =',vow)
if ch==1:
cchar(s)
print('')
print(l)
elif ch==2:
palincheck(s)
print('')
print(l)
elif ch==3:
capati(s)
print('')
print(l)
elif ch==4:
replacer(s)
print('')
print(l)
elif ch==5:
countnonvowel(s)
print('')
print(l)
OUTPUT: PART-1 =>
Page |4
PART-2:
PART-3:
PART-4:
Page |5
PART-5:
print('')
def swap_first(l):
l[:len(l)//2],l[len(l)//2:]=l[len(l)//2:],l[:len(l)//2]
print(l)
return print
def swap_adjacent(l):
for i in range(0,len(l)-1,2):
l[i],l[i+1]=l[i+1],l[i]
print(l)
def remove_repeat(l):
l1=[]
for x in l:
if x not in l1:
Page |7
l1.append(x)
print(l1)
def count_fl_char(l):
for i in l:
if str(i).isnumeric()==True:
continue
else:
if len(i)>3:
if i[0]==i[-1]:
print(i,'HAS FIRST & LAST CHAR SAME AND HAS MORE THAN 3 LETTERS')
def count_palindrome(l):
for i in l:
if str(i).isnumeric():
n = str(i)
if len(str(i)) < 2:
continue
elif n[:len(n)] == n[len(n)::-1]:
print (f'{i} is a palindromic number')
else:
if i[:len(l)] == i[len(i)::-1]:
print (f'{i} is a palindrome')
def shift_left(l):
t=l[0]
for i in range (1,len(l)):
l[i-1]=l[i]
l[len(l)-1]=t
print(l)
def shift_right(l):
Page |8
t=l[len(l)-1]
for i in range(len(l)-2,-1,-1):
l[i+1]=l[i]
l[0]=t
print(l)
def magic_num(l):
import math
for i in l:
s=0
p=str(i)
if p.isnumeric():
for a in p:
f=math.factorial(int(a))
s=s+f
if str(s)==i:
print(i,'IS A MAGIC NUMBER')
if ch==1:
swap_first(l)
print('')
print(line)
elif ch==2:
swap_adjacent(l)
print('')
print(line)
elif ch==3:
remove_repeat(l)
print('')
print(line)
elif ch==4:
count_fl_char(l)
Page |9
print('')
print(line)
elif ch==5:
count_palindrome(l)
print('')
print(line)
elif ch==6:
shift_left(l)
print('')
print(line)
elif ch==7:
shift_right(l)
print('')
print(line)
elif ch==8:
magic_num(l)
print('')
print(line)
OUTPUT -> PART-1:
PART-2:
P a g e | 10
PART-3:
PART-4:
P a g e | 11
PART-5:
PART-6:
PART-7:
P a g e | 12
PART-8:
f=1
while x > 0:
f = f*x
x -= 1
return f
def sumofseries():
x = int(input('Enter number: '))
n = int(input('Enter number of terms: '))
sum = 0
for i in range(n):
value = (x**(2*n)/m.factorial(2*n-1))
n -= 1
sum += value
print('')
print('Sum of series =',sum)
print('')
print(l)
print('')
def fibonacci():
n = int(input('Enter number of terms for series: '))
a=0
b=1
print('First',n,'terms of Fibonacci series ->')
print('')
for i in range(n):
print (a)
c=a+b
a=b
b=c
print('')
print(l)
P a g e | 14
print('')
def reversenumber():
n = int(input('Enter number to be reversed: '))
a = str(n)[::-1]
b = int(a)
print('Reversed Number =',b)
print('')
print(l)
print('')
def hcf(num1, num2):
if num1 > num2:
smaller = num2
if num1%smaller==0:
return smaller
else:
smaller = num1
if num2%smaller==0:
return smaller
for i in range(1, smaller//2 +1):
if num1 % i == 0 and num2 % i == 0:
hcf = i
return hcf
def lcm(num1, num2):
if num1 > num2:
greater = num1
else:
greater = num2
while True:
if greater % num1 == 0 and greater % num2 == 0:
lcm = greater
P a g e | 15
break
greater += 1
return lcm
if ch==1:
sumofseries()
elif ch==2:
fibonacci()
elif ch==3:
reversenumber()
elif ch==4:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print('')
hcf(num1,num2)
lcm(num1,num2)
print("The Highest common factor of", num1, "and", num2, "=", hcf(num1, num2))
print("The Lowest common multiple of", num1, "and", num2, "=", lcm(num1, num2))
print('')
print(l)
print('')
OUTPUT: PART-1=>
P a g e | 16
PART-2:
PART-3
P a g e | 17
PART-4:
def query():
an=int(input(' ENTER ACCOUNT NO. YOU WANT TO CHECK : '))
if an in account :
print(' Account holder name : ',account[an][0])
print(' Account balance : ',account[an][1])
else:
print(' NO ACCOUNT FOUND')
def close():
an=int(input(' ACCOUNT YOU WANT TO CLOSE :'))
account.pop(an)
def allaccdetails():
for an in account:
print(' Account number - ',an)
print(' Name - ',account[an][0])
print(' Balance - ',account[an][1])
def main() :
P a g e | 19
while True :
print('')
print('~'*45)
print('TASKS PERFORMABLE # ')
print('')
print(s,'1 : CREATE/ADD')
print(s,'2 : DEPOSIT')
print(s,'3 : WITHDRAWAL')
print(s,'4 : QUERY')
print(s,'5 : VEIW ALL ACCOUNT DETAILS')
print(s,'6 : CLOSE')
print('')
print('~'*45)
ch=int(input('\t ENTER CHOICE : '))
print('')
if ch==1:
create()
elif ch==2:
deposit()
elif ch==3:
withdrawal()
elif ch==4:
query()
elif ch==5:
allaccdetails()
elif ch==6:
close()
while True:
print('\t\tWELCOME TO THE BROKE BANK')
main()
P a g e | 20
PART-2:
PART-3
P a g e | 21
PART-4:
P a g e | 22
PART-5:
PART-6
P a g e | 23
# Q6 para.txt MORE TEXT FILE COUNT RELATED FUNCTIONS
def main():
while True:
print('YOUR CHOICES ARE ->')
print("1 - To count number of words starting with 'A' ")
print('2 - TO count number of words that have length > 10')
print("3 - TO count occurence of word 'the'")
ch=int(input('ENTER CHOICE :'))
if ch==1:
countA()
elif ch==2:
count_10()
elif ch==3:
cthe()
print()
main()
'''
OUTPUT =>
para.txt :
I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
YOUR CHOICES ARE ->
1 - To count number of words starting with 'A'
2 - TO count number of words that have length > 10
3 - TO count occurence of word 'the'
ENTER CHOICE :1
NO. of words starting with 'A' : 3
'''
# Q6 para.txt COUNT RELATED FUNCTION ON LINE BASIS
def main():
while True:
print('OPTIONS ARE ->')
print("1 - To count number of line starting with 'T' ")
print("2 - To count number of line containing 'is' ")
ch=int(input('ENTER CHOICE :'))
if ch==1:
countlineT()
elif ch==2:
countis()
print()
main()
'''
OUTPUT :
para.txt
I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
today is today
tom is good cat
Tom is friend of jerry Tom is also creature
is there a good tv set is it realy a good package
aliscyinto is nothing but hoax
Is quite there _ is it good for study
remZ()
'''
OUTPUT ->
original para.txt
I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
today is today
tom is good cat
Tom is friend of jerry Tom is also creature
is there a good tv set is it realy a good package
ZOLO is nice ZOHO is also great
aliscyinto is nothing but hoax
Is quite there _ is it good for study
zam is zam
new para.txt
I am a student
lol who cares!
so as i was saying
Aldo has been hiding on supermaximusisland
also i do not know whats happening , ANDY is missing
And london has already fallen
and the world has gone crazy
this is the reason for mass stupid media consuption
today is today
tom is good cat
Tom is friend of jerry Tom is also creature
is there a good tv set is it realy a good package
aliscyinto is nothing but hoax
Is quite there _ is it good for study
zam is zam
'''
# Q9 File for creation of sports related info storing
def createfile():
f=open('sports.txt','a')
l=[]
while True:
sportid=input('ENTER SPORT ID :')
sname=input( 'ENTER SPORT NAME :')
fee= (input('ENTER FEE :'))
timing=input( 'ENTER TIMINGS :')
rec=sportid+"\t"+sname+"\t"+fee+"\t"+timing+"\n"
l.append(rec)
c=input('CONTINUE =')
if c=='n':
break
f.writelines(l)
def printinfo():
f=open('sports.txt','r')
l=f.readlines()
print()
print("SPORTS ID\t SPORTSNAME\t FEES\t TIMINGS")
for i in l:
print(i)
f.close()
def main():
while True:
print('1- Make text file sports.txt which contains sports information')
print('2- Read file sports.txt')
ch=int(input('ENTER CHOICE :'))
if ch==1:
createfile()
if ch==2:
printinfo()
main()
"""
OUTPUT ->
"""
# Q10 To create a binary file student.dat and do the functions
import pickle
def writeline():
f=open('student.dat','ab')
while True:
rno=int(input('ENTER ROLL NUMBER :'))
name=input( 'ENTER NAME :')
mks=float(input( 'ENTER MARKS :'))
rec=[rno,name,mks]
pickle.dump(rec,f)
c=input('CONTINUE -')
if c=='n':
break
f.close()
def readbinfile():
with open('student.dat','rb')as f:
while True:
try:
rec=[]
rec=pickle.load(f)
print(rec)
except EOFError:
break
def mkscondition():
with open('student.dat','rb')as f:
while True:
try:
rec=[]
rec=pickle.load(f)
if rec[2]>90:
print(rec)
except EOFError:
break
def main():
print('THESE ALL THINGS CAN BE DONE TO STUDENT FILE')
print('1 - ADD RECORD')
print('2 - DISPLAY ALL RECORDS')
print('3 - DISPLAY RECORD WHERE MARKS>90')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
writeline()
if ch==2:
readbinfile()
if ch==3:
mkscondition()
main()
"""
OUTPUT :
"""
# Q 11 TO edit the student.dat file for following functions
import pickle
import os
def changemks():
f1=open('student.dat','rb')
f2=open('temp.dat','wb')
rno=int(input("ENTER STUDENT ROLL NUMBER :"))
nmks=int(input('ENTER NEW MARKS :'))
while True:
try:
rec=pickle.load(f1)
for i in rec:
if rec[0]==rno:
rec[2]=nmks
pickle.dump(rec,f2)
except EOFError:
break
f1.close()
f2.close()
os.remove('student.dat')
os.rename('temp.dat','student.dat')
def delete():
f1=open('student.dat','rb')
f2=open('temp.dat','wb')
r=int(input('ENTER ROLL NUMBER THAT NEEDS TO BE DELETED - '))
while True:
try:
rec=pickle.load(f1)
if rec[0]!=r:
pickle.dump(rec,f2)
except EOFError:
break
f1.close()
f2.close()
os.remove('student.dat')
os.rename('temp.dat','student.dat')
def readbinfile():
f=open('student.dat','rb')
rec=[]
while True:
try:
rec=pickle.load(f)
print(rec)
except EOFError:
break
def main():
print('1 - UPDATE MKS OF SPECIFIC STUDENT USING ROLL NUMBER {USING TEMP.DAT}')
print('2 - DELETE RECORD OF SPECIFIC STUDENT {USING TEMP.DAT}')
print('3 - DISPLAY ALL RECORDS')
print('4 - EXIT')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
changemks()
elif ch==2:
delete()
elif ch==3:
readbinfile()
elif ch==4:
break
main()
"""
OUTPUT :
"""
# Q12 CERATE BOOK DATABASE AND TO DISPLAY ALL RECORDS
import pickle
def bookcreation():
f=open('books.dat','ab')
l=[]
while True:
bookno=input( 'ENTER BOOK NUMBER :')
def readbook():
with open('books.dat','rb')as f:
while True:
try:
l=pickle.load(f)
print('BookNumber','Name','Subject','Price')
for i in l:
print(i[0],i[1],i[2],i[3])
except EOFError:
break
def main():
print('1 - ADD RECORDS')
print('2 - DISPLAY ALL RECORDS')
print('3 - EXIT')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
bookcreation()
elif ch==2:
readbook()
elif ch==3:
break
main()
"""
OUTPUT :
"""
# Q13 UPDATE BOOK DATABASE AND SEARCH SPECIFIC
def pricecondition():
with open('books.dat','rb')as f:
while True:
try:
rec=[]
rec=pickle.load(f)
for i in rec:
if i[3]>500:
print('BookNumber','Name','Subject','Price')
print(i[0],i[1],i[2],i[3])
except EOFError:
break
def readbook():
with open('books.dat','rb')as f:
while True:
try:
l=pickle.load(f)
print('BookNumber','Name','Subject','Price')
for i in l:
print(i[0],i[1],i[2],i[3])
except EOFError:
break
def main():
print('1 - UPDATE PRICE OF SPECIFIC BOOK')
print('2 - SHOW BOOKS WITH PRICE>500')
print('3 - READ ALL')
while True:
ch=int(input('ENTER CHOICE :'))
if ch==1:
changeprice()
elif ch==2:
pricecondition()
elif ch==3:
readbook()
print()
main()
'''
OUTPUT :
ENTER CHOICE :2
BookNumber Name Subject Price
3 QWERTY science 1000
ENTER CHOICE :1
ENTER BOOK NUMBER :1
ENTER NEW BOOK PRICE :666
ENTER CHOICE :2
BookNumber Name Subject Price
1 Will action 666
BookNumber Name Subject Price
3 QWERTY science 1000
ENTER CHOICE :3
BookNumber Name Subject Price
1 Will action 666
2 today fictionb 300
3 QWERTY science 1000
4 Longing biography 350
'''
# Q14 BINARY CINEMA DIRECTORY
import pickle
import os
def createmovie():
with open('cinema.dat','ab') as f:
while True:
mno=int(input('ENTER MOVIE NUMBER :'))
mname=input( 'ENTER MOVIE NAME :')
mtype=input( 'ENTER MOVIE TYPE :')
c={'movno':mno,'movname':mname,'movtype':mtype}
pickle.dump(c,f)
con=input('CONTINUE :')
if con=='n':
break
print(c)
def readdict():
with open('cinema.dat','rb') as f:
while True:
try:
m=pickle.load(f)
print('MOVIE NUMBER ->',m['movno'])
print('MOVIE NAME ->',m['movname'])
print('MOVIE TYPE ->',m['movtype'])
print()
except EOFError:
break
def movietype():
movietype=input('ENTER MOVIE TYPE YOU WANT TO SEARCH :')
with open('cinema.dat','rb') as f:
while True:
try:
m=pickle.load(f)
if m['movtype']==movietype:
print('MOVIE NUMBER ->',m['movno'])
print('MOVIE NAME ->',m['movname'])
print()
except EOFError:
break
def moviesearch():
moviesearch=input('ENTER MOVIE YOU WANT TO SEARCH :')
with open('cinema.dat','rb') as f:
while True:
try:
m=pickle.load(f)
if m['movname']==moviesearch:
print('MOVIE NUMBER ->',m['movno'])
print('MOVIE NAME ->',m['movname'])
print('MOVIE TYPE ->',m['movtype'])
print()
except EOFError:
break
def main():
print('1 - DISPLAY DETAILS')
print('2 - SEARCH SPECIFIC MOVIE TYPE')
print('3 - SEARCH SPECIFIC MOVIE NAME')
print('4 - CREATE DATABASE OF MOVIES')
print('5 - EXIT')
print()
while True:
ch=int(input('ENTER CHOICE :'))
print()
if ch==1:
readdict()
elif ch==2:
movietype()
elif ch==3:
moviesearch()
elif ch==4:
createmovie()
elif ch==5:
break
main()
"""
OUTPUT :
1 - DISPLAY DETAILS
2 - SEARCH SPECIFIC MOVIE TYPE
3 - SEARCH SPECIFIC MOVIE NAME
4 - CREATE DATABASE OF MOVIES
5 - EXIT
ENTER CHOICE :4
ENTER CHOICE :2
ENTER CHOICE :3
import csv
def wcsv():
with open ('student_game.csv','w') as f:
fields=['St_id','St_name','Game_name','Result']
w=csv.writer(f)
w.writerow(fields)
while True:
ID=int(input('Enter student ID :'))
nm=input( 'Enter student name :')
gn=input( 'Enter game name :')
rt=input( 'Enter game result :')
r=[ID,nm,gn,rt]
w.writerow(r)
c=input('Continue ->')
if c=='N' or c=='n':
break
def rcsv():
with open ('student_game.csv','r') as f:
r=csv.reader(f)
for i in r:
print(i[0],i[1],i[2],i[3])
def main():
while True:
print('1 - Enter record')
print('2 - Read record')
ch=int(input('Enter choice =>'))
if ch==1:
wcsv()
elif ch==2:
rcsv()
main()
'''
OUTPUT ->
1 - Enter record
2 - Read record
Enter choice =>1
Enter student ID :1
Enter student name :AKSHAN
Enter game name :GOLF
Enter game result :WIN
Continue ->Y
Enter student ID :2
Enter student name :TANI
Enter game name :BASKETBALL
Enter game result :DRAW
Continue ->Y
Enter student ID :3
Enter student name :KUSH
Enter game name :FOOTBALLL
Enter game result :LOST
Continue ->Y
Enter student ID :4
Enter student name :ANDY
Enter game name :CHESS
Enter game result :DRAW
Continue ->Y
Enter student ID :5
Enter student name :LINSY
Enter game name :FENCING
Enter game result :WIN
Continue ->N
1 - Enter record
2 - Read record
Enter choice =>2
St_id St_name Game_name Result
1 AKSHAN GOLF WIN
2 TANI BASKETBALL DRAW
3 KUSH FOOTBALLL LOST
4 ANDY CHESS DRAW
5 LINSY FENCING WIN
'''
import mysql.connector as mc
mydb=mc.connect(host='localhost',user='root',passwd='password')
mycursor=mydb.cursor()
mycursor.execute('show databases')
for i in mycursor:
print(i)
def create():
mycursor.execute('create database if not exists stock')
mycursor.execute('use stock')
mycursor.execute('create table if not exists ITEM (ICODE varchar(5) primary key,Idesc varchar(10),Price numeric(8,2), Purdate date)')
def insertrecord():
mycursor.execute("use stock")
while True:
icode=input ('ENTER ITEM CODE :')
ides=input ('ENTER ITEM DESCRIPTION :')
p=int(input ('ENTER ITEM PRICE :'))
purdate=input('ENTER PURCHASE DATE [YYYY-MM-DD] :')
mycursor.execute("insert into item values(%s,%s,%s,%s);",(icode,ides,p,purdate))
mydb.commit()# TO SAVE CHANGES IN DATA BASE
c=input("CONTINUE :")
if c=='n' or c=='N':
break
def showall():
mycursor.execute('use stock')
mycursor.execute('select * from item')
myrec=mycursor.fetchall()
for i in myrec:
print(i[0],i[1],i[2],i[3],i[4])
def showgreater_1000():
mycursor.execute('use stock')
mycursor.execute('select * from item where price > 1000')
myrec=mycursor.fetchall()
for i in myrec:
print(i[0],i[1],i[2],i[3],i[4])
def priceincrease():
mycursor.execute('use stock')
record=input('ENTER ITEM CODE :')
priceinc=int(input('PRICE INCREASE BY :'))
mycursor.execute(f"update item set price=price+{priceinc} where icode={record};")
mydb.commit()# TO SAVE CHANGES IN DATA BASE
def newcolumn():
mycursor.execute('use stock')
mycursor.execute("alter table item add (DISCOUNT numeric(25,10) default 100 );")
mydb.commit()# TO SAVE CHANGES IN DATA BASE
def startp():
mycursor.execute('use stock')
mycursor.execute("select * from item where ides like 'P%';")
myrec=mycursor.fetchall()
for i in myrec:
print(i[0],i[1],i[2],i[3],i[4])
def main():
while True:
print('1 - CREATE')
print('2 - WRITE')
print('3 - SHOW ALL RECORDS')
print('4 - SHOW ITEM WITH PRICE > 1000')
print('5 - INCREASE PRICE BY X FOR SPECIFIC RECORD')
print('6 - ADD NEW COLUMN DISCOUNT{ NUMERIC }')
print('7 - SHOW WHERE IDES START WITH P')
1 - CREATE
2 - WRITE
3 - SHOW ALL RECORDS
4 - SHOW ITEM WITH PRICE > 1000
5 - INCREASE PRICE BY X FOR SPECIFIC RECORD
6 - ADD NEW COLUMN DISCOUNT{ NUMERIC }
ENTER CHOICE :6
1 - CREATE
2 - WRITE
3 - SHOW ALL RECORDS
4 - SHOW ITEM WITH PRICE > 1000
5 - INCREASE PRICE BY X FOR SPECIFIC RECORD
6 - ADD NEW COLUMN DISCOUNT{ NUMERIC }
ENTER CHOICE :3
('1', 'food', Decimal('200000.00'), datetime.date(2019, 8, 2), Decimal('100.0000000000'))
('2', 'phone', Decimal('700.00'), datetime.date(2006, 9, 2), Decimal('100.0000000000'))
('3', 'calculator', Decimal('400.00'), datetime.date(2000, 1, 23), Decimal('100.0000000000'))
('4', 'burger', Decimal('30000.00'), datetime.date(2023, 2, 24), Decimal('100.0000000000'))
('5', 'tyres', Decimal('390.00'), datetime.date(1994, 4, 12), Decimal('100.0000000000'))
'''