5_6276252898103925673
5_6276252898103925673
5_6276252898103925673
Submitted by:
Student Name :
Class : XII - B
Subject : Computer Science (083)
INDEX
S.NO CONTENT
1 WAP to counts the number of alphabets, digits, uppercase, lowercase, spaces
and other characters (status of a string).
2 WAP to remove all odd numbers from the given list.
3 Program to enter any number and print factorial of this number. Also check
whether this number is prime number or not.
4 Program to find the max, min and mean of values from a inputted list.
5 Program to generate Fibonacci series.
6 Program to store student’s information in a dictionary and display
information on the basis of admission no.
7 Write a function roll_D (), that takes 2 parameters- the no. of sides (with
default
value 6) of a dice, and the number of dice to roll-and generate random roll
values
for each dice rolled. Print out each roll and then return one string “That’s
all”.
Example roll_D(6, 3)
⦿ 4
⦿ 1
⦿ 6
9 Write a program to display those string which are starting with ‘A’ from the
given list
10 Write a program to show sorting of elements of a list step-by-step.
11 Write a user defined function to find the greatest common divisor between
two numbers.
12 Write a user defined function to display first n prime numbers.
13 Program to calculate income tax of an employee on the basis of basic salary
and total savings inputted by the user.
14 WAP to calculates the following using concepts of module:
1. Area of Circle
2. Circumference of a circle
3. Area of rectangle
4. Perimeter of a rectangle
15 WAP to print the number of occurrences of a substring into a line using built
in string function find().
16 Write a program to show and count the number of words in a text file
‘DATA.TXT’ which is starting with a word ‘The’, ‘the’.
18 Write a program to enter the numbers and find Linear Search, Binary Search,
Lowest Number using array code with user defined functions.
la,ua,d,s,c=0,0,0,0,0
for i in x:
if i in 'qwertyuioplkjhgfdsazxcvbnm':
la=la+1
elif i in 'QWERTYUIOPLKJHGFDSAZXCVBNM':
ua=ua+1
elif i in '1234567890':
d=d+1
s=s+1
else:
c=c+1
print('No. of:')
print('Alphabet:',la+ua,'\nLoewcase:',la,'\n Uppsercase:',ua)
print('Digits:',d,'\nSpaces:',s,'\nOther characters:',c)
Output
while True:
x=int(input('Enter:'))
l.append(x)
if y=='n':
break
print('Original list:',l)
for i in l:
if i%2==0:
continue
else:
a=l.remove(i)
print('New list:',l)
Output
b=x
y=x//2
a=0
for i in range(2,y+1):
if x%i==0:
a=a+1
if a==0:
else:
f=1
for z in range(1,b+1):
f=f*z
print('Factorial of',b,'=',f)
Output
l=[]
ma=0
while True:
x=int(input('Enter:'))
l.append(x)
if y=='n':
break
if l[0]>l[1]:
ma=l[0]
mi=l[1]
else:
ma=l[1]
mi=l[0]
for i in range(2,len(l)):
if l[i]>=ma:
ma=l[i]
for i in range(2,len(l)):
if l[i]<=mi:
mi=l[i]
s=0
for p in l:
s+=p
print('Mean:',s/len(l))
Output
a=0
b=1
c=a+b
print(a,b,end=' ')
for i in range(1,x-1):
print(c,end=' ')
a=b
b=c
c=a+b
Output
while True:
n=input('Enter name:')
m=int(input('Enter marks:'))
d[a]=[n,m]
if q=='n':
break
def show():
y=d[x]
print('Adm no:',x,'Name:',y[0],'Marks:',y[1])
d={}
while True:
print('2.Show details')
print('3.Exit')
p=int(input('Enter choice:'))
if p==1:
detail()
elif p==2:
show()
elif p==3:
break
Output
def roll_D(side,dice):
import random
for i in range(dice):
x=random.randint(1,side)
print('Die',i+1,':',x)
print('That\'s all')
roll_D(a,b)
Output
l=len(x)
i=0
print('Original list:',x)
while i<l:
if x[i]%7==0:
x[i],x[i+1]=x[i+1],x[i]
i=i+2
else:
i=i+1
print('New list:',x)
Output
for i in l:
if i[0] in 'A':
print(i)
Output
print('1.Bubble sort')
print('2.Insertion sort')
print('3.Exit')
while True:
x=int(input('Enter choice:'))
if x==1:
l=[]
for p in range(a):
b=int(input('Enter no.:'))
l.append(b)
print('Original list',l)
n=len(l)
for i in range(n-1):
for j in range(n-i-1):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l)
if x==2:
l=[]
for p in range(a):
b=int(input('Enter no.:'))
l.append(b)
print('Original list',l)
for i in l:
j=l.index(i)
while j>0:
if l[j-1]>l[j]:
l[j-1],l[j]=l[j],l[j-1]
print(l)
else:
break
j=j-1
if x==3:
break
Output
if x>y:
s=y
else:
s=x
for i in range(1,s+1):
ans=i
return ans
a=int(input('Enter 1 no:'))
b=int(input('Enter 2 no:'))
print('HCF is',hcf(a,b))
Output
j=0
x=2
while j!=p:
y=x//2
a=0
for i in range(2,y+1):
if x%i==0:
a=a+1
if a==0:
print(x,end=' ')
x=x+1
j=j+1
else:
x=x+1
primes()
Output
if b<=250000:
tax=0
elif b<=500000:
if s>150000:
s=150000
tot_income=b-s-250000
tax=tot_income*0.05
elif b<=1000000:
if s>150000:
s=150000
tot_income_5slab=500000-s-250000
tot_income_20slab=b-500000
tax=tot_income_5slab*0.05+tot_income_20slab*0.02
else:
if s>150000:
s=150000
tot_income_5slab=500000-s-250000
tot_income_30slab=b-1000000
tax=tot_income_5slab*0.05+tot_income_30slab*0.03+100000
Output
Practical Assignment No.14
Program 14: WAP to calculates the following using concepts of
module:
1. Area of Circle
2. Circumference of a circle
3. Area of rectangle
4. Perimeter of a rectangle
Module Circle:
import math
def area(r):
return math.pi*(r**2)
def circumference(r):
return 2*math.pi*r
Module Rectangle:
def area(l,b):
return l*b
def perimeter(l,b):
return 2*(l+b)
Main program:
import Circle
import Rectangle
while True:
print('1.Area of Circle')
print('2.Circumference of Circle')
print('3.Area of Rectangle')
print('4.Perimeter of Rectangle')
print('5.Exit')
x=int(input('Enter choice:'))
if x==1:
elif x==2:
elif x==3:
l=int(input('Enter length:'))
b=int(input('Enter width:'))
elif x==4:
l=int(input('Enter length:'))
b=int(input('Enter width:'))
elif x==5:
break
else:
print('Invalid')
Output
Practical Assignment No.15
Program 15: WAP to print the number of occurrences of a substring
into a line using built in string function find().
x=input('Enter any string:')
c=0
i=0
while i<len(x):
if x.find(y,i)!=-1:
m=x.find(y,i)
i=m+1
c=c+1
else:
i=i+1
Output
Practical Assignment No.16
Program 16: Write a program to show and count the number of words
in a text file ‘DATA.TXT’ which is starting with a word ‘The’, ‘the’.
Data.txt:
I live in Delhi.
Main Program:
f=open('Data.txt','r')
a=f.readlines()
i=0
while i<=len(a)-1:
c=0
print(a[i])
for j in a[i]:
if j==' ':
c=c+1
i+=1
f.close()
Output
Practical Assignment No.17
Program 17: Consider a binary file “Emp.dat” containing details such
as empno: ename: salary(separator‘:’). Write a python function to
display details of those employees who are earning between 20000
and 40000.
import pickle
f=open('Emp.dat','rb')
x=pickle.load(f)
print(x)
for y in x:
print(y)
f.close()
Output
Practical Assignment No.18
Program 18: Write a program to enter the numbers and find Linear
Search, Binary Search, Lowest Number using array code with user
defined functions.
def linear_search():
l=[]
for i in range(x):
y=int(input('Enter no:'))
l.append(y)
print('Original list:',l)
f=0
p=0
while p<len(l):
if l[p]==n:
f=1
p=p+1
break
else:
p=p+1
if f==1:
else:
print(n,'not found')
def binary_search():
l=[]
for i in range(x):
y=int(input('Enter no:'))
l.append(y)
print('Original list:',l)
l.sort()
print('Sorted list:',l)
mid=len(l)//2
low=0
high=len(l)-1
if n>l[mid]:
low=mid+1
else:
high=mid-1
mid=(low+high)//2
if low>high:
print('Element is at None')
else:
print('Element is at',mid+1)
def lowest_no():
l=[]
while True:
x=int(input('Enter:'))
l.append(x)
if y=='n':
break
if l[0]>l[1]:
mi=l[1]
else:
mi=l[0]
for i in range(2,len(l)):
if l[i]<=mi:
mi=l[i]
print('Lowest no:',mi)
while True:
q=int(input('Enter choice:'))
if q==1:
linear_search()
elif q==2:
binary_search()
elif q==3:
lowest_no()
else:
break
Output
Practical Assignment No.19
Program 19: Write a function to create a copy of file “test.txt” named
as “file.txt” which should convert the first letter of the file and the
first alphabetic character following a full stop into upper case.
test.txt:
Main code:
def capital():
f1=open('test.txt','r')
f2=open('text.txt','w')
while True:
line=f1.readline()
if not line:
break
line=line.lstrip()
line=line.rstrip()
l=len(line)
s=''
s=s+line[0].upper()
i=1
while i<l:
if line[i]=='.':
s=s+line[i]
i=i+1
if i>=l:
break
s=s+line[i].upper()
else:
s=s+line[i]
i=i+1
f2.write(s)
f1.close()
f2.close()
capital()
Output
Practical Assignment No.20
Program 20: Anant has been asked to display all the students who
have scored less than 40 for Remedial Classes. Write a user-defined
function to display all those students who have scored less than 40
from the binary file “Student.dat” and copy their records to new file
“stud.dat”
def main():
import pickle
f=open('Student.dat','rb')
f1=open('Stud.dat','wb')
x=pickle.load(f)
print('All records:',x)
for y in x:
if y[2]<=40:
a=y
pickle.dump(a,f1)
print(a)
f.close()
f1.close()
main()
Output
Practical Assignment No.21
Program 21: Create a CSV file “Groceries” to store information of
different items existing in a shop. The information is to be stored
w.r.t. each item code, name, price, qty. Write a program to accept the
data from user and store it permanently in CSV file.
import csv
f=['Code','Name','Price','Quantity']
rows=[]
while True:
c=input('Enter code:')
n=input('Enter name:')
p=int(input('Enter price:'))
q=int(input('Enter quantity:'))
r=[c,n,p,q]
rows.append(r)
x=input('Enter more?')
if x in 'Nn':
break
filename='Groceries.csv'
with open(filename,'w',newline='')as y:
w=csv.writer(y)
w.writerow(f)
for i in rows:
w.writerow(i)
Output
Practical Assignment No.22
Program 22: Write a menu-driven program implementing user-
defined functions to perform different functions on a csv file
“student” such as:
(a) Write a single record to csv.
(b) Write all the records in one single go onto the csv.
(c) Display the contents of the csv file.
def onebyone():
import csv
fields=['Name','Class','Marks']
rows=[['Aman',12,95],
['Aditya',11,93],
['Adarsh',12,90],
['Gaurav',11,91],
['Vivek',12,92]]
with open('student.csv','w',newline='')as f:
w=csv.writer(f)
w.writerow(fields)
for i in rows:
w.writerow(i)
print('File written one by one')
def onego():
import csv
fields=['Name','Class','Marks']
rows=[['Aman',12,95],
['Aditya',11,93],
['Adarsh',12,90],
['Gaurav',11,91],
['Vivek',12,92]]
with open('student.csv','w',newline='')as f:
w=csv.writer(f)
w.writerow(fields)
w.writerows(rows)
print('File written in one go')
def display():
import csv
f=open('student.csv','r')
r=csv.reader(f)
for i in r:
print(i)
f.close()
while True:
print('1.Write records one by one')
print('2.Write records in one go')
print('3.Display')
print('4.Exit')
p=int(input('Enter choice:'))
if p==1:
onebyone()
elif p==2:
onego()
elif p==3:
display()
elif p==4:
break
else:
print('Invalid')
Output
Practical Assignment No.23
Program 23: WAP to add, delete and display the records using list
implementation through stack.
l=[]
while True:
print('1.Add Record')
print('2.Delete Record')
print('3.Display')
print('4.Exit')
x=int(input('Enter choice:'))
if x==1:
a=input('Enter a record:')
l.append(a)
elif x==2:
if l==[]:
print('Empty record')
else:
print('Record deleted:',l.pop())
elif x==3:
i=len(l)
print('Stack is:')
while i>0:
print(l[i-1])
i=i-1
elif x==4:
break
else:
print('Invalid')
Output
Practical Assignment No.24
Program 24: Write a Program to show MySQL database connectivity in
python to insert records in a table and display records.
def insert():
import mysql.connector
d=mysql.connector.connect(host='localhost',\
user='root',passwd='1234',\
database='school')
x=d.cursor()
d.commit()
print('Records inserted!!')
def show():
import mysql.connector
d=mysql.connector.connect(host='localhost',\
user='root',passwd='1234',\
database='school')
x=d.cursor()
x.execute('select*from student')
a=x.fetchall()
for b in a:
print(b)
while True:
print('1.Insert values')
print('2.Show details')
p=int(input('Enter choice:'))
if p==1:
insert()
elif p==2:
show()
else:
break
Output
Practical Assignment No.25
Program 25: WAP with My SQL and Python connectivity for searching
a record on a condition from a table.
import mysql.connector
d=mysql.connector.connect(host='localhost',\
user='root',passwd='1234',\
database='school')
x=d.cursor()
a=x.fetchall()
for y in a:
print(y)
Output
Practical Assignment No.26
Program 26: WAP with My SQL and Python connectivity for updating a
record on a condition from a table.
import mysql.connector
d=mysql.connector.connect(host='localhost',\
user='root',passwd='1234',\
database='school')
x=d.cursor()
d.commit()
Output
Practical Assignment No.27
Program 27: WAP with My SQL and Python connectivity for deleting a
record on a condition from a table.
import mysql.connector
d=mysql.connector.connect(host='localhost',\
user='root',passwd='1234',\
database='school')
x=d.cursor()
d.commit()
Output
Practical Assignment No.28
Program 28: WAP with My SQL and Python connectivity for showing
records on a condition from a table.
import mysql.connector
d=mysql.connector.connect(host='localhost',\
user='root',passwd='1234',\
database='school')
x=d.cursor()
a=x.fetchall()
for y in a:
print(y)
Output
My SQL Queries:
Practical Assignment No.29
Table: Suppliers
SCODE SNAME
21 Premium stationery
23 Soft plastics
22 Tetra supply
TO CREATE TABLE:
TO INSERT DATA :
(i). To display details of all the items in the store table in ascending order of Lastbuy.
Select * from store
Order by LastBuy;
(ii). To display itemno and item name of those items from store table whose rate is more than
15 rupees.
Select * from store
Where rate>15;
(iii). To display the details of those items whose supplier code is 22 or quantity in store is more
than 110 from table store.
Select * from store
Where scode=22 or qty>110;
(iv). To display minimum rate of items for each supplier individually as per scode from the table
store.
Select store.scode,suppliers.sname,min(store.rate)
From store,suppliers
Where store.scode=21;
Select store.scode,suppliers.sname,min(store.rate)
From store,suppliers
Where store.scode=22;
Select store.scode,suppliers.sname,min(store.rate)
From store,suppliers
Where store.scode=23;
TO INSERT DATA :
(ii) To display cost of all the books published for FIRST PUBL.
Select price,book_name from books
Where publishers = ‘first publ’;
(iii) To display the BOOK_NAME and price of the books, more than 3 copies of which have been
issued.
Select book_name , price from books
Where qty > 3;
(iv) To show total cost of books of each type.
Select type,sum(price) from books
Where type = ‘Comp’;
Table: Model
TO INSERT DATA :
Table:
(i) To display the details of all the models in the Model table in ascending order of
DateOfManufacture.
select * from model order by Date_Of_Manufacture;
(ii)To display the details of those models manufactured in 2011 and whose Cost is below 2000
select * from model where year(Date_Of_Manufacture) = 2011 and cost <
2000;
(iii) To decrease the cost of all the models in Model table by 15%.
Update Model
set Cost = Cost – 0.15*Cost;
(v) Show the company name and contact person from company table where comp_ho is okhla.
Select comp_name,contact_person from company
TO CREATE TABLE :
Create table carden
(Ccode integer(4),
Car_name varchar(10),
Make varchar(15),
Colour varchar(10),
Capacity integer(2),
Charges integer(3));
TO INSERT DATA :
Insert into carden Values(501,’A-Star’,’Suzuki’,’Red’,3,14);
Insert into carden Values(503,’Indigo’,’Tata’,’Silver’,3,12);
Insert into carden Values(502,’Innova’,’Toyota’,’White’,7,15);
Insert into carden Values(509,’SX4’,’Suzuki’,’Silver’,4,14);
Insert into carden Values(510,’S class’,’Mercedes’,’Red’,4,35);
Table:
(ii) To display name, make and capacity of cars in descending order of their sitting capacity.
SELECT CarName,Make,Capacity FROM carden ORDER BY Capacity DESC;
(iii) To display the highest charges at which a vehicle can be hired from CARDEN.
SELECT MAX(Charges) FROM carden;
(iv) Display the number of different MAKE from carden table.
SELECT COUNT(DISTINCT Make) FROM CARDEN;
Table: Orders
TO CREATE TABLE :
Create table Orders
(Order_id integer(5),
P_name varchar(10),
Qty integer(4),
Rate integer(4),
Sale_date date,
Discount integer(3));
TO INSERT DATA :
Insert into Orders Values(1001,’Pen’,10,20,’2019-10-05’,Null);
Insert into Orders Values(1002,’Pencil’,20,10,’2019-10-21’,Null);
Insert into Orders Values(1003,’Book’,10,100,’2019-11-02’,50);
Insert into Orders Values(1004,’Eraser’,100,5,’2019-12-05’,25);
Insert into Orders Values(1005,’Copy’,50,20,’2019-12-10’,Null);
Table:
(i) Write SQL query to display Pname, Quantity and Rate for all the orders that are either Pencil
or Pen.
SELECT P_name, Qty, Rate FROM Orders WHERE P_name IN(‘Pencil’,’Pen’);
(ii) Write SQL query to display the orders which are not getting any Discount.
SELECT * FROM Orders WHERE Discount is NULL;
(iii) Write SQL query to display the Pname, Quantity and Sale_date for all the orders whose total
cost (Quantity * Rate) is greater than 500.
SELECT P_name, Qty, Sale_date FROM Orders WHERE Qty * Rate > 500;
(iv) Write SQL query to display the orders whose Rate is in the range 20 to 100.
SELECT * FROM Orders WHERE Rate BETWEEN 20 AND 100;
(v) Write SQL query to display the minimum quantity, maximum
quantity and total quantity of Orders.
SELECT MIN(Qty), MAX(Qty), SUM(Qty) FROM Orders;