0% found this document useful (0 votes)
17 views52 pages

Untitled 2 Ocr

Uploaded by

yadunandansaran
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)
17 views52 pages

Untitled 2 Ocr

Uploaded by

yadunandansaran
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/ 52

- -- - - - -- - - - - - - - - - - - - - --

FILE I: PROGRAM ON
FUNCTION DEFINING
Pt. W AP to define a function named greatest() which
receives three parameters the function returns the
greatest out of three. Also call the function.
def grcat(a,b,c):
if a>b and a>c:
x=a
elif b >a and b >c:
x=b
el~e:
X=C
return x
d =int(input('Enter 1st ntunber--'))
e=int(input('Enter 2nd nu1nber--'))
f int(input('Enter 3rd ntunber--'))
z=great(d,e,f)
print('Greatest nun1ber is--',z)

OUTPUT:
Enter 1st number--25
Enter 2nd number--55
Enter 3rd number--30
Greatest number is-55

P2. WAP to define a function primenumber (n). The


function checks whether the number is prime or not
and displays it. Call the function.
def prime_ no(n):
C=O
fo r i in range(1,n+ 1):
if n%i==O:
C=C+l
if C==2 :
print('Number is prime')
else:
print('Nu1nbe1· is not prime')

a=int(input(' Ente1· number--'))


z= :lrin1e_no(a)

OUTPUT:
(i)Enter number--5
Number is prime
(ii)Enter number--8
Number is not prime

P3. WAP to define a function factorial v ue (n) which


receives an integer parameter, the --- ction returns
the factorial value of the number. all the function.

def fact_ value(n):



l =l
f =l
\vhile i<=n:
f=f*i
• •
l = l +l
rett1rn f

a= int(input('Enter number--'))
z=fact_ value(a)
print('Factorial value--',z)
OUTPU'I':
Enter number--5
Factorial value- 120

P4. WAP to define a function named Armstrong (n).


The function retur11s1 if the number passed as
parameter is Armstrong number els e it retur11s o. Call
the function and display whether the number is '
Armstrong number or not. I

def ar111(n):
nt=n
S=O
l1ile 11>0 :
, ,1

r = n %10
s =s+r**3
n =n //10
if S==nl:
print ('It is an At·mstrong number')
else:
print('It is 11ot a n Armstrong number')

a=int(input('Enter number--'))
z=arm(a)

OUTPUT:
Enter number--25
It is not an Armstrong number
Enter number--153
It is an Armstrong number

P5. W AP which defines a function sum (n). The


function returns the su1n of n natural numbers. Call
the function and display the sum.
def sum(n):
S=O
fo r i in range(1,n+ 1):

S=S+ l
retu1·11 s

x=int(inpt1t('Enter number of natural numbers--'))


Z=Stlm(x)
print('Sun1 is--' ,z)

OUTPUT:
Enter number of natural nurnbers--5
Sum is- 15

P6. W AP which defines a function named swapO). The


function receives the list as a para1neter. Function
swaps the alternate elements in the list and displays it.
Call the function.
def s,vapO):
a=lenO)
fo r i in range(o,a,2):
t=l[i]
l[i]=l[i + 1]
l[i+ l]=t
return I

x=eval(input('Enter List:'))
z=swap(x)
print ('Swapped List is:',z)

OUTPUT:
Enter List: [1,2,3,4,5,6]
Swapped List is: [2, 1,4,3,6,5]
P7, WAP to define a function reverse 0). '111e function
reverses the list passed as a parameter. Call the
function and dis la the reversed list.
f
,J, •f rc•v(I):
I>=[]
a = l,·11(1)
t, ,r i i11 r;11114l•(a- 1,- 1,-1):
J>=lfiJ
l>.appcnd(p)
r, ·I , 1r 11 I>

X=t•v,1l(i111>t1t('I•:11lt• r l ~ist :'))


zr rcv(x)
print ('l{<!Vl'rst•d l~ist is: ',z)

0l[[Pll1';
Enter List:[ 1,2,3,4,5,6]
llcversed List is:[6,5,4,3,2,1]
J
PS. WAP to define a function count O,n). The function
counts the occurrences of n in the list I and rettir11s it.
Call the function and display the total number of
occurrences. •I
.
d ef co unt(l,n):
C=()
a=le nO)
f<> r i in range(o,a):
if l[i] ==n:
C=C+l
pri nt(n, 'has occurred' ,c, 'times')

x= eval(input('enter List:'))
y=eval(input('enter number to be searched--'))
count(x,y)
OUTPUT:
Enter List:[11,22,25,24,36,40,22]
Enter number to be searched--22
22 has occurred 2 times

P9. W AP to define a function suin (I), which returns


the s111n of all the ele01ents of the list l passed as
paraineter. Call the function and display the sunt of
the list.
def sun1(l):
S= O
a=lenO)
fo1~ i i11 1·ange( o ,a):
s=s+l[i]
print ('Sun1 of list elements--',s)

x=eval(input('Enter list:'))
y=sum (x)

OUTPUT:
Enter list:[14,20,25,22,10]
Sum of list elements-91

Pio. W AP to define a function greatest (I). The


fu11ction retu1·ns the greatest value in the list. Call the
function and dis la the eatest value .
.L..,___ _ _ _ __ _ _ _ __ ---,

def greatestO):
g=o
a=lenO)
for i in range(o,a):
if l[i]>g:
g=l[i]
print('Greatest number--',g) ''

x=eval(input('Enter list elements: '))

l
greatest(x)
0 UT: •'

Enter list elements:[11,25,45,85,97,44,26]


Greatest number-97

LEZ: ROG SON IF AND ELIF

P11.Write a progra:rn to enter the salary of an elllployee


. Based on tlte salaiwy catego1·ise the elllployee in group
sand dis ~la ~. - - - - - - - - r - - - - - - - - - - - ---,
Sala Cate o
;.._____,aL---------1

>= 20000 A
10000 to 1 9 B
below 10000. C
I
I
I
z=int(input('Enter salary--'))
if Z>= 20000 :
x= ''A''
elif z>=10000 and a< 19999:
x= ''B''
else:
x= ''C''
print ('Employee belongs to',x,'category')

0 UT:
Enter salary--15000
Employee belongs to B category

P12. Write a progra1n to enter the sale of a salesman in

.
!
float data type and calculate the co1n1nission and displ
ay
Sale Co0101ission

>= 0000 10 % of sale


15000 to<~ 0000 8 % of sale
10000 to < 1 ooo 6% of s ale
Below10000 ~% of sale

a =float(i11put('Ente1· sales amount--'))


if a>=30000:
b =o.1*a
elif a>=15000 and a< 30000: ,
b =o.o8*a
elif a>=10000 a11d a<15000:
b =o.o6*a
else:
b =o.04*a
print ('Commission amount is--',b)

OUTPUT:
Enter sales an1ount--18000
Commission amount is--1440.0

P13. Write a progran1 to enter distance in kilo1netres a


n d CalCulae ... t 0 the £0 IIOWID
t the f are accord.1ni~ . ~
For kiloinetres Fare
>= 1000 500
> =soo and < 1000 300
<500 200

a=int(input('Enter distance(in kilometers)--'))


if a>= lOOO : I
I

h=soo
e I i f a> = 5 <><> ~• n d a< 1c>o c>:
b=:~oo
elst':
b=2oc>
)t"i 11t('Amou11t of fare--' ,b)

OUTPUT:
Enter distance(in l<llo1neters)--1100
Amount of fare--500

Fil :file handlin I st set

P14. Write a progi·ain to create a text file memo.txt,


get the string input fi·ont ·t he user and display the
details.

a =ope11(''n1e1110.L'<t'',''w'')
s=str(inpt1t('' enter string''))
a.write(s)
a.closeO

a=open('memo.txt', 'r')
s=a.read()
print('Content--' ,s)

0 UT:
Enter string--Rain goes to school
Content-- Ram goes to school.

P15. Read the text file inento.txt and count and display
the nuniber of vowels in it.
r - -- - - - -- - - - -- - - ~ - - -- - - -- -- - - - ----...
a =open('men10. txl', 'r')
s =a.read()
C=O
x=le11(s)
fc>1· i i11 ra11gc(o,x):
if s[i] ==''a'' <>r s[i ] ==''c'' <>r s[i] ==''i'' or s[i]==''o'' or s[i]==''u'':
C=C+l
D1·int(''Nt1111l1er of vowels i1re:'',c)

OUTPUT:
Number of vo\vels a1·e: 6

Pt6. Write a program to read a text file memo.txt and


count nun1ber of alpl1abets, digits and other syinbols

a =opc11('111e1110. txt', 'r')


s=a.read()
Cl= O
C2= 0
C3= 0
C4= 0
x=len(s)
for· i in range(o ,x):
if s[i].isalpha():
Cl=Cl+ l
el if s[i] .isdigit():
C2=C2+ 1
elif s[i].isspace():
C4=C4+ l
else:
c3=c3+ 1
print('Number of alphabets--',c1)
print('Number of digits--',c2)
print('Number of symbols--',c3)

0 UT:
Number of alphabets--15
Number of digits--o
Number of symbols-1
P17. Write a 01enu driven progra1n with following
option 1. To write details of student to file 2. to display
all the details 3. to append details

a=int(i11put('Enter choice 1 or 2 or 3--'))


if a== l : I

a=open('stu.txt', 'w')
I

I

s=str(input('E11te1· 11an1e,class and rollnumber respectively--


'))
a.write(s)
a.close()
elit· a= =2: '
I

a=ope11('stu. txt', 'r')


s=a.read()
print ('Details of student:',s)
a.close()
elif a==3:
a=ope11('stu. txt', 'a')
s=input('Enter details of 2nd student--'
a.write(s)
print('Updated details:',s)
a.close()

0 UT:
(i)Enter choice 1 or 2 or 3--1
Enter name,class and rollnumber respectively--
Harshita,12,1
(ii)Enter choice 1 or 2 or 3--2
Details of student:Harshita,12,1
(iii)Enter choice 1 or 2 or 3--3
Enter details of 2nd student--Alisha,12,2
Updated details: Harshita,12,1
Alisha,12,2
I

l

P18. Write a progra1n to read the password and save it


in encrypted f orin to the text file ''passwrd.txt''

def pswd():
f =open ( '' password .txt'' , '' w '')
x=input(''Enter the password--'')
a =len(x)
c=''
fo1· i in ra11ge( o,a) :
0=01·d(x[i])
#pri11t(o)
c=c+cl11·(0+ 23)
pri11t('E11c1y1)ted l't1ssword--',c,end= ' ')
f.write(c)
f.close()
pswd()

OUTPUT:
Enter the password--Marial<Sp123
Encrypted Password--dxxHIJ

P19.Waf to decrypt the pass in the file password.txt


and display it

def pswdcrypt():
f= open(''password. bct'', ''r'')
x=f.read()
a=len(x)
c=''
for i in range(o,a):
o=ord(x[i])
#print(o)
c=c+chr( 0-23)
print('Decrypted Password--',c,end= ' ')
f.close()
l

pswdcrypt()
I

OUTPUT:
Dec~t_ed Password--Mariaksp123 --

File : ile.handlin • nd set


I
P20.Write a progi·ain to create a text file student.txt,
which stores the details of 3 students.

a=open('stt1de11t. txt', 'w')


pri11t('Enter details of 3 students')

fc)t· i i11 range(1,4):


s =str(i11pt1t('Enter name,class and section of student no.',i,'--
'))
a.write(s)
a. write('\ n')
a.close()
I

OUTPUT:
I

Enter details of 3 students


Enter name,class and section of student no.1-Abhay,10,A
Enter name,class and section of student no.2- Amit,11,C
Enter name,class and section of student no.3-Akshay,12, B

P21.Write a progrant to read the text file student.txt


and display the details

a=open('student.txt','r')
s=a.read()
print(s)
a.close()
OUTPUT:
Abhay10A
Amit 11 C
Akshay12B
P22. Write a progrant to read a text file 01eino.txt and
display every alternate character in capital letter.

a=open('memo. txt','r')
s=a.read()
x=le11(s)
Sl= ''
for' i i11 ra11ge(o ,x,2):
s1=s[i]+s[i+1].upper()
PI'int('new: ',s1)

0 UT:
AbHaY 10 a
An1It 11 C
AkShAy 12 B

P23. Write a prograni to read ate file 01eD10.txt and


display every first character the word in capital
letter

a=open ('memo.bct','r')
s=a.read()
s1 =s. title()
print(s1)

0 UT:
Ram Goes To School.

P24. Write a prograni to read a text file nieIDo.txt and


write all the capital letters froDl the file in capital.txt
and sntall letters in slllall. txt
a=open('memo. txt','r')
h=open('capital. txt', 'w')
c=open('small. txt', 'w')
s=a.read()
x=len(s)
Sl=''
S2= ''
for i in range(o,x):
if s[i].isupper():
Sl=Sl+S[i]
if s[i].islower():
S2=S2+s[i]
b.write(s1)
c.write(s2)
pri11t(s1)
p1. int(s2)
a.close()

OUTPUT:
R
Amgoestoschool.

P25. Write a prograDl to read the content of a text file


Ineino. txt and display every new sentence in the new
line along with the line nuIDber.

a=open('memo.txt','r')
s=''
C= O
,vhile True:
if s!='':
break '
I

C=C+ l
s=a.readline()
print(s,'is in line no.',c)
a.close()
a=open('memo.txt','a+')
fo1~i in range(o,3):
s=input('enter text')
a.write(s)
a.write('\n')
a.close()

OUTPUT:
Ram goes to School is in line number 1
He is a good boy is in line number 2
He studies Computer is in line number 3

P26. Write a progrant that reads characters front the


keyboard one by one. All the lowercase characters get
stored in the file lower. txt and all uppercase
characters get stored inside file upper.txt and all the
other characters in the file other.txt

u=ope11(''upper .txt'', ''w'') I

I=open(''lower. txt'',''w'') I
I

o=ope11('' other. txt'', ''w'')


wl1ile True:
s=input(''Enter character--'')
if s.isupper():
u.write(s)
elif s.islower():
l.write(s)
else:
o.write(s)
x=input(''Want to enter more character?'')
if x!='y' and x!='Y':
breal<
u.close()
I.close()
a.close()
u=open(''upper. txt'' ,"r'')
l=open(''lower. txt'', '' r'')
0=01Jen('' other. txt'', '' r'')
print(u.read())
print(l.read())
print(o.read())
u.close()
I.close()
a.close()

OUTPUT:
E11ter cl1a1·actcr--a
Wa11t to e11te1· 11101·e cl1a1·nctc1·? y
E11ter cl1arncte1·--U
Wa11t to e11te1· 11101·e cl1a1·actc1·? Y
Enter cl1aracte1·--o
Wa11t to e11te1· 11101·e cl1aracter? y
Enter cl1a1·acte1·-- I<
Wa11t to enter mo1·e cl1a1·acte1·? Y
E11ter cl1aracter--4
Want to ente1· 1nore cl1aracter? y
Enter character ()
Want to enter more character? j
UK
Ao
4()

1le :readline functions

P27.Understanding readline functions


r -·- -- --- •--= • .,,_ -•-=--=-------""-..-----=--&...o--tt.c:=
f=o pen('lettcr.txt','w')
s1=input('enter string 1--')
S2=input('enter string 2--')
s3=input('entcr string 3--')
s4=s1 +'\n'+s2+ '\ n' +s3
f.write(s4)
print(s4)
f.close()

OUTPUT:
enter string 1-- It is 1·ai11ing ou tsidc.
enter stri11g 2--Lovcly W ea·tl1er
enter string 3--Time ·f or some p al<odas,
It is raining outside.
Lovely Weather
Tin1e for some palcodas.

P28.To print different lines of a text file.

a=ope11('letter .txt','r')
s=a.readline()
print(s)
s=a. read.line()
print(s)
s=a.readlineO
print(s)
OR

f= open('letter. txt','r')
S= '' ''
while s:
s.readline()
print(s)
.f [.] ,,,,
1 S 1 == :
breal<
f.close():

OUTPUT:
It is raining outside.

Lovely Weather.

Time for some palcodas.

P29.To display lines starting wi·t h 'i'.

f=open(''lette1·. txt'', '' r'')


S = '' ''
,,v11 i1e s!= '' '' :
s=f. readline()
if s[o]==''I'':
p1·i11t(s)
f. close()

OUTPUT:
It is raining outside.

P30. Write a prograin to read a text file 'letter.txt' and


display the total n11niber of characters in each line.
Also Display the line having maxiIDulll nuinber of
characters.

a=open('letter. txt','r')
g=o
S= ,, ''
wl1ile True:
s=a.readline() I
j

x=len(s)
print('Number of characters--',x)
if x>g:

r - - - - - - - -- - - - - - - - - - -- - - -- - - -~-
g=x
Drint('lon ~est line:' ,g)

OU'fPUT:
Number of characters--22
Number of characters--15
Number of characters--23
Longest line: Time for some pakodas.

P31.Write a prograin to read a text file letter.txt and


count nu111ber of 'it' present in the file as separate
wo1·d.

f= open('letlc1·. txt','1·')
s=f.read()
l=s.split()
Cl= O
C2= 0
10 1· i i11 l:
it' i = ='it' :
Cl=Cl+ l
elif i== 'It':
C2=C2+ 1
f.close()

0 UT:
It comes o times. •
it comes 1 times.

P32.Write a Prograin to read a text file letter.txt and


display only 3 letter words on the screen.

f= open('letter. txt','r')
s=f.read()
l=s.split()
for i in I:
a =len(i)
if a==3:
print(i)
f.close()

0 UT:
For
P33.Write a PrograD1 to read a text file letter.n.1: and
reverse and display eve1-y four letter word

f=open('lette1~.txt', '1·')
s =f. read()
l=s.split()
fo1· i i11 I:
if le11(i)==4 :
a =le11(l)
b=''
fo1· j in 1·a11ge(a -1,-1,-1) :
c=c+i[a]
print(c)
f.close()

OUTPUT:
emiTemos

P34.Write a function to read a text file 01e010.txt and


count no.ofp.Also call the ftinction

def readfileO:
f= open('memo.txt', 'r')
s=f.read()
x=len(s)
C=O
for i in range(o,x):
if s[i]=='p' or s[i]=='P':
- - -- ------------

- - - - - - - - - - - - - - - - - - - - =~~ =="' =
c=c+ 1
Jlt·i11t('ll 011d 11 co111e',c,'ti1nes')
f.close()

rend file()

O\J'fPU'1::
P n11d p co111e 2 ti1ncs

oard ue ions

Q35.Write 11 lqt11clio11 de·fl11i·t ion ·fo1• PURE,.f EXT() IN


C++ tl1nt would dis11Iny tl1c co1·1·ected version of the
entire article of tlte file OTES.TXT with all the
alpl1abets ''K'' to be displayed as an alphabet 'C' on
screc11.

dl't· l)\11·etext():
f=ope11('MYNOTES.1 X'l~','1·') 1

s=a.read()
x=le11(s)
Sl=''
for i i11 ra11ge(o,x):
if s[i]=='l<':
Sl=Sl+'c'
elif s[i]=='K':
Sl=Sl+'C'
else:
Sl=Sl+s[i]
print(' corrected:',s1)

puretextO
0 UT:
I OWN A cu-·1·---E LI'ffLE CAR.
I CARE FOR THAT AS MY CI-IILD.

Q36.Write a function in p _on to count the number of


lines in a text file 'STORY.'I.X'I'' which is st .... g with
an alphabet 'A' .
OR
Write a 1netl1od/function DISPLAYWORDS() in
python to rend lines ft·orn a text
file STORY.TX1,, n11d display tl1ose wo1·ds, which are
less tl1an 4 chn1·ncte1·s

''' ( 1st cltc>icc) '''


f=()})Cll('S'l~O llY.'l'X'l'', '1·')
S= '' ,,
C= O
,,,l1ill' s:
s=f.1~eadline()
1"f s== '' :
bt'"Cl.\ l(
if s[o]= ='A':
C=C+ l
pri11t('Number of Lines starti11g with A:',c)
f.close()
'''(2nd choice) '''
f= open('STORY.--'l'XI'','r')
s=a.read()
l=s.splitO
fo r i in I:
if len(i)<4:
print('words with less than 4 characters--',i)
f.closeO

f=open('STORY.TXT','w')
l=['A lion','A tiger','There was','He is']
for i in 1:
f.write(i)
f. write('\n ')
f.close()

OUTPU'1::
(i)(tst choice) Number of Lines starting with A:2
(ii)(2nd choice)words with less than 4 characters--A
words with less tl1nn 4 cl1n1·acters--A
words with less thn11 4 cl1n1·nctcrs--was
words witl1 less thnn 4 c11n1·ncters--I-Ie
words witl1 less tl1n11 4 cl1nrnctcrs- is

Q37.Write n fu11ction Rcv'fext() to read a text file ''


Input.ttt '' n11d Print only
word starti11g with 'I' in 1·evcrse order .
Example: If value in text ttle is: INDIA IS MY
CO RY
Output will be: AIDNI SI MY CO __ Y

def RevText():
f=ope11('Input.txt', 'r')
s=f.readline()
l=s.split()
c=''
for i in 1:
if i[o]=='I':
a=len(i)
for b in range(a-1,-1,-1):
c=c+i[b]
else:

C=C+l
print(c)
f.close()
g=open('Input. txt', 'w')
s='India Is my country'
g.write(s)
g.close()

RevText()

OU'I'PU'f:
aidnlslmy country

LE7:SUM PROG MOFTEXT


FILES
P.38
1. Add new text to file
2. Display tlte content of text file
3. Append text to tl1e existing file
4. Display all tl1e content in uppe1·case

def ,vr():
f=ope11(' xyz.txt', 'w')
s=str(input('Write text:'))
f.write(s)
f.close()
def pr():
f=open('xyz. txt','r')
s=f.read()
print(s)
f.closeO
def app():
f=open('xyz.txt', 'a+')
s=f.write('content appended')
print(s)
f.close()
def upercase():
f=open('xyz.txt', 'r ')
s=f.read()

----
x=len(s)
fot' i i11 range( o,x):
A=s[i].upper()
p1·int(A,end=' ')
f.close()

q=int(inpt1t('E11ter n digit between 1 and 4--'))


if q==l:
wr()
clif q==2:
pr()
elif q==3:
app()
t'lii~q ==4:
upe1·case()
t"'lse:
)I'i11t('i11vnlid e11t1y')

OUTPUT:
(i)Ente1· a digit betwee11 1 a11d 4--1
Write t~\.1::Tl1is is Compute1· Lesson.
(ii)Enter a digit between 1 and 4--2
This is Computer Lesson.
(iii)Enter a digit between 1 and 4--3
This is Computer Lesson.content appended
(iv) Enter a digit between 1 and 4--4
TH IS IS COMPUTER LESSO N. C O NT ENT
APPENDED

1le8:bina 1les
P39.To show that print contniand can change printed v
alues in print co1ninand

import pickle
f =open ( ,, num.da t '' , '' wb'')
a=4
h=s
C=6
pickle.dump(a,f)
piclde.dump(b,f)
picltle.dump( c,f)
f.close()

f= open(''num.dat '', ''rb'')


a=picl<le.load(f)
b=piclde.load(f)
c=picl<le.load(f)
print(a+5,b,c)
f.close()

OUTPUT:
956

P40.Write a function to enter the details of 3


einployees and write thent to file e1np.dat
Eno, naine, sala1·y

import pickle
def bin():
f=open('emp.dat','wb')
l=[]
for i in range(o ,3):
a=int(input('Enter employee no.--'))
b=input('Enter name--')
c=int(input('Enter salary--'))
1.append(a)
l.append(b)
l.append(c)
picl<le.dumpO,O
f.close()

bin()
f=open('em p.dat', 'rb')
l=[]
l=picl<le.load(f)
print(l)
f.close()

OUTPUT:
(i)Enter en1ployee no.--1
Enter name--Alcsl1ay
Enter salary--10000
Enter en1ployee no.--2
Enter name--Abhay
Enter salary--12000
Enter employee no.--3
Enter name--Bhavesh
Enter salary-15000
(ii)[1, 'Akshay', 10000, 2, 'Abbay', 12000, 3, 'Bhavesh', 15000]

P 41.Write a Progra1n to enter the details of students


and then display the details.(Roll No., Na111e, Marks)

import pickle
f=open(' study1.dat', 'wb ')
l= □
for i in range(o,2):
a=int(input('enter rollno. '))
b=input('enter name')
c=float(input('enter marks'))
l.append(a)
l.append(b)
-------- - --

l.append(c)
pickle.dump(l,t)
f.close()

in11)ort pickle
f=open('study1 .dat', '1·b')
l=[]
l=pickle.load(t)
J)t·int(l)
f.close()
OUTPUT:
(i)Enter rollno.1
E11ter na m eAsl1111it
Enter n1a1·l<S80
Enter rollno.2
Enter name Brijesl1
Enter ma rl<S92.5
[1, 'Ashmit', 80.0 , 2, 'Brijesl1', 92.5]
lI
I
II
P42.Write a Function to read n111n1.dat and find the
s111n of even and odd nuntbers separately
/
def data2():
f= open('num1.dat', 'rb'~ ~-
s1=0
S2=0
fo r i in range(o ,10):
a=pickle.load(f)
if a%2==0 :
Sl=Sl+a
else:
S2=S2+a
print('Sum of odd--',s1)
print('Sum of even--',s2)
data2()

0 UT:
Sum of odd--138
Sum of even-48

P43.Write a Function to read nu1n1.dat and display all


the nu1nbers

in11Jort picltle
def data1():
f= ope11('nu1n1.dat', '1~b')
fo1· i i11 range(o ,10):
a =piclcle.load(t)
pri11t(a,end=' ')
f.close()
data1()

OUTPUT:
14 22 20 5 44 7 15 10 28 21

P44.Write a Function to write 10 nun1bers to the


bina1·y file nuni1.dat.

i111po1t picltle
def data():
f= open('num1.dat','wb')
for i in range(o ,10):
a=int(input('enter number'))
pickle.dump(a,f)
f.close()
data()

OUTPUT:
Enter number--14
Enter number--22
Enter number--20 -
Enter number--5
Enter number--44
Enter number--7
Enter number--15
Enter number--10
Enter number--28
Enter number--21

LE9:BIN _2NDSET

P45.Write a Function to read the binary file emp.dat


and display all tl1c details

i1111)01·t picltle
d ef e111pl1():
f= open('en1p.dat', 'rb')
l=[]
t111:
,vl1ile True:
l=pickle.load(f)
printO)
except EOFError:
print('out of loop')
pass
f.closeQ
empl1()

0 UT:
[1, 'Aman', 9000, 'A']
[2, 'Anmol', 15000, 'A']
out of loop
P46.Write a Function to read the binary file emp.dat
and display the details of the elllployees who receive
salary inore than 10000.0

in1port pickle
def empl2():
f=open('emp.dat','rb')
l=[]
try:
,vl1ile True: •

l=pickle.load(t)
if 1[2] >10000:
pri11t(l)
except EOFE1~1"01·:
{Jass
f.close()

en1 12()

OUTPUT:
[2, 'Anmol', 15000, 'A']

P 4 7.Write a Function to te the details of employees


to the binary file ernp.dat, till the user's choice is 'y'
The details are.
Eno, nante, salaa·y,grade

in1port pickle
def employ():
f= open(' em p .dat', 'wb ')
l= □ •
I

try:
while True:
en=int(input('Enter employee number--'))
n=input('Enter name--')
s=int(input('Enter salary--'))
g=str(input('Enter grade--'))
I.a PJ)cnd (c11)
1.o ppe11d( 11)
l.a1>pe11d(s)
1.n ppe11d(g)
picl<lc.dt11n JJ(l, 0
I=[]
p L. str( i111>t1t ( 'cl<> y<>tl w ,1111 le> <'lll<'r· 111c,1·<' cl<·t.1i]s ·?:'))
i f J) r y C t
1
:

lll'(': 11,
(' X C' ( ' l)t J•:()JIJ•:1·1·<>1·:
I); l '-, ' ~
f.clos(~{)
c111ploy()

ou·rpu1":
E11tcr e1nployce 11t1111bc1·-- 1
Enter na111e--A111an
Enter sala1y--9000
E11ter grade--A
Do you want to enter more details?:y
Enter employee numbe1 --2 1

E11ter name--Anmol
E11ter salary-15000
Enter grade--A
Do you want to ent er more details?:n

P .48Write a menu driven prograin for the following


specifications of product
Product number
Product name
Date of purchase

I
Quantity
Rate
Menu
1. Enter new product details
2. Dis play the product according
to product number
3. Display details of product according to the date of
purchase
4. Display all the details
5. Delete a s ingle product
6. Update a product
7. EXIT

NO'f E:Malce use of functions ·f or all the options


i111 ll() t·t picltle
def \lt·o1() :
f=opc11 ('product.da t', 'wb')
l = []
,vl1ile T1·t1e:
a=int(i11put('Enter product 11umber--'))
b=input('Ente1· product name--')
c=input('Enter date of purchase in format DD/MM/ --
')
d =int(input('Enter quantity--'))
e=int(input('Enter rate--'))
l.append(a)
l.append(b)
l.append(c)
I.append(d)
l.append(e)
pickle.dump0,f)
l=[]
p=input('Do you want to add another product?y or no:')
if p!='y':
breal<
f.close()
def pro2():
try:
f = <>I)(~11 (' Il1·c><I ,1ct·.<Iti t·' ,' r IJ' )
q =int(i111>11t ('J,:11l'c1· J)r·c,clt1cl nt1111l1cr wlt<>SC <lc l,1ils l<> be dis
J>l:1yccl --'))
flag=<>
l=l]
w I1i I(, ' 1•1•11 <! :
1= J)icl<lc. lc>Lld(f)
i,. 1[ ()] ~ C q :
)ll'i11t (l)
flug= I
f.closc()
(' X('l'lll 1~01·t 1◄:1· 1·0 1·:
i f. fl ll gm= C) :
1)1·i11t ('J>1·c,(lt1ct 11c>l f<>1111cl witl1 tl1i,t })J'<>duct nc>.')
Jl 1· i1l I( 'S l!i 11·(.~
I1 C11 ( lCti')
( '~( ·t 'Ill 1·◄,i l cNotl 4,0tl ll dE1·1·0 1·:
J)t·i11l('J 'ilt~Jl<>l f<>ltt1<l')
4

<lt•f· J)I'():~() :
t I'\'

:
f=<>1)c11('1)1·odt1ct.dttt', '1·b')
w=i111>ttt('E11tc1· date of pu1·cl1ase for the product whose <let
~lils to be dis played in ·fo rmat DD/ ~M/ --')
flag=<)
l =[]
\vl1 ile 1~1·ue:
l=piclde.load(f)
if l[2] ==W:
printO)
flag=l
f.close()
except EOFError:
if flag==O:
print('Product not found')
cxceJJt FileNotFou11dError:
print('File not found')
def pro4():
t1·y:
f=o en('product.dat', 'rb ')
p t·int('Displaying a ll product details')
l =[]
\V 11 iIe "f 1·t1c:
l=picl<le.load(O
printO)
cxcc1>t EOFError :
J)t\ SS
def pro6():
f=opc11('product.da t', 'r+ b')
e=int(i11pt1t('Enter prodt1ct nt1111bcr wl1osc details to be updat
ed--'))
L = []
A=int(i11put('E11ter new p1·odt1ct no.--'))
B=in1Jut('Entc1· nc\v product name--')
C=i11put('Enter pt1rcl1asc date--')
D=int(inpt1t('Enter qt1i111tity--'))
E=int(i11put('Enter new rate--'))
L.append(A)
L.append(B)
L.append(C)
L.append(D)
L.append(E)
l=[]
,vl1ile True:
x=f.tellO
l=pickle.load(O
if l[o]==e:
l=L
f.seek(x)
pickle.load(O
print('Updated')
f.close()
def pros():
import os
import pickle
try:
f= open('product.dat', 'rb ')
f1= open('product1.dat', 'wb')
--- ---- - -----------------

r =int(input('Enter product number wl1ose details to be dele


ted--'))
1= []
,,vl1ile True:
l=picltle.load(f)
if l[o] l=r:
picltle.dump(l,f1)
f.close()
ft .close()
exceJ)t EOFE1·ro1·:
pri11t('Selectcd lJoolc 110 .dc tnils deleted')
])t\ SS
os. ren1ove('1)1·od ttct.dnt')
os.re11ame('J)t·odt1ct1.dal', '111·odt1ct.da t')

, vl1ilc 1,1·t1e:
z=int(input('''1)Enter 1 fo1· adding new p roduct d etails.
2)E11ter 2 fo1· displayi11g d e tails of product according to produ
ct nu1nber.
3)Enter 3 to searcl1 d etails for p1·oduct of particular date.
4)Enter 4 for displaying details of products.
s)Enter 5 fo1· deleting product of a particular product number

6)Enter 6 for updating details of ~-~oduct with specified produ
ct number.
7)Enter 7 for exiting file--''' ..
if Z== l :
pro1()
elif z== 2:
pro2()
elif z==3:
pro3()
elif z==4 :
pro40
elif Z==s :
pros()
elif z== 6:
pro6()

. . .
-- ---

-
t'lif' 1,cc7:
lll"('ll ),

~~ 1,1·i11t('l11v,1li<I')

OUTJ.>Ul'l
(i) 1)l.:11ter t f<>r lt<.l<.ling 110w pr·c>duct details.
2)E11tc1· 2 f<>r clis11loying <lotuils of product acco1·ding to product
11t1111be1·.
3)E11tcr 3 t<.) sct1rcl1 clcl11ils for p1·oduct of pn11:icular date.
4)E11tcr 4 fo1· displnyi11g dot11ils of JJrodttcts.
5)Ente1· 5 fot· deloti11g JJ1·odt1ct <>fa particular product number.
6)E11te1· 6 fo1· 11pdltli11g <lcti1ils of IJt·oduct witl1 specified product
11 t1111 bet',
7)E11te1· 7 for· cxiti11g filo--1
E11ter 1>1'0dt1ct 11t1111be1·--1
E11ter prodt1ct 11n111e--Soap
E11ter date of pu1·cl1ase in fo1--1nat DD/MM/ --10/02/2020
E11ter qunntity--10
E11ter rate--5
Do you want to add another p1·oduct?y or no:y
Enter product number--2
Enter product name--Powder
Enter date of purchase in format D / /MM --17/02/2020
Enter quantity--15
Enter rate--10
Do you want to add another product?y or no:no

(ii)1)Enter 1 for adding new product details.


2)Enter 2 for displaying details of product according to product
n\11nber.
3)Enter 3 to search details for product of particular date.
4)Enter 4 for displaying details of products.
s)Enter 5 for deleting product of a particular product number.
6)Enter 6 for updating details of product with specified product
number.
7)Enter 7 for exiting file--2
--- - - - -- - - - --

Enter product number ,vbose details to be displayed--1


[t, 'Soap', '10/02/2020', 10, 5]
Search ended

(iii)1)Enter 1 for adding new product details.


2)Enter 2 for displaying details of product according to product
number.
3)Enter 3 to searcl1 details for product of particular date.
4)Enter 4 for displaying details of products.
s)Enter s for deleting product of n particular product number.
I
6)Enter 6 for updating details of p1·oduct with specified product I

number.
7)Enter 7 for ~'Xiting file--3
Enter date of purcl1ase for the product whose details to be
displayed in forn1at DD/MM -- I
I,

17/02/2020
[2, 'Powder', '17/02/2020', 15, 10]

(iv)1)Enter 1 for adding new product details.


2)Enter 2 for displaying details of product according to product I
number.
3)Enter 3 to search details for product of pa1·ticular date.
I
4)Enter 4 for displaying details of products. I
s)Enter 5 for deleting product of a pa1·ticular product number. I
I
I
6)Enter 6 for updating details of product with specified product '

number.
7)Enter 7 for exiting file--4
Displaying all product details
[1, 'Soap', '10/02/2020', 10, 5]
[2, 'Powder', '17/02/2020', 15, 10]

(v) 1)Enter 1 for adding new product details.


2)Enter 2 for displaying details of product according to product
number.
3)Enter 3 to search details for product of particular date.
4)Enter 4 for displaying details of products.
s)Enter s for deleting product of a particular product number.
6)Enter 6 for updating details of product with specified product
number.
7)Enter 7 for exiting file--5
Enter product number whose details to be deleted--1
Selected book no.details deleted
1)Enter 1 for adding new product details.
2)Enter 2 for displaying details of product according to product
number.
3)Enter 3 to search details for product of particular date.
4)Enter 4 for displaying details of products.
s)Enter s for deleting product of a particular product number.
6)Enter 6 for updating details of product with specified product
number.
7)Enter 7 for exiting file-4
Displaying all product details
[2, 'Powder', '17/02/2020', 15, 10]

(vi)1)Enter 1 for adding new product details.


2)Enter 2 for displaying details of product according to product
number.
3)Enter 3 to search details for product of particular date.
4)Enter 4 for displaying details of products.
s)Enter s for deleting product of a pa1·ticular product number.
6)Enter 6 for updating details of product with specified product
number.
7)Enter 7 for exiting file--6
Enter product number whose details to be updated--2
Enter new product no.--1
Enter new product name--Powder
Enter purchase date--17/02/2020
Enter quantity--20
Enter new rate--10
Updated
1)Enter 1 for adding new product details.
2)E11te1· 2 fo1· displayi11g details of product according to product
11UJ11be1·.
3)E11ter 3 to search details for product of particular date.
4)Enter 4 for displaying details of products.
5)E11te1· 5 fo1· deleting product of n particular product number.
6)Enter 6 for updati11g dctnils of product witl1 specified product
nu111be1·.
7)E11ter 7 fo1· exiting file--4 '
j
Displayi11g all p1-oduct dctnils
[1, 'Powder', '17/02/2020', 20, 10]

(vii)1)E11ter 1 fo1· ndding 11ew p1·oduct details.


2)E11ter 2 fo1- displaying details of }J1·oduct according to product
nu111ber.
3)E11ter 3 to sea1-cl1 details fo1· p1·oduct of particular date.
4)Enter 4 for displaying details of p1·oducts.
s)Enter s for deleting product of a pa1ticular product number.
6)Enter 6 for updati11g details of product with specified product
nun1ber.
7)Enter 7 for exiti11g file--7

le11:C FILES stack


ro rams

P49.Write details of any e1nployee .. eno, naine, saJa1·y


to the file emp.csv
Display the details
iIn J)O 11 CSV
L=['E.no. ','name', 'salary']
f= open('student.csv', 'w')
A=csv.writer(f,delimiter =',')
A. writerow(L) I

l=[] I
I I

,vl1ilc True: i I
a =str(inpttt('cntcr e1111>loycc no.'))
b =str(i11pt1t('cnte1· nt1111e'))
c=str(i11pt1t('e11tcr snli11)''))
l.append(a)
l.append(b)
l.append(c)
A.writerow(l)
I= []
p =i11put('do you ,va11t to enter anotl1er detail')
if p! ='y':
break
f.close()

f= open('student.csv', 'r')
A=csv.reader(f)
for B in A:
print(' '.join(B))

OU'fPUT:
(i)Enter employee number--1
Enter name--Chaitanya
Enter salary--20000
Do you want to enter another detail?:y
Enter employee number--2
Enter naine--Akshat
Enter salary--25000
Do you want to enter another detail?:n
(ii)E.no. name salary
1 Chirag 20000
2 Akshat 25000
-- -- ~-- -- --- - - - ~- - - --- - -- - ·-- --- - ----------- -

p o.
i111port csv
field = ['roll no' ,'na1ne','marlcs']
rows=[['1', 'abc','34.5'],['2','pqr' ,' 56.5'],['3', 'xyz', '98.5']]
f=open(''student.csv'', ''w'')
fp =csv.writer(f,delimiter= ',')
fp. writerow(field)
fp.writerows(rows)
f.close()

i111po1·t CSV
r =ope11(''stude11t.csv'', ''r'')
rs=csv.reade1·(r)
fo1· w i11 rs:
p1·int(' '.join(w))
r.close()

OUTPUT:
rollno name marks
1 abc 34.5
2 pqr 56.5
3 xyz 98.5

P51.WAF PUSH_Fibo(N) which inserts the Fibonacci


values in the stock S[] till N terins passed as
arguinents to the function.
Fibonacci series :o t 1 2 3 5 ......N ter1ns. Display the
stack.

def PUSH_Fibo(N):
L= □
a=o '
b=l l

L.append(a)
L.append(b) •

C= O
- ---- ------ --- --- - ------ -

fo1~i i11 range(o,N-1):


c=a+b
L.append(c)
a=b
b =c
k=len(L)
I
if k==O: : I
,I
~I
pri n t('U nderflow') I .

for j i11 range(l<-1,-1,-1): '


'I
I

'I
print(L[j])

A=int(inpttt('Enter the limit of Fibonacci Series:'))


PUSH Fibo(A)

OUTPUT:
Enter the limit of Fibonacci Series:10
'

55 34 21 13 8 5 3 2 1 1 0 I II
I I

.I
'
I
'

P52.Write a Function POP_NAME(N) ,where Nis a list


of narnes pushed in a stock.The function pops the last
na1ne fro1n the stack and retu1·ns 1 if the element is
popped, else if the list is e01pty then it should display
the Inessage 'UnderFlow' and retu1·11 o.

def POP_NAME(N):
x=LEN(N)
k=o
if X == O :
print('Underflow')
else:
N.pop()
k= l
retu r·n k
L=eval(input('Enter List:'))
B=POP_ NAME(L)
print(A)
OUTPUT:
(i)Enter List:[1,2,3,4,5,6,7,8,9]
1
(ii)Enter List:[]
Underflow
0

P53. Write SQL queries for (i) to (iv) and find outputs for SQL queries (v)
to (viii), ,vhich nre bnsed on tl1e tables TRANSPORT nnd TRIP
TABLE: TRANSPORT
TCODE 1TYPE P13RKM
103 ORDINARY BUS 90
105 SUV 40
104 CAR 20
103 ORDINARY nus 90
101 VOLVO BUS 160
102 AC DELUXE nus 140

Note:

• PERKS is l~rcigl1t Cl1n1·t1gcs (Jcr kl lo111ctc1·


• l 'l "YPE is l '1•a11s1Jo1·t Vcl1iclc 'l~ypc

TABLE: TRIP
NO NAME ·roA"rE KM TCODE NOP
11 Thnish Khan 2015-12-13 200 101 32
13 Danish Sal,ai 2016-06-21 100 103 45
15 Ram Kumar 2016-02-13 350 102 .
~- -
LIi. "/ .

12 Fen Shen 2016-01-13 90 1r 40


17 AanI<umar 2015-02-10 75
V
/ 104 2

14 Veena 2016-06-28 80 / 105 4


/
16 Rajpal Kirti 2016-06-06 J.DC 101 25

Note: j

'

• NO is Driver Number
• KM is Kilometer travelled
• NOP is number of travellers travelled in vehicle
• TDA TE is Trip Date

1. To display NO, NAME, TDA TE fro1n the table TRIP in descending order of
l
NO.
2. To display the NAME of the drive1·s fro1n the table TRIP who are traveling
by transport vehicle with code l O1 or 103.
3. To display the NO and NAME of those drivers ·fro1n the table TRlP who
travelled between '2015-02-10' and '2015-04-01'.
4. ·1·(J displny ull tl1c dct,,ils fr<l111 t,1lllc ·1·1{11> ir1 \vl1icl1 tt1c di~t,11,cc lr,1vc llcd is
r11orc tl1n11 I 00 KM ir1 nsccr1<.lir1g <lr<.lcr <lf' NC )I>
5. Sl~Ll~c ·1· (_'()l JN'I' c•), 'l'l'()l)I ~1:r(llll ·1·1t11,
GllOlJll l)Y 1'(.'()1)1 ~ I IAVNINC 1 (.'()lJr1' I' (•) .. I;
(,. Sl•:l..,l ~C'I' I>IS'l'IN(,'I' 'l'C()l)l •: f'r<>111 '1'1{11•:
7. Sl~l ~l ~t ~·1· A .·1·c~()IJI •:, NAMI ~. 'J"l' Yl>I ~
1: l{()M '1'1{11• A, ·1·t{ANSl,()lt'I' 11
WI 11~1{1~ A . ·1·c.~C>IJI ~-:, ll . ·1·t,()l)I ~ ANI> KM <' <JC);
8. Sl~l ~l: t··1· NAMI ~. KM • t•l ~l{KM
1:1{C>M ·1·1~11> A, 'l'l{ANSJ><>l{'I' ll
WI 11~1{1~ A. '1'('01)1 ~= ll . 'l'CC) l)I ~ANI) A. 'l'C(>IJJ ~= I ()5';

Ar1s,,·c1·:

1. SELECl' NO. NAME, 'l'DA'l'I~ l•'l{OM 'l'llf I> ORDEI{ OY NO;


2. SELEC'I" NAMI~ 1: 1~0M rl.. lll P
WI IERE l 'CODE = 10 l Oil ~l'CODE = 103 ·
3. SELECl"' NO AND NAME PROM TRIP '
WI-IERE '2015-02-1 O' < 1.. DA'l' E < '2015-04-01';
4. SELECT NO, NAME, TDATE, KM, TCODE FROM TRIP
Wl-IERE KM > 100 ORDER BY NOP;
5. TO DISPLAY Tl-IE MORE Tl-IAN ONE COUNT OF TC E FROM Tf-lE
TABLE TRIP
6. TO DISPAL Y SEPERATE TCODE OF TABLET __
7. TO DISPA Y THE NAME AND CODE O .. TI-IO TRANS PORTERS,
WHO HA VE TRA YELLED MORE TI-IAN S.
8. TO DISPLAY THE NAME AND EXPEND ITARE OF A TRANSPORTER
WHO HA VE TCODE AS 105.

PS4 Write SQL query to add a column total price witl1 datatype numeric
and size 10, 2 in a table product.
Answer:
ALTER TABLE PRODUCT ADD TOTAL PRICE NUMBER (10,2).

PSS Sonal needs to display name of teachers, who have ''0'' as the third
character in their name. She wrote the following query.
SELECT NAME FROM TEACHER WHERE NAME = ''$$0?'';
But the query is'nt producing the result. Identify the problem.
Answer:
The wildcards are incorrect. The corrected query is SELECT NAME FROM
TEACHER WHERE NAME LIKE' 0%'. I

I

Write SQL queries for:
Table: Applicants
ID Name Course Phone
1 l~vi BCA 421()716
2 Rita MBA 215611
3 Sunll MCA 321157
4 Kumar BCA 512771
Tobie: Councs
Nam~ Duration
MBA 2 ycnr 4(),000
MCA 3 yc11r 70,000
BCA 3 ear 20,000

1. l "'o display 11u111c, tee, gc11dc1·, .ioi11yct11· tlbot1t tl1c applica11ts, who have joined
bctbrc 20 10.
2. To display na111cs ol" ,lpJJlict,11ts, wl10 a1·c JJlaying fee more than 30000.
3. To display na111cs oJ"all applica11ts i11 ascending order of their joinycar.
4. l ~o displny tl1e year n11d tl1e total 11u111be1· of applicants joined in each YEAR
fro111 tl1c table APPLICANTS.
5. To display tl1e C_ID (i.e., Cot11·selD) a11d tl1e number of applicants registered
in tl1e course fron1 tl1e APPLICANTS and table.
6. To display tl1e applicant's nan1e witl1 tl1eir respective course's name from the
tables APPLICANTS and COURSES.
7. Give the output of following SQL state111ents:
o SELECT Name, Joinyear FROM APPLICANTS
WHERE GENDER= 'F' and C_ID= 'A02';
o SELECT MIN (Joinyear) FROM
APPLICANTS
WHERE Gender='m';
o SELECT A VG (Fee) FROM APPLICANTS
WHERE C- ID= 'AOT OR C - ID= 'A05';
o SELECT SUM- (Fee), C_ID FROM C_ ID
GROUPBYC- ID
HA YING COUNT(*)=2;

Answer:

1. SELECT NAME,FEE,GENDER,JOINYEAR
FROM APPLICANTS
WHERE J OINYE AR <2010
2. SELECT NAME FROM APPLICANTS WHERE FEE > 30000
3. SELECT NAME FROM APPLICANTS ORDERBY JOINYE AR ASC
4. SELEC~r YfJAI{ , COUN'l 1

1:l{OM
)•)

APfll ~ICANTS GROUI> l3Y Yl~AI{;


5. SELECT C_ID, COUN'l·J•) 1:llOM
APPLICANTS, C()lJflSl~S Ull<)UJ> 13V II)
WI-IERl~ AJ>f>LJCAN'fS.C ID=COUllSliS. C II)
6. SEl.;ECT NAME,COlJllS E
- l·, llOM
-
AJ>Pl.;ICAN'l'S, COlJllSl~S
WI-J ERI~ APPl.,ICAN'fS . C I f)=C(>lJllSI ~S. C f I)
o Avisl1u 2()09
- -
0 2009
0 67
o 55000 AO I

P56 Write SQL co111111nnds for tl1c queries (i) to (iv) 11nd output for (v) &
(viii) bnscd 011 11 t11blc COMPANY and CUSTOMER.
COMl,ANY
CID NAME CITY PRO DU CTNAME
111 SONY DELl·ll TV
222 NOKIA MUMUAI MOBILE
333 ONIDA DELHI TV
444 SONY MUMBAI MOBILE
555 BLACKBERRY MADRAS MOBILE
666 DELL DELl1I LAPTOP
/
CUSTOMER
I• • •

CUSTID NAME PRICE QTY CID •

101 ROHANSHARMA 70,000 20 222


102 DEEPAK KUMAR 50,000 10 666
103 MOHAN KUMAR 30,000 5 111
104 SAHlL BANSAL 35,000 3 333
105 NEHASONI 25,000 7 ,144
106 SONAL AGGARWAL 20,000 5 333
107 ARUNSINGH 50,000 15 666

1. To display those company nrune which are having prize less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the prize by 1000 for those customer whose name starts with ,,S?
4. To add one more column totalprice with decimal] 10,2) to the table customer
5. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;
·---- - - - - -- - - - - -

6. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WI-IERE


QTY>IO;
7. SELEC'f AVG(QTY) FROM CUSTOMER WI IERE NAME LIKE ''o/or°/o;
8. SElJECT PRODUCTNAME,CITY, PRICE I
FROM COMPANY, CUSTOMER WIIERE I
COMPANY. CID=ClJSTOMER.CID AND
PRODUC1~ AM l:=''MOl3 IL I~'';

Ans,vcr:

I. SELEc·1. NAMl~1:llOM COMI>ANY WI ll~lll~


COMPANY.CID=C\JS1. 0Ml~ll. CII) ANI)
t>tllCL~ < 30000;
2. SELECT NAME 1-:ROM ("OMPANY Ollf)l~ll lJY NAME DESC;
3. UPDA l 'E cus·1. 0Ml~lt
SET PRIC'E = t>RICE + 1000
WI IERE NAME LIKii 'So/o';
4. ALTER l ' ABLE CUSTOMER
ADD 1. 01. ALPRICE DECIMAL( 10,2);
3 DELHI
2 MUMBAI
1 MADRAS
5.
6. 50000, 70000
7. 11
MOBILE MUMBAI 70000
MOBILE MUMBAI 25000
8.
il
I
I
I

P57 Answer the questions (o) and (b) on the basis of the following tables
SHOP andACCESSORIES.
Table: SHOP
Id ' SName Area
S001 ABC computronlcs er
5002 All lnfotech Media GK II
S003 Tech Shop er
S004 Geeks ll'Cno Soft Nel,ru Plac<?
5005 Hltech l ech Store Ncl,ru Pince
nble: ACCESSORIBS
No , Name Price Id
AOI Mother Boi\rd 12000 S01
A02 Hord Dbk 5000 S01
A03 Keyboard 500 S02
A04 Mouse 300 SOI
AOS Mother Board 1300(1 5()2
A06 Keyboard 400 S03
AO? LCD 6000 S04
T08 LCD 5500 sos
T09 Mouse 350 sos
TIO Hard Disk 4500 S03

(a) Write the SQL queries:

I. To display Naine and Price of all the Accessories in ascending order of their
Price.
2. To display Id and SName of all Shop located in Nehru Place.
3. To display Minimum and Maximum Price of each Name of Accessories.
4. To display Name, Price of all Accessories and their respective SName where
they are available.

(b) Write the output of the following SQL

1. SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE>


=5000;
2. SELECT AREA, COUNT(*) FROM SI-IOPPE GROUP BY AREA;
3. SELECT COUNT (DISTINCT AREA) FROM SHOPPE;
4. SELECT NAME, PRICE*0.05 DISCOUNT FROM ACCESSORIES
WHERE SNO IN ('S02',S03');

Answer:
(a)
1. Sl.:1. 1:(''I" Nutnc. l>ricc 1:ll(>M A('('l ·:SS()l{ll ~S ()l{l>l ~I{ llY J>ril:c A"c;
2. Sl~l. . 1:(,. 1. 11) SN~111,c 1:ll()M SI 1()1> WI 11 ~1{1 ·. Arc,, ··Ncl,rt1 l>l ,,cc··;
3. Sl: l.. l:c ·1· Nun1c, 111t1x (l>ricc); 111i11(l>ricc) 1:1t<>M A('C' l~S'-;() ltll ~S. Cir<>ltp l\y
Na,nc;
4. Sl: l,l:c ·1· Nt1111c,pricc, Stll\lllC 1:ll()M
A(,Ct":SS()l~ll :S, SI IC>J> WI 11~1{1 ~SI 1()1 ~
11)- A(~(,1:SSC>lt 11 ~S .11);
4

iI
I I
I
I I

I I
I
' I
I I
I
I
I I
I
I
I

'
I II
.I
' I
I '
: I

' i
I
. I
I :
I

I
I

I
I
I
I
-

You might also like