PR Copy 2024 - 25
PR Copy 2024 - 25
ct=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
print('''Select the number system:
2 for binary
8 for octal
16 for hexadecimal \n''')
d=int(input("Enter your choice :"))
x=int(input("Enter a number in Decimal ="))
y=''
while x>0:
r=x%d
y=ct[r]+y
x=x//d
if d==2:
print("Binary value=",y)
elif d==8:
print("Octal value= ",y)
else:
print("Hexadecimal value=",y)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#P2 : Write a program to input the value of x and n and print the sum of the
following series
# x - x²/2! + x³/3! - x⁴/4! - ...... xⁿ/n!
----------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-------------------------------------
poem.txt file
RAIN
Rain, rain, go away
Come again another day
Daddy wants to play
Rain, rain go away
Rain, rain, go away
------------------------------------------
# P4 Read a text file line by line and display each word seperatly by a # sign
f=open("poem.txt",'r')
s=f.readlines()
for line in s:
words=line.split()
for word in words:
print(word+"#",end='')
print("")
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# P6 : remove all the lines containg 'a' and write in a new file.
f=open('poem1.txt','r')
n=open('new_poem1.txt','w')
fn=open('temp_poem1.txt','w')
print("These lines are in the file: ",f.name,'\n')
print(f.read())
print('-'*50,'\n')
f.seek(0)
print("These Lines containg 'a' were Writen To the New file :",n.name,'\n')
s=f.readline()
while 1:
for x in s:
if x=='a':
print(s,end='')
n.write(s)
break
else:
fn.write(s)
s=f.readline()
if not s:
break
print('\n',"-"*50,'\n')
f.close()
fn.close()
f=open('poem1.txt','w')
fn=open('temp_poem1.txt','r')
tem=fn.read()
f.write(tem)
print("These lines not containg 'a' were remain in The Old file: ",f.name)
print(tem)
n.close()
fn.close()
f.close()
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
theory '''CSV (Comma Separated Values) is a simple file format
used to store tabular data, such as a spreadsheet or database.
A CSV file stores tabular data (numbers and text)
in plain text. Each line of the file is a data record.
Each record consists of one or more fields,
separated by commas. The use of the comma as a field
separator is the source of the name for this file format.
-----------------------------------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
---------------------------------------------------------
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
def entering():
f = open('p11db.csv' , 'a' , newline = '')
wr=csv.writer(f)
a='y'
while a=='y' or a=='Y':
print("\t Enter a New Record : ")
i=int(input("Enter a New ID No: "))
n=input("Enter the User Name : ")
p=input("Enter the new password : ")
r=[i,n,p]
wr.writerow(r)
print('any key to exit...')
a=input("press 'y' to continue... ")
f.close()
def readdata():
f = open('p11db.csv' , 'r')
rd=csv.reader(f)
print("\n \t DATA in the CSV File : ")
for i in rd :
print(i)
f.close()
def searchID():
s = input('Enter a rollno to search: ')
f = open('p11db.csv','r')
rd=csv.reader(f)
print("\n \t The CSV File Search : ")
for i in rd :
if i[0]==s:
print(i)
break
else:
print('ID not Found..')
f.close()
while True:
print('\t','-*'*18)
print(' Enter 1 For Create New ID and Password.')
print(' Enter 2 For Read All ID Details.')
print(' Enter 3 For SEARCH password of a ID.')
print(' Enter 4 For EXIT Program..')
choice = int(input('\t Enter you choice: '))
if choice == 1:
entering()
elif choice == 2:
readdata()
elif choice == 3:
searchID()
elif choice == 4:
print('Thanks .. ! ')
break
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
P14: Implement a stack
print("PROGRAM TO IMPLEMENT A STACK ..... ")
def push(li):
print(' New Employee details : ')
eid=int(input("Eid number: "))
ename=input("Employee name : ")
new=[eid,ename]
li.append(new)
def popele(li):
if(li==[]):
print("Stack is Empty...")
else:
print("Deleted Employee is:",li.pop())
def display(li):
print(li)
stack=[]
while True:
print("\n1 FOR PUSH : ")
print("2 FOR POP : ")
print("3 FOR DISPLAY : ")
print("4 FOR EXIT : ")
choice=int(input("Enter your choice:"))
if(choice==1):
push(stack)
elif(choice==2):
popele(stack)
elif(choice==3):
display(stack)
elif(choice==4):
break
else:
print("Wrong input or invalid... ")
input("for continue press enter : ")
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
print("P-15 PROGRAM TO IMPLEMENT A QUEUE of student roll, name & age ")
def enqueue(qq):
print(' New Student details : ')
a=int(input("roll number: "))
b=input("Student name : ")
c=int(input("Age of student : "))
s=[a,b,c]
qq.append(s)
def dequeue(qq):
if(qq==[]):
print("Queue is Empty...")
else:
print("Deleted element is:",q[0])
qq.pop(0)
def display(qq):
print(qq)
q=[]
while True:
print("\n 1 FOR PUSH : ")
print("2 FOR POP : ")
print("3 FOR DISPLAY : ")
print("4 FOR EXIT : ")
choice=int(input("Enter your choice:"))
if(choice==1):
enqueue(q)
elif(choice==2):
dequeue(q)
elif(choice==3):
display(q)
elif(choice==4):
break
else:
print("Wrong input or invalid... ")
input("for continue press enter : ")
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# P17 Create and List the Databaes.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="davalkusa")
print("Name of the DB object variable : ")
print(mydb)
mycursor=mydb.cursor()
mycursor.execute('create database ex1xii')
mycursor.execute('show databases')
print('MySQL Database List: ')
i=1
for x in mycursor:
print(i,' - ',x)
i+=1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
================================================================
================================================================
+------+---------------+-----------+------+------------+------+---------+--------+
| eno | ename | job | mgr | doj | sal | comm | deptno |
+------+---------------+-----------+------+------------+------+---------+--------+
| 7369 | Sunita Sharma | clerk | 7902 | 1980-01-17 | 2800 | NULL | 20 |
| 7499 | Ashok Singhal | salesman | 7698 | 1981-02-20 | 3600 | 300.00 | 30 |
| 7521 | Rohit Rana | salesman | 7698 | 1981-02-22 | 5250 | 500.00 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 4975 | NULL | 20 |
| 7654 | Martin S. | Salesman | 7698 | 1981-09-28 | 6250 | 1400.00 | 30 |
| 7698 | Binod Goel | Manager | 7839 | 1981-05-01 | 5850 | NULL | 30 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 2450 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1987-04-19 | 5000 | NULL | 20 |
| 7839 | Kavita Sharma | President | NULL | 1981-11-17 | 5000 | NULL | 10 |
| 7844 | Tushar Tiwari | Salesman | 7698 | 1981-09-08 | 4500 | NULL | 30 |
| 7876 | Anand Rathi | Clerk | 7788 | 1987-05-23 | 6100 | NULL | 20 |
| 7900 | Jaydeep Rana | Clerk | 7698 | 1981-12-03 | 4950 | NULL | 30 |
| 7902 | Sumit Vats | Analyst | 7566 | 1981-12-03 | 3500 | 3600.00 | 20 |
| 7934 | Manoj Kaushik | Clerk | 7782 | 1982-01-23 | 5300 | NULL | 10 |
+------+---------------+-----------+------+------------+------+---------+--------+
14 rows in set (0.00 sec)
Q4.To list all name and employee number from the table.
mysql> select ename,eno from emp;
Q5. To list all name , date of joining and salary of the all employees.
mysql> select doj,sal from emp;
Q6. To display the employee name and the incremented value of salary as salary +
300.
mysql> select ename,sal+300 as 'sal+300' from emp;
Q7.To list the employee name and its annual salary =12 x salary + 100 .
mysql> select ename,sal*12+100 as 'annual salary' from emp;
Q13. To list the ename where manager code are in 7902,7566 and 7788.
mysql> select ename from emp where mgr in(7902,7566,7788);
Q15. To display all the columns in the ascending order of date of joining.
mysql> select * from emp order by doj;
Q16. To list all the columns in the ascending order of department number and
decreasing order of salary
mysql> select * from emp order by deptno , sal desc;
Q17. Display the employee name and job of employees date of joining between 20
February 1981 and 1st Nov 1981.
mysql> select ename ,job from emp where doj between '1981-02-20' and '1981-11-01';
Q18. Display the name and department number of all employees in department 20 and
30 alphabetically order by in name.
mysql> select ename , deptno from emp where deptno in(20,30) order by ename;
Q20. To list name and salary of all employees who earn commissionmysql> select
ename ,sal from emp where comm is not null;
Q21. To list the name of all employee where second letter of their name is a.
mysql> select ename from emp where ename like '_a%';
Q22. To list the name and job of all the employees who work in department 20 and
their manager is 7788
mysql> select ename ,job from emp where deptno =20 and mgr=7788;
Q24. To list the department number job and sum of the salaries group by department
number and job.
mysql> select deptno,job,sum(sal) from emp group by deptno,job;
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
MYSQL Practicals2:
Write the output of the following commands.
mysql> select ename, 12*sal + comm from emp where ename='ashok singhal';
+---------------+---------------+
| ename | 12*sal + comm |
+---------------+---------------+
| Ashok Singhal | 43500 |
+---------------+---------------+
================================================================
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
P19 and P20: Database handling to create database and table , insert, display,
delete,records
import mysql.connector
import os
mydb=mysql.connector.connect(host="localhost",user="root",passwd="kvuc")
def display_msg():
mycursor=mydb.cursor()
mycursor.execute("select * from stu")
row=mycursor.fetchone()
while row is not None:
print("--->",row)
row=mycursor.fetchone()
def create_table():
try:
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE school")
mycursor.execute("use school")
mycursor.execute("CREATE TABLE stu (ROLL INT(2),SNAME VARCHAR(30),DOB
DATE,ADDRES VARCHAR(50))")
print("Table Created")
except:
mycursor = mydb.cursor()
mycursor.execute("use school")
print("Databse or Table Already Created")
def add_msg():
mycursor=mydb.cursor()
ro=input("Enter Stu Roll: ")
sn=input("Enter the Name of Student: ")
dob=input("Enter dob yyyy-mm-dd : ")
ad=input("Enter the Address : ")
def delete_msg():
mycursor=mydb.cursor()
ro=input('Enter roll for Delete the Record')
sql="DELETE FROM stu WHERE ROLL='%s'"%ro
mycursor.execute(sql)
print('ROLL No : ',ro," record is deleted ")
mydb.commit()
def Main_Menu():
print("Enter 1 : TO ADD NEW Student")
print("Enter 2 : TO DISPLAY STUDENT LIST")
print("Enter 3 : TO DELETE Student from the list")
print("Enter others to Exit")
try:
userInput = input("Please Select An Above Option: ")
if (userInput=='1'):
print("\n")
add_msg()
elif(userInput=='2'):
display_msg()
elif (userInput=='3'):
delete_msg()
else:
print("Enter correct choice. . . ")
ch = input("\nwant to continue Y/N: ")
if(ch == 'Y' or ch=='y'):
Main_Menu()
else:
print("Program going to Exit")
exit("\n! Thanks")
except():
print("Something Wrong in code")
create_table()
Main_Menu()
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# P21 To view databases & its tables in The MySQL.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="kvuc")
mycursor=mydb.cursor()
mycursor.execute('show databases')
print('MySQL Database List: ')
for x in mycursor:
print(x)
print(' \n Enter the Database to view its tables. ')
dbn=input('DataBase Name : ')
sql="use %s"%dbn
mycursor.execute(sql)
mycursor.execute('show tables')
for x in mycursor:
print(x)
t=input('Enter Table : ')
print(" The Content of Table : ",t)
mycursor.execute("desc %s"%t)
for x in mycursor:
print(x[0],end='\t \t')
print('\n','- * - '*20)
mycursor.execute("select * from %s"%t)
for x in mycursor:
print(x)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx
MYSQL Practicals3:
TABLE : doctor
+------+----------------+------------+--------+------------+
| ID | DNAME | DEPT | GENDER | EXPERIENCE |
+------+----------------+------------+--------+------------+
| 101 | JOHN | ENT | M | 12 |
| 104 | SEJAL | ORTHOPEDIC | F | 5 |
| 107 | DOLLY MISHRA | CARDIOLOGY | F | 10 |
| 104 | DEEPACK VISHWA | SKIN | M | 3 |
| 105 | JOHNSAN | MEDICINE | M | 4 |
| 114 | LARA | SKIN | F | 3 |
| 109 | K GEORGE | MEDICINE | F | 9 |
| 117 | LUCKY | ENT | F | 3 |
| 111 | BILL | MEDICINE | F | 12 |
| 130 | MORPHY | ORTHOPEDIC | M | 15 |
+------+----------------+------------+--------+------------+
TABLE : SALARY
+------+-------+-----------+-------------+
| ID | BASIC | ALLOWANCE | CONSULATION |
+------+-------+-----------+-------------+
| 101 | 12000 | 1000 | 300 |
| 104 | 23000 | 2300 | 500 |
| 107 | 32000 | 4000 | 500 |
| 114 | 12000 | 5200 | 100 |
| 109 | 42000 | 1700 | 200 |
| 105 | 18900 | 1690 | 300 |
| 130 | 21700 | 2600 | 300 |
| 130 | 40000 | 3200 | 600 |
+------+-------+-----------+-------------+
Q1. Display NAME OF ALL DOCTORS WHO ARE IN MEDICINE DEPT HAVING MORE THAN 10years
EXPERIENCE.
mysql> select dname from doctor where dept='medicine' and experience>10;
+-------+
| dname |
+-------+
| BILL |
+-------+
Q2. DISPLAY DOCTOR NAME , DEPT AND NET_SALARY = BASIC + ALLOWANCE OF ALL FEMALE
DOCTOR.
mysql> select d.dname, d.dept, s.basic+s.allowance as Net_salary from doctor
d,salary s where d.id=s.id and gender='f';
+--------------+------------+------------+
| dname | dept | Net_salary |
+--------------+------------+------------+
| SEJAL | ORTHOPEDIC | 25300 |
| DOLLY MISHRA | CARDIOLOGY | 36000 |
| LARA | SKIN | 17200 |
| K GEORGE | MEDICINE | 43700 |
+--------------+------------+------------+
Q5. Display all information in natural join of more than 4 year experience doctor
in each department.
mysql> select * from doctor d natural join salary s group by dept having
experience>4;
+------+--------------+------------+--------+------------+-------+-----------
+-------------+
| ID | DNAME | DEPT | GENDER | EXPERIENCE | BASIC | ALLOWANCE |
CONSULATION |
+------+--------------+------------+--------+------------+-------+-----------
+-------------+
| 107 | DOLLY MISHRA | CARDIOLOGY | F | 10 | 32000 | 4000 |
500 |
| 101 | JOHN | ENT | M | 12 | 12000 | 1000 |
300 |
| 104 | SEJAL | ORTHOPEDIC | F | 5 | 23000 | 2300 |
500 |
+------+--------------+------------+--------+------------+-------+-----------
+-------------+