Python Ass.
Python Ass.
Assigmente :- 1
C:\Users\Admin>cd..
C:\Users>cd..
C:\>cd sqlite
C:\sqlite>sqlite3
1- Create table temp with one column name dt, insert 5 different type of records and check the type
of it in sorted order.
sqlite> begin;
sqlite> rollback;
sqlite> begin;
sqlite> commit;
3-Create table tblemp with fields Eid, Fname, Lname, Jdate, Salary, M_id, post.
create table dbstud.tblemp (Eid, Fname, Lname, Jdate, Salary, M_id, post);
11-Write a query to get all employee details from the employee table order by first name in
descending order.
12-Write a query to get the employee ID, fname, lname, salary in ascending order of salary.
13-Write a query to get the maximum and minimum salary from employees table.
14-Write a query to get the average salary and number of employees in the employees table.
15-Write a query get all first name from employees table in upper case.
16-Write a query to display the fname, lname and salary for all employees whose salary is not in the
range 10,000 through 15,000 and are in M_id 30 or 100.
select fname, lname, salary from tblemp where salary not between 10000 and 15000 and m_id=30
or m_id=100;
17-Write a query to display the last names of employees whose names have exactly 6 characters.
18-Write a query to display the last names of employees having 'e' as the third character.
19-Write a query to display the post of Manager and CEO in the employees table.
20-Write a query to display the fname of all employees who have both an "a" and "i" in their first
name.
select fname from tblemp where fname like '%a%i%' or fname like '%i%a%';
Assignment :- 2
import sqlite3 as sq
print("1")
cn=sq.connect("c:\sqlite\sales.db")
print("2")
cn.commit()
print("4")
for i in s2:
print(i)
print("5")
for i in s2:
print(i)
106-DHRUVIN GHORI Page 6
Database Handling Using Python
print("6")
a=s3.fetchall()
for i in a:
print(i)
print("7")
for i in s4:
print(i)
print("8")
for i in a:
print(i)
print("9")
for i in a:
print(i)
print("10")
for i in a:
print(i)
print("11")
for i in a:
print(i)
print("12")
for i in b:
print(i)
print("13")
for i in s5:
print(i)
print("14")
for i in s6:
print(i)
print("15")
print("16")
for i in s8:
print(i)
print("17")
for i in range(3):
for i in s9:
print(i)
cn.commit()
Assigment – 3
import sqlite3
cn=sqlite3.connect("c:\sqlite\sales.db")
print("1")
print("2")
cn.execute("create trigger trg_tblorder before insert on tblorder begin select case when
new.pur_amt<1000 then raise(abort,'not enough purchase amount.')end; end;")
print("trigger is created.")
print("3")
cn.commit()
print("5")
for i in a:
print(i)
print("6")
for i in a:
print(i)
print("7")
for i in a:
print(i)
print("8")
for i in a:
print(i)
print("9")
for i in a:
print(i)
print("10")
for i in a:
print(i)
print("11")
a=cn.execute("select *,(case when sid in(5001, 5002) then 'A' when sid in (5003,5005) then 'B' else
'C' end)as class from tblorder")
for i in a:
print(i)
print("12")
for i in a:
print(i)
print("13")
a=cn.execute("select * from salesman1 where sid not in (select sid from tblorder)")
106-DHRUVIN GHORI Page 11
Database Handling Using Python
for i in a:
print(i)
print("14")
for i in b:
print(i)
print("15")
for i in c:
print()
Assignment :- 4
import sqlite3
cn=sqlite3.connect("c:\sqlite\sales.db")
print("1")
print("2")
cn.execute("create trigger trg_customer before insert on customer begin select case when
new.grade not between 100 and 500 then raise (abort,'invalid grade.')end; end;")
print("triger is create.")
print("3")
print("5")
for i in a:
print(i)
print("6")
a=cn.execute("select cid, cust_name, grade,(case when grade=100 then 5000 when grade=200 then
10000 else 15000 end)as bouns from customer")
for i in a:
print(i)
print("7")
for i in a:
print(i)
print("8")
for i in a:
print(i)
print("9")
for i in s1:
print(i)
print("10")
for i in s1:
print(i)
print("11")
for i in range(3):
for i in s1:
print(i)
106-DHRUVIN GHORI Page 14
Database Handling Using Python
print("12")
for i in a:
print(i)
print("13")
for i in a:
print(i)
print("14")
for i in a:
print(i)
Assignment :- 5
print(Armstrongnumber(155),"\n\n")
print(Palidromenumber(155),"\n\n")
print(Reversenumber(155),"\n\n")
106-DHRUVIN GHORI Page 15
Database Handling Using Python
while(True):
if (choice==1):
insert.insert_data()
continue
elif (choice==2):
Delete.Delete_Data()
continue
elif (choice==3):
Update.Update_Data()
continue
elif (choice==4):
Display.Display_Data()
continue
elif (choice==5):
Search.Search_Data()
continue
elif (choice==6):
break
else:
print("Invalid input")
106-DHRUVIN GHORI Page 16
Database Handling Using Python
continue
def insert_data():
import sqlite3
con = sqlite3.connect('C:\sqlite\databasepro.db')
print("connected")
con.commit()
print("Record inserted")
def Delete_Data():
import sqlite3
con = sqlite3.connect('C:\sqlite\databasepro.db')
print("connected")
print("Record Deleted")
con.commit().
def Display_Data():
import sqlite3
con = sqlite3.connect('C:\sqlite\databasepro.db')
print("connected")
106-DHRUVIN GHORI Page 17
Database Handling Using Python
for i in a:
print(i)
def Update_Data():
import sqlite3
con = sqlite3.connect('C:\sqlite\databasepro.db')
print("connected")
con.commit()
def Search_Data():
import sqlite3
con = sqlite3.connect('C:\sqlite\databasepro.db')
print("connected")
for i in a:
print(i)
def Armstrongnumber(num):
sum1 = 0
num1 = num
while(num>0):
rem = num%10
num = num//10
if(num1==sum1):
print("Number is Armstrong")
else:
def Palidromenumber(num):
rev = 0
num1 = num
while(num>0):
rem = num%10
num = num//10
if(num1==rev):
print("Number is palidrome")
else:
def Reversenumber(num):
rev = 0
num1 = num
while(num>0):
rem = num%10
106-DHRUVIN GHORI Page 19
Database Handling Using Python
num = num//10
Assignment :- 6
import pandas as pd
df=pd.read_excel('Data.xlsx','Sheet1')
print(df)
mn=df[['Sub1','Sub2','Sub3']].mean(axis=0)
print(mn)
mn=df[['Sub1','Sub2','Sub3']].mean(axis=1)
print(mn)
md=df[['Sub1','Sub2','Sub3']].median(axis=0)
print(md)
md=df[['Sub1','Sub2','Sub3']].median(axis=1)
print(md)
mo=df[['Sub1','Sub2','Sub3']].mode(axis=0)
print(mo)
mo=df[['Sub1','Sub2','Sub3']].mode(axis=1)
print(mo)
sd=df[['Sub1','Sub2','Sub3']].std(axis=0)
print(sd)
sd=df[['Sub1','Sub2','Sub3']].std(axis=1)
print(sd)
vr=df[['Sub1','Sub2','Sub3']].var(axis=0)
print(vr)
vr=df[['Sub1','Sub2','Sub3']].var(axis=1)
print(vr)
r=df.Rno
n=df.Name
s1=df.Sub1
s2=df.Sub2
s3=df.Sub3
##plt.plot(r,s1,label='Sub1',color='b')
##plt.plot(r,s2,label='Sub2',color='g')
##plt.plot(r,s3,label='Sub3',color='r')
##plt.xlabel('Rollno.')
##plt.ylabel('Marks')
##plt.legend()
##plt.show()
##plt.scatter(r,s1,marker='o',c='Red',edgecolor='Black')
106-DHRUVIN GHORI Page 21
Database Handling Using Python
##plt.scatter(r,s2,marker='o',c='Yellow',edgecolor='Black')
##plt.scatter(r,s3,marker='o',c='Blue',edgecolor='Black')
##plt.xlabel('Rollno.')
##plt.ylabel('Marks')
##plt.show()
##plt.hist(s1,edgecolor='Black',bins=r,color='Red')
##plt.hist(s2,edgecolor='Black',bins=r,color='Red')
##plt.hist(s3,edgecolor='Black',bins=r,color='Red')
##plt.xlabel('Rollno.')
##plt.ylabel('Marks')
##plt.show()
plt.bar(r,s1,label='Sub1')
plt.bar(r,s2,label='Sub2')
plt.bar(r,s3,label='Sub3')
plt.xlabel('Rollno.')
plt.ylabel('Marks')
plt.show()