Computer Science
Computer Science
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
Output
Text
Output
Output
Output
Output
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
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
Output
Mysql Output
Output
Mysql Output
(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:
(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:
(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:
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;