0% found this document useful (0 votes)
54 views11 pages

Xii CSC Practicals-Ii

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

EX.

NO – 4 CSV FILES
14. Result.csv is created to store the results of students in different sports.
The structure of Result.csv is :
[St_Id, St_Name, Game_Name, Result]
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'
Write the following user defined functions:
 Accept() – to accept a record from the user and add it to the file Result.csv. The column
headings should also be added on top of the csv file.
 wonCount() – to count the number of students who have won any event.
import csv
def accept():
f=open('result.csv','a',newline='')
y=1
while y==1:
id=int(input("Enter student ID-"))
name=input("Enter student name-")
g_name=input("Enter game name-")
res=input("Enter the result Won/Lost/Tie-")
data=[id,name,g_name,res]
wr=csv.writer(f)
wr.writerow(["Studentid","SName","GameName","Result"])
wr.writerow(data)
y=int(input("Whether you want to add more students. If yes press 1--"))
f.close()
def wonCount():
with open('result.csv','r') as f:
cnt=0
data=csv.reader(f)
for i in data:
if i[3].lower()=='won':
cnt+=1
print(i)
print("No. of students won any event is",cnt)
accept()
wonCount()
OUTPUT
Enter student ID-2005
Enter student name-SURAJ
Enter game name-CRICKET
Enter the result Won/Lost/Tie-WON
Whether you want to add more students. If yes press 1--1
Enter student ID-2158
Enter student name-YASER
Enter game name-FOOTBALL
Enter the result Won/Lost/Tie-LOST
Whether you want to add more students. If yes press 1--2
['2005', 'SURAJ', 'CRICKET', 'WON']
No. of students won any event is 1
EX.NO – 5 STACK
15. A list, NList contains following record as list elements:
[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the following user defined
functions in Python to perform the specified operations on the stack named travel.
 Push_element(NList): It takes the nested list as an argument and pushes a list
object containing name of the city and country, which are not in India and distance is less
than 3500 km from Delhi.
 Pop_element(): It pops the objects from the stack and displays them. Also, the function
should display “Stack Empty” when there are no elements in the stack.

NList=[["NewYork","U.S.A",11734],["Dubai","UAE",2194],
["London","England",6693],["Columbo","Srilanka",3405]]
travel=[]
def Push_element(NList):
for i in NList:
if i[1]!="India" and i[2]<3500:
travel.append([i[0],i[1]])
def Pop_element():
while len(travel):
print(travel.pop())
else:
print("Stack nempty")
Push_element(NList)
Pop_element()
OUTPUT
['Columbo', 'Srilanka']
['Dubai', 'UAE']
Stack nempty
16. Write a function in Python, Push (Vehicle) where, Vehicle is a dictionary containing details of
vehicles - {Car_Name: Maker}.
The function should push the name of car manufactured by "TATA' (including all the possible
cases like Tata, TaTa, etc.) to the stack.

vehicle={"Santro" : "Hyundai", "Nexon": "TATA", "Safari" : "Tata"}


stk=[]
def push(vehicle):
for i in vehicle:
if vehicle[i].lower()=='tata':
stk.append(i)
def pop():
while len(stk):
print(stk.pop())
else:
print("Stack nempty")
push(vehicle)
pop()
OUTPUT
Safari
Nexon
Stack nempty

EX NO – 6 PYTHON SQL CONNECTIVITY

17. Write a Python program that displays all records through Python Program. User Name as
‘Root’, Password as ‘coresql’, database as ‘ mysql’, Table name as ‘employee’]

import mysql.connector as sqltor


mycon=sqltor.connect(host="localhost",user="root",passwd="coresql",database="mysql")
mycur=mycon.cursor()
mycur.execute("select * from employee")
data=mycur.fetchall()
for i in data:
print()
mycon.close()

OUTPUT

18 Write a program to increase the salary of all employees by 1000, through Python program.
[User Name as ‘Root’, Password as ‘coresql’, database as ‘ mysql’, Table name as
‘employee’]
import mysql.connector as sqltor
mycon=sqltor.connect(host="localhost",user="root",passwd="coresql",database="mysql")
mycur=mycon.cursor()
mycur.execute("update employee set salary=salary+1000")
print("Changes done successfully")
mycon.commit()
mycon.close()

OUTPUT
Changes done successfully
19 Write a program to add a new record into the table, through Python program. [User Name
as ‘Root’, Password as ‘coresql’, database as ‘ mysql’, Table name as ‘employee’]. The
new record values has to be accepted from the user

import mysql.connector as sqltor


mycon=sqltor.connect(host="localhost",user="root",passwd="coresql",database="mysql")
mycur=mycon.cursor()
empno=int(input(“Enter Emp Number”))
ename= input(“Enter Name”)
job= input(“Enter Job”)
salary= int(input(“Enter Salary”))
mycur.execute("insert into employee values({}, ‘{}’, ‘{}’, {}”) . format (empno, ename
,job,salary)
print("Data added successfully")
mycon.commit()
mycon.close()

OUTPUT
Data added successfully
EX. NO.: 7 DDL COMMANDS
a) Write MySQL statement to create a database named FOOD.
b) Write MySQL statement to create a table named Nutrients based on the following
specification.
Column name Data type Constraints
P_ID Char(3) Primary key
Name Char(12)
Desig Char(15)
Salary Integer(6)
Allowance Integer(5)
c) Write MySQL statement to add a new column dept of suitable data type?
d) Write MySQL statement to display the attributes, its data type and constraints.
e) Write MySQL statement to remove the table from the database.
Ans: a) create database food;

b) create table Personal(P_ID char(3) primary key, Name char(12), Desig char
(15),Salary integer(6), Allowance integer(5));

c) alter table personal add dept integer(3);

d) desc personal;

e) drop table personal;

Output: a) Query OK, 1 row affected (0.03 sec)

b) Query OK, 0 rows affected (0.58 sec)

c) Query OK, 5 rows affected (0.42 sec)


Records: 5 Duplicates: 0 Warnings: 0

d)

6 rows in set (0.23 sec)

e) Query OK, 0 rows affected (0.14 sec)


EX.NO.: 8 SIMPLE DML COMMANDS
Consider the table Personal given below:
Table: Personal

Based on the given table, write SQL queries for the following:
a) To insert the details of ‘Ravina’ into the table.
b) Display the details of clerks.
c) Display all the different designations.
d) Display ID, name and salary of personals whose name contains ‘hi’ anywhere
and salary in the range 40000 to 90000.
e) Display the names of personals whose are getting salary.
f) Display the details of all supervisors and managers.
g) Display Name and Total Salary (sum of Salary and Allowance) of all
personals. The column heading ‘Total Salary’ should also be displayed.
h) To change the salary of clerk whose is getting null with 10000.
i) Increase the salary by 5% of all personals.
j) Delete the record of Supervisors who have salary greater than 25000.
Ans: a) insert into Personal values('P05','Ravina','Supervisor',null,2100);

b) select * from personal where desig='clerk';

c) select distinct(desig) from personal;

d) select P_ID, name, salary from personal where name like '%hi%' and salary
between 40000 and 90000;

e) select name from personal where salary is not null;

f) select * from personal where desig in ('supervisor','manager');

g) select name, salary+allowance "Total Salary" from personal;

h) update personal set salary=10000 where salary is null and desig='clerk';

i) update personal set salary=salary+(0.05*salary);

j) delete from personal where desig='supervisor' and salary>25000;


Output: a) Query OK, 1 row affected (0.11 sec)

b)

2 rows in set (0.06 sec)

c)

3 rows in set (0.02 sec)

d)

1 row in set (0.00 sec)

e)

3 rows in set (0.00 sec)

f)

3 rows in set (0.00 sec)

g)

5 rows in set (0.02 sec)

h) Query OK, 1 row affected (0.09 sec)


Rows matched: 1 Changed: 1 Warnings: 0
i) Query OK, 3 rows affected (0.05 sec)
Rows matched: 5 Changed: 3 Warnings: 0
j) Query OK, 1 row affected (0.07 sec)
EX.NO.: 9 DML COMMANDS – GROUP BY, ORDER BY AND AGGREGATE
FUNCTIONS
Based on the SQL table CAR_SALES, write suitable queries for the following

a) Display the highest and lowest sales in the first quarter.


b) Display the number of records in the table car_sales.
c) Display the number of different segment of cars.
d) Display the average sales in the second quarter of all SUV’s.
e) Display the total sale of all diesel cars in the first quarter. The column heading
should be ‘TOT SALE’.
f) Display the records in the descending order of sales in the second quarter.
g) Display fuel wise average sales in the first quarter.
h) Display segment wise highest sales in the second quarter.
a) select max(qt1),min(qt1) from car_sales;

b) select count(*) from car_sales;

c) select count(distinct segment) from car_sales;

d) select avg(qt2) from car_sales where segment='SUV';

e) select sum(qt1) "TOT SALE" from car_sales where fuel='Diesel';

f) select * from car_sales order by qt2 desc;

g) select fuel,avg(qt1) from car_sales group by fuel;

h) select segment,max(qt2) from car_sales group by segment;


Output: a)

1 row in set (0.02 sec)


b)

1 row in set (0.01 sec)

c)

1 row in set (0.04 sec)

d)

1 row in set (0.03 sec)

e)

1 row in set (0.00 sec)

f)

8 rows in set (0.01 sec)

g)

2 rows in set (0.01 sec)


h)

4 rows in set (0.00 sec)


EX.NO.: 10 QUERIES WITH TWO TABLES – JOINS
Consider the tables PRODUCT and BRAND given below:
Table: PRODUCT

Table: BRAND

Write SQL queries for the following:


a) Display product name and brand name from the tables PRODUCT and
BRAND.
b) Display product name, brand name and unit price from the tables PRODUCT
and BRAND whose unit price is more than 50.
c) Display the average rating of Medimix and Dove brands.
d) Display all the brand names with sum of unit price.
e) Display the name, price, and rating of products in descending order of rating.
a) select PName, BName from product p, brand b where p.BID=b.BID;

b) select PName,BName,Uprice from product p, brand b where p.BID=b.BID and


UPrice>50;

c) select BName,avg(Rating) from product p, brand b where p.BID=b.BID and


b.BName in('Medimix','Dove') group by p.BID;

d) select p.bid,bname,sum(uprice) from product p,brand b where p.BID=b.BID group


by p.BID;

e) select pname,uprice,rating from product order by rating desc;


Output: a)

b)

c)

d)

e)

You might also like