0% found this document useful (0 votes)
29 views25 pages

Cs Program File

Cs program files

Uploaded by

varunhumain1
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)
29 views25 pages

Cs Program File

Cs program files

Uploaded by

varunhumain1
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

Index

 Programs In Python

 Program to find largest number in list with the help of user defined
function.
 Program to input any string and count & display total number of lower
case letters, uppercase letters and digits present in the string.
 Program to input numbers in a list display and count the numbers of odd
elements in the list.
 Program to accept a sentence by the user count and display number of
words , number of characters and Convert each lowercase alphabet to
uppercase and each uppercase alphabet to lowercase.
 Program to display n numbers of Fibonacci series.
 Program to enter elements in a tuple and find out the sum of those
elements which has 3 in unit place.
 Program to sort the elements of list with the help of insertion sort
method.
 Program to reverse the elements of list.
 Program to sort the elements of list with the help of Buble sort method.
 Program to find out the sum of list elements which is divisible by 5.
 Program to input name and percentage of n students in dictionary and
display those students names who's percentage is more than 85 using
functions.
 Program to read the content from a text file [Link] . Count and display
total no. of words present int the file and display total no. of words
having exactly 4 characters.
 A binary file [Link] has structure[book no,book name,author,price].
Write a user defined function createfile() to input data and add to
[Link] and also write a fuction countrecord(author)in python which
excepts the author name as parameter and count and return no. of
books by the given author stored in file [Link]
 Program to create a CSV file [Link] to stor n records of books and
display all the details in the given format:
Book no. Book name Quantity Price
Structure of the file: [Bookno,Bookname,Quantity,Price]
 Implimentation of stack in python using list.
 Program in python that defines and calls the following user defined
functions:
1) courier_add(): it takes the values from the user and adds the details
to a binary file '[Link]'. Each record consist of a list with field
elements as cid,s_name,source and destination.
2) courier_search(): takes the destination as the input and displays all
the courier records going to that destination.

 Connectivity program

 Program to create a table product in database inventory using mysql


connectivity with python.
 Program to display records of all the product whose price is more then
the inputed price.
 Program to update the record of product of inputed pid.
 Program to insert a new record in table product.

 SQL queries
Q ) Program to find largest number in list with the help of user defined
function.

def big(x):
p=0
for i in x :
if i > p:
p=i
return (p)
l=eval(input('enter the list:'))
n=big(l)
print('the list is',l)
print('the largest number in the list is',n)
Q ) Program to input any string and count & display total number of
lower case letters, uppercase letters and digits present in the string.

def detail(x):
l=0
L=' '
u=0
U=' '
d=0
D=’ ‘
for i in x :
if [Link]():
l=l+1
L=L+i
elif [Link]():
u=u+1
U=U+i
elif [Link]():
d=d+1
D=D+i
print('total no. of lower letters are',l,'and letters are',L)
print('total no. of upper letters are',u,'and letters are',U)
print('total no. of digits are',d,'and digits are',D)
a=input('enter the string:')
print('the string is',a)
detail(a)
Q ) Program to input numbers in a list display and count the numbers of
odd elements in the list.

def odd(x):
p=0
q=[ ]
for i in x:
if i%2!=0:
p=p+1
[Link](i)
print('total no. of odd elements are',p,'and elements are',q)
l=eval(input('enter the list:'))
print ('the list is',l)
odd(l)
Q ) Program to accept a sentence by the user count and display number
of words , number of characters and Convert each lowercase alphabet to
uppercase and each uppercase alphabet to lowercase.

def master(x):
w=[Link]()
c_w=len(w)
n_ch=0
for i in w:
for j in i:
n_ch=n_ch+1
r=[Link]()
print('total no. of words are',c_w,'and words are',w)
print('total no. of characters are',n_ch)
print('new sentence is',r)
a=input('enter the sentence:')
print('the sentece is',a)
master(a)
Q ) Program to display n numbers of Fibonacci series.

def fibo(x):
l=[0,1]
if x==1:
l=[0]
elif x==0:
l=[]
for i in range(1,x-1):
a=l[i-1]+l[i]
[Link](a)
return(l)
n=int(input('enter the value of n:'))
m=fibo(n)
print('the fibonacci series is',m)
Q ) Program to enter elements in a tuple and find out the sum of those
elements which has 3 in unit place.

def o_sum(x):
s=0
for i in x:
if i%10==3:
s=s+i
return(s)
t=eval(input('enter the tuple:'))
print('the tuple is',t)
a=o_sum(t)
print('the sum of elements with 3 in unit place is',a)
Q ) Program to sort the elements of list with the help of insertion sort
method.

def ins_sort(x):
n=len(x)
for i in range(1,n):
j=i-1
k=x[i]
while j>=0 and k<x[j]:
x[j+1]=x[j]
j=j-1
x[j+1]=k
return(x)
l=eval(input('enter the list:'))
print('the list is',l)
a=ins_sort(l)
print('sorted list is',a)
Q ) Program to reverse the elements of list.

def rev(x):
n=len(x)
for i in range(0,n//2):
x[i],x[n-1]=x[n-1],x[i]
n=n-1
return(x)
l=eval(input('enter the list:'))
print('original list is',l)
a=rev(l)
print('reversed list is',a)
Q ) Program to sort the elements of list with the help of Buble sort
method.

def bub_sort(x):
n=len(x)
for i in range(0,n):
for j in range(0,n-1):
if x[j]>x[j+1]:
x[j],x[j+1]=x[j+1],x[j]
return(x)
l=eval(input('enter the list:'))
print('the list is',l)
a=bub_sort(l)
print('the sorted list is',a)
Q ) Program to find out the sum of list elements which is divisible by 5.

def sum(x):
s=0
for i in x:
if i%5==0:
s=s+i
return(s)
l=eval(input('enter the list:'))
print('the list is',l)
a=sum(l)
print('the sum of elements divisible by 5 is',a)
Q ) Program to input name and percentage of n students in dictionary
and display those students names who's percentage is more than 85
using functions.

def detail(x):

k=[Link]()
l=[]
for i in k:
if [Link](i)>85:
z=i
[Link](z)
return(l)
n=int(input('enter the value of n:'))
d={}
for j in range(0,n):
a=input('enter the name:')
b=int(input('enter the percentage :'))
d[a]=b
print('complete details are',d)
p=detail(d)
print('students above 85 percent are',p)
Q ) Program to read the content from a text file [Link] . Count and
display total no. of words present int the file and display total no. of
words having exactly 4 characters.

f1=open('[Link]','r')
st=[Link]()
c=0
s=[Link]()
w_4ch=[]
for i in s:
if len(i)==4:
c=c+1
w_4ch.append(i)
print('words are',s)
print('total no. of words are',len(s))
print('total no. of words with 4 characters are',c)
print('words with 4 characters are',w_4ch)
[Link]()
Q) A binary file [Link] has structure[book no,book name,author,price].
Write a user defined function createfile() to input data and add to [Link]
and also write a fuction countrecord(author)in python which excepts the
author name as parameter and count and return no. of books by the given
author stored in file [Link]

import pickle
def createfile():
f=open('[Link]',.'ab')
b_no=int(input('enter book no.'))
b_n=input('enter book name')
a=input('enter book author')
p=int(input('enter book price'))
r=[b_no,b_n,a,p]
[Link](r,f)
[Link]()
def countrecord(author):
f=open('[Link]',.'ab')
c=0
try:
while True:
l=[Link](f)
if l[2]==author:
c=c+1
except:
[Link]()
return(c)
print('main menu')
while True:
print('1 for adding record')
print('2 for displaying')
ch=input('enter your choice')
if ch=='1':
createfile()
elif ch=='2':
a1=input('enter author to be searched')
countrecord(a1)
else:
break
Q) program to create a CSV file [Link] to store n records of books and
display all the details in the given format:
Book no. Book name Quantity Price
Structure of the file: [Bookno,Bookname,Quantity,Price]

import csv
def creation():
f=open('[Link]','w')
c=[Link](f)
n=int(input('enter no. of books'))
for i in range(0,n):
b=int(input('enter book no.'))
bn=input('enter book name')
q=int(input('enter book quantity'))
p=int(input('enter book price'))
r=[b,bn,q,p]
[Link](r)
[Link]()
def accessing():
f=open('[Link]','r')
r=[Link](f)
print('Book no.\t','Book name\t','Quantity\t','Price')
for i in r:
print(i[0]\t,i[1]\t,i[2]\t,i[3])
[Link]()
creation()
accessing()
Q) implimentation of stack in python using list

def isempty(st):
if st==[]:
return True
else:
return False
def spush(st,item):
[Link](item)
top=len(st)-1
def spop(st):
if isempty(st):
print('stack is underflow')
else:
x=[Link]()
print('deleted element is',x)
if len[st]==0:
top=None
else:
top=len(st)-1
def speek(st):
if isempty(st):
print('stack is underflow')
else:
top=len(st)-1
return(st[top])
def sdisplay(st):
if isempty(st):
print('stack is underflow')
else:
top=len(st)-1
print(st[top],'<---top')
for i in range(top-1,-1,-1):
print(st[i])
stack=[]
top=None
while True:
print('---stack operation---')
print('1 for push')
print('2 for pop')
print('3 for peek')
print('4 for display')
print('5 to exit')
ch=int(input('enter item'))
if ch==1:
n=int(input('enter item'))
spush(stack,n)
elif ch==2:
spop(stack)
elif ch==3:
x=speek(stack)
if x==None:
print('stack is empty')
else:
print('top most element is',x)
elif ch==4:
sdisplay(stack)
elif ch==5:
break
print('stack operation finished')
Q) program in python that defines and calls the following user defined
functions:
1) courier_add(): it takes the values from the user and adds the details to
a binary file '[Link]'. Each record consist of a list with field elements
as cid,s_name,source and destination.
2) courier_search(): takes the destination as the input and displays all
the courier records going to that destination.

import pickle
def courier_add():
f1=open('[Link]','wb')
cid=int(input('enter courier id'))
sn=input('enter name')
so=input('enter source')
des=input('enter destination')
l=[cid,sn,so,des]
[Link](l,f1)
[Link]()
def courier_search(x):
f2=open('[Link]','rb')
l=[Link](f2)
try:
while True:
if i[3]==x:
print(i)
except:
[Link]()
print('enter menu')
while True:
print('to add press 1 & to search ptess 2')
n=int(input('enter value:'))
if n==1:
courier_add()
elif n==2:
d= input('enter destination')
print('records with same destinatiion are')
courier_search(d)
else:
break
Q)Program to create a table product in database inventory using mysql
connectivity with python.

import [Link] as mys


mycon =
[Link](passwd='hello',host='localhost',user='system',database=
’inventory’)
myc=[Link]()
[Link](‘create table product (pid varchar(5) primary key,pname
char(30),price float,rank varchar(2)’)
[Link]()
Q)Program to display records of all the product whose price is more then
the inputed price.

import [Link] as mys


mycon =
[Link](passwd='hello',host='localhost',user='system',database=’inv
entory’)
myc=[Link]()
prize=float(input(‘enter the price:’))
[Link](‘select * from product where price>{}’.format(prize,))
data=[Link]()
for row in data:
print(‘the records are-‘)
print(row)
[Link]()
Q)Program to update the record of product of inputed pid.

import [Link] as mys


mycon =
[Link](passwd='hello',host='localhost',user='system',database=’inv
entory’)
myc=[Link]()
def upd_pname(x):
n=input(‘enter new value:’)
[Link](‘update product set pname=’{}’ where
pid=’{}’’.format(n,x))
def upd_price(x):
n=input(‘enter new value:’)
[Link](‘update product set price=’{}’ where
pid=’{}’’.format(n,x))
def upd_rank(x):
n=input(‘enter new value:’)
[Link](‘update product set rank=’{}’ where
pid=’{}’’.format(n,x))
prodid=input(‘enter the pid:’)
print(‘to update pname press 1’)
print(‘to update price press 2’)
print(‘to update rank press 3’)
n=int(input(‘enter your choice:’))
if n==1:
upd_pname(proid)
elif n==2:
upd_price(proid)
elif n==3:
upd_rank(proid)
[Link]()
[Link]()
Q) Program to insert a new record in table product.

import [Link] as mys


mycon =
[Link](passwd='hello',host='localhost',user='system',database=’inv
entory’)
myc=[Link]()
pid=input(‘enter the pid:’)
pname=input(‘enter the pname:’)
price=int(input(‘enter the price:’))
rank=input(‘enter the rank:’)
[Link](‘insert into product
values(‘{}’,’{}’,{},’{}’)’.format(pid,pname,price,rank,)
[Link]()
[Link]()
Q) To write Queries for the following fuctions based on the given table:

Rollno Name Gender Age Dept DOA Fees


1 Arun M 24 CS 1997-01-10 120
2 Ankit M 21 History 1998-03-24 200
3 Anu F 20 Hindi 1996-12-12 300
4 Bala M 19 Null 1999-07-01 400
5 Charan M 18 Hindi 1997-09-03 250
6 Deepa F 19 History 1997-06-27 300
7 Dinesh M 22 CS 1997-02-21 210
8 Usha F 23 Null 1997-07-31 200

1) Write a Query to create a Create new database Students.


Create database Students;

2) Write a Query to open a database students.


Use database students;

3) Write a query to create above table called: Info.


Create table Info (Rollno int Primary key, Name Varcharl(10), gender
varchar (3), Age int, Dept varchar (15), DOA date, Fees int);

4) Write a Query to list all the existing database names.


Show databases;

5) Write a Query to list all the tables that exists in the current database.
Show tables;

6) Write a Query to insert 3 rows of above tables into info table.


Insert into Info values (1, 'Arun', 'M', 24, 'CS', '1997-01-10', 120);
Insert into Info values (2, 'Ankit', 'M', 21, 'History', '1998-03-
14',200);
Insert into Info values (3, 'Anu', 'F', 20, 'Hindi', '1996-11-11' ,300);

7) write a Query to display all the details of the students from table Info.
select * from Into;
8) Write a Query to display Roll no ,Name & Department of the students
from Info table.
Select Rollno, Name, Dept from Info;

9) Write a Query to tell total no. of Male and Female students.


Select Gender,count(*) from Info group by Gender;

10) Write a Query to display records of students in ascending order of


their age.
Select * from Info order by Age;

11) Write a Query to display records of students whose department is CS.


Select * from Info where Dept='CS';

12) Write a Query to display records of students whose fees is greater


than 200.
Select * from Info where Fees > 200;

13) Write a Query to decrease the fees of students whose DOA is less
than 1997 by 50.
Update Info Set Fees = Fees-50 where DOA < '1997-01-01';

14)Write a Query to display details of all students whose Name ends with
‘a’ and is at least 3 character long.
Select * from info where Name like ‘_ _%a’;

15) Write a Query to delete records of students whose Dept is Hindi.


Delete from Info where Dept='Hindi';

You might also like