0% found this document useful (0 votes)
15 views

Computer Science

Uploaded by

Yash
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Computer Science

Uploaded by

Yash
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Computer Science

1. PROGRAM TO PERFORM ARITHMETIC OPERATION BASED ON


USER CHOICE
print("1 for add")
print("2 for minus")
print("3 for multiply")
print("4 for divide")
a=input("Enter the operation to perform:-")
b=int(input("Enter your first number:"))
c=int(input("Enter your second number:"))
if a=="1":
print("result is:",b+c)
elif a=="2":
if b>c:
print("result is:",b-c)
else:
print("result is:",c-b)
elif a=="3":
print("result is:",b*c)
elif a=="4":
if c==0 or b==0:
print("Enter numbers other than 0")
else:
print("Press 1 for",b,"/",c,"or 2 for",c,"/",b)
d=int(input("Enter the number:"))
if d==1:
print(b/c)
elif d==2:
print(c/b)
else:
print("Wrong input")
else:
print("miscellaneous operation")
Output
2. CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI
SERIES
a=0
b=1
print("how many fibonacci numbers do you want?")
n=int(input("Enter the numbers needed:"))
if n<=0:
print("Kindly enter positive integers for correct output")
else:
print(a)
print(b)
for i in range(2,n):
c=a+b
a=b
b=c
print(c)

Output
3. CREATING PROGRAM T0 FIND FACTORIAL AND SUM OF LIST
OF NUMBERS USING PYTHON
def factorial(n):
f=1
if n<0:
print("sorry,we cannot take negative number")
elif n==0:
print("The factorial of 0 is 1")
else:
for i in range(1,n+1):
f=f*i
print("The factorial of",n,"is",f)
def sum_list(n):
s=0
l=[]
for i in range(n):
s=s+i
l.append(i)
print("the sum of list",l,"is:",s)
print("1. To find factorial")
print("2. To find sum of the list is:")
a=int(input("enter your choice:"))
if a==1:
n1=int(input("Enter the number you want to get factorial of:"))
factorial(n1)
elif a==2:
l=[]
n2=int(input("How many number you want in the list?"))
sum_list(n2)

Output
4. CREATING A PYTHON PROGRAM TO IMPLEMENT 3
MATHEMATICAL FUNCTIONS
import math
print("1 for square root")
print("2 for power")
print("3 for sine")
a=int(input("Which mathematical function you want to use?"))
n=int(input("Enter number:"))
if a==1:
b=math.sqrt(n)
print("square root of",n,"is",b)
elif a==2:
c=int(input("enter the power for your number"))
d=math.pow(n,c)
print("result of power on",n,"is",d)
elif a==3:
e=math.sin(n)
print("sine of",n,"is",e)
else:
print("Wrong input")

Output

5. PROGRAM TO GENERATE RANDOM NUMBER WHILE


ROLLING THE DICE
import random
print("do you want to roll the dice?")
print("yes or no")
a=input(":")
while a=="yes":
print("your number is",random.randint(1,6))
print("do you want to roll the dice?")
print("yes or no")
a=input(":")
else:
print("ok")

Output

6. CREATING A PYTHON PROGRAM TO READ A TEXT FILE LINE


BY LINE AND DISPLAY EACH WORD SEPARATED BY '#'
a=open("C:Users\\yash\\text.txt","r")
d=a.readlines()
for i in d:
f=i.split()
for j in f:
print(j+"#",end="")

Text

Output

7. CREATING A PYTHON PROGRAM TO READ A TEXT FILE


AND DISPLAY THE NUMBER OF VOWELS/ CONSONANTS/
LOWER CASE/ UPPER CASE CHARACTERS.
f=open("C:Users\\yash\\text.txt","r")
c=f.read()
v=0
j=0
l=0
u=0
for i in c:
if i in "a,e,i,o,u,A,E,I,O,U":
v=v+1
if i in "b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z,B,C,D,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z":
j=j+1
if i.islower():
l=l+1
if i.isupper():
u=u+1
f.close()
print("number of times vowels apper in sorry.txt is:",v)
print("number of times vowels consonents in sorry.txt is:",j)
print("number of times letter in lowercase in sorry.txt is:",u)
print("number of times letter in uppercase in sorry.txt is:",l)

Output

8. CREATING A PYTHON PROGRAM TO COPY PARTICULAR


LINES OF A TEXT FILE INTO AN ANOTHER TEXT FILE
f=open("C:Users\\yash\\text.txt","r")
a=open("copy.txt","w")
b=f.readline()
for i in b:
if i=="I":
a.write(b)
print("lines are copied from sorry.txt to copy.txt starting with letter I")
f.close()
a.close()
print("...")
g=open("copy.txt","r")
x=g.readlines()
print(x)
g.close()

Output

9. CREATING A PYTHON PROGRAM TO CREATE AND SEARCH


RECORDS IN BINARY FILE
import pickle
a=open("a.dat","ab")
o="y"
while o=="y":
r=int(input("enter roll number:"))
n=input("enter name")
l=[r,n]
pickle.dump(l,a)
o=input("do you want to add more record")
a.close()
b=open("a.dat","rb")
try:
while True:
c=pickle.load(b)
print(c)
except EOFError:
b.close()
c=open("a.dat","rb")
z=int(input("enter the roll number you want to find"))
try:
while True:
x=pickle.load(c)
if x[0]==z:
print(x)
break
except:
c.close()

Output

10. CREATING A PYTHON PROGRAM TO CREATE AND


UPDATE/MODIFY RECORDS IN BINARY FILE
import pickle
a=open("a.dat","wb")
o="y"
while o=="y":
r=int(input("enter roll number:"))
n=input("enter name:")
l=[r,n]
pickle.dump(l,a)
o=input("do you want to add more record:")
a.close()
b=open("a.dat","rb")
try:
while True:
c=pickle.load(b)
print(c)
except EOFError:
b.close()
k=input("do you want to modity record...?(y/n):")
print("=============")
print("UPDATE PORTAL")
print("=============")
while k=="y":
c=open("a.dat","rb+")
z=int(input("enter the roll number you want to modify name:"))
try:
while True:
p=c.tell()
x=pickle.load(c)
if x[0]==z:
print("the search roll_no is found and details are:",x)
x[1]=input("enter the new name:")
print("name is sucessfully modified",x)
pickle.dump(c,z)
found=1
break
except:
c.close()
k=input("do you want to modify more records?...(y/n):")

Output
11. CREATING A PYTHON PROGRAM TO CREATE AND SEARCH
RECORDS IN BINARY FILE
import csv
a=open(r"C:\Users\raghav\Desktop\emp.csv","w",newline="")
b=csv.writer(a)
ch=”y”
while ch=="y":
i=int(input("enter employee id:"))
n=input("enter employee name:")
s=int(input("enter employee salary:"))
l=[i,n,s]
b.writerow(l)
ch=input("do you want to add more records?(y/n)")
a.close()
a=open(r"C:\Users\raghav\Desktop\emp.csv","r")
c=input("enter employee id that you want:")
f=csv.reader(a)
found=0
for i in f:
if i[0]==c:
print("details of the employee is:")
print(i)
found=found+1
break
if found==0:
print("no such employee exists.")
a.close()

Output

12. CREATING A PYTHON PROGRAM TO IMPLEMENT STACK


OPERATIONS
def push(st,sp,ele):
sp=sp+1
st.append(ele)
print("stack pointer",sp)
return sp
def pop(st,sp):
if st==-1:
print("under flow")
else:
print("popped element is:",st.pop())
return sp
def peek(st,sp):
if sp==-1:
print("under flow")
else:
print("topmost stack item is:",st[sp])
def display(st,sp):
if sp==-1:
print("under flow")
else:
print("stack items are:",st)
st=[]
sp=-1
print("stack operations:")
print("1.push")
print("2.pop")
print("3.peek")
print("4.display")
ch="y"
while ch=="y":
a=int(input("choose your stack operation:"))
if a==1:
ele=int(input("enter your element"))
sp=push(st,sp,ele)
elif a==2:
sp=pop(st,sp)
elif a==3:
peek(st,sp)
elif a==4:
display(st,sp)
else:
ch=input("do you want to continue?(y/n)")

Output
13. CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL
WITH PYTHON (INSERTING RECORDS AND DISPLAYING
RECORDS)
import mysql.connector
a=mysql.connector.connect(host="localhost",user="root",password="admin")
b=a.cursor()
c=b.execute("CREATE DATABASE IF NOT EXISTS employees")
c=b.execute("use employees")
c=b.execute("create table empl(empid int(10)primary key,empname varchar(20),empsalary int(12))")
ch="y"
while ch=="y":
n=int(input("enter employee id:"))
na=input("enter employee name:")
s=int(input("enter employee salary:"))
f=("insert into empl values(%s,%s,%s)")
values=[n,na,s]
b.execute(f,values)
print("record inserted")
a.commit()
ch=input("do you want to add more records?(y/n)")
print("YOUR DATA IS ADDED")
print("RECORD SAVED IN DATABASE ARE:")
b.execute("select* from empl")
a=b.fetchall()
for i in a:
print(i)

Output
Mysql Output

14. CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH


PYTHON (SEARCHING AND DISPLAYING RECORDS)
import mysql.connector
a=mysql.connector.connect(host="localhost",user="root",password="admin",database="employees")
b=a.cursor()
c=b.execute("use employees")
print("*********************")
print("MY SQL SEARCH PROGRAM")
print("*********************")
n=int(input("enter the employee id you want to search:"))
c=b.execute("select* from empl where empid={}".format(n))
d=b.fetchone()
if n!=None:
print(d)
else:
print("record not found")

Output

Mysql Output

15. CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL


WITH PYTHON (UPDATING RECORDS)
import mysql.connector
a=mysql.connector.connect(host="localhost",user="root",password="admin",database="employees")
b=a.cursor()
c=b.execute("use employees")
print("********************")
print("MY SQL UPDATE WINDOW")
print("********************")
ch=input("do you want to update record(y/n):")
while ch=="y":
i=int(input("enter the employee id you want to update"))
c=b.execute("select* from empl where empid={}".format(i))
d=b.fetchone()
print(d)
n=int(input("enter the employee salary you want to update:"))
c=b.execute("update empl set empsalary={} where empid={}".format(n,i))
a.commit()
print("record of the employee is updated")
c=b.execute("select* from empl")
d=b.fetchall()
for i in d:
print(i)
ch=input("do you want to update more records?(y/n)")

Output
Mysql Output

SQL COMMAND EXERCISE-1


AIM:
To write Queries for the following Questions based on the given table:
Roll no name gender age dept DOA Fees
1 Arun M 24 Computer 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-05 250
6 deepa F 19 History 1997-06-27 300
7 Dinesh M 22 Computer 1997-02-25 210
8 usha F 23 null 1997-07-31 200

(a) Write a Query to Create a new database in the name of "STUDENTS"


Sol:mysql> CREATE DATABASE STUDENTS;
(b) Write a Query to Open the database "STUDENTS"
Sol:mysql> USE STUDENTS;
(c) Write a Query to create the above table called: Info
Sol: CREATE TABLE STU(Rollno int Primary key,Name varchar(10),Gender varchar(3), Age
int,Dept varchar(15),DOA date,Fees int);
(d) Write a Query to list all the existing database names.
Sol:SHOW DATABASES;

(e) Write a Query to List all the tables that exists in the current database.
Sol: mysql> SHOW TABLES;

Output:

(f) Write a Query to insert all the rows of above table into Info table. Sol:
INSERT INTO STU VALUES (1,'Arun','M', 24,'COMPUTER','1997-01-10', 120); INSERT INTO
STU VALUES (2,'Ankit','M', 21,'HISTORY','1998-03-24', 200); INSERT INTO STU VALUES
(3,'Anu','F', 20,'HINDI','1996-12-12', 300);
INSERT INTO STU VALUES (4,'Bala','M', 19, NULL,'1999-07-01', 400);
INSERT INTO STU VALUES (5,'Charan','M', 18,'HINDI','1997-06-27', 250);
INSERT INTO STU VALUES (6,'Deepa','F', 19,'HISTORY','1997-06-27', 300);
INSERT INTO STU VALUES (7,'Dinesh','M', 22,'COMPUTER','1997-02-25', 210); INSERT INTO
STU VALUES (8,'Usha','F', 23, NULL,'1997-07-31', 200);
(g) Write a Query to display all the details of the Employees from the above table 'STU'.
Sol: mysql> SELECT * FROM STU;
OUTPUT:
(h) Write a query to Rollno, Name and Department of the students from STU table.
Sol: mysql> SELECT ROLLNO,NAME,DEPT FROM STU;
OUTPUT:

(a) Write a Query to select distinct Department from STU table.


Sol: mysql> SELECT DISTICT(DEPT) FROM STU;
Output:

(b) To show all information about students of History department.


Sol: mysql>SELECT * FROM STU WHERE DEPT='HISTORY';
Output:

(c) Write a Query to list name of female students in Hindi Department.


Sol: mysql> SELECT NAME FROM STU WHERE DEPT='HINDI' AND GENDER='F';
Output:

(d) Write a Query to list name of the students whose ages are between 18 to 20.
Sol: mysql> SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND20
OUTPUT:

(e) Write a Query to display the name of the students whose name is starting with 'A'.
Sol: mysql> SELECT NAME FROM STU WHERE NAME LIKE 'A%';
Output:

(f) Write a query to list the names of those students whose name have second alphabet 'n' in
their names.
Sol:mysql> SELECT NAME FROM STU WHERE NAME LIKE '_N%';
OUTPUT:

(a) Write a Query to delete the details of Roll number is 8.


Sol: mysql> DELETE FROM STU WHERE ROLLNO=8; Output (After Deletion):

(b) Write a Query to change the fess of Student to 170 whose Roll number is 1 and fess <130.
Sol:UPDATE STU SET FEES=170 WHERE ROLLNO=1 AND FEES<130;
Output(After Update):

(b) Write a Query to add a new column Area of type varchar in table STU.
Sol: mysql> ALTER TABLE STU ADD AREA VARCHAR(20);
Output:

(c) Write a Query to Display Name of all students whose Area Contains NULL.
Sol: mysql> SELECT NAME FROM STU WHERE AREA IS NULL;
Output:
(d) Write a Query to delete Area Column from the table STU.
Sol: mysql> ALTER TABLE STU DROP AREA;
Output:

(e) Write a Query to delete table from Database.


Sol: mysql> DROP TABLE STU;
Output:

SQL COMMAND EXERCISE-2


AIM:
To write Queries for the following Questions based on the given table:
TABLE:STOCK
PNO PNAME DCODE QTY UNITPRICE STOCKDATE
5005 Gel pen 101 150 12 2021-03-31
5005 Ball 102 100 12 2021-01-01
point
5002 Gel 103 125 4 2021-02-18
premium
5006 Pencil 104 200 6 2020-01-19
5001 Scale 105 210 6 2020-01-19

TABLE:Dealers
DCODE DNAME
101 Shakti stationalreies
102 Classic stationeries
103 Indian book house
104 Classic stationeries
105 Classic stationeries
106 Indian book house

(a) To display the total Unit price of all the products whose Dcode as 102.
Sol:mysql> SELECT SUM(UNITPRICE) FROM STOCK GROUP BY DCODE HAVING
DCODE=102;

OUTPUT:

(b) To display details of all products in the stock table in descending order of Stock date.
Sol: mysql> SELECT * FROM STOCK ORDER BY STOCKDATE DESC;
OUTPUT:

(c) To display maximum unit price of products for each deal individually as per dcode From the
table Stock.
Sol:mysql> SELECT DCODE,MAX(UNITPRICE) FROM STOCK GROUP BY DCODE;
Output:

(d) To display the Pname and Dname from table stock and dealers.
Sol: mysql> SELECT PNAME,DNAME FROM STOCK S,DEALERS D WHERE
S.DCODE=D.DCODE;

You might also like