Monday, 30 June 2025

practical 15

 








CREATE TABLE STREAM (


    s_id INT PRIMARY KEY,

    s_name VARCHAR(20)

);


CREATE TABLE STUDENT (

    adno INT PRIMARY KEY,

    fname VARCHAR(20),

    lname VARCHAR(20),

    class VARCHAR(5),

    sec CHAR(1),

    fee INT,

    mobile BIGINT,

    area VARCHAR(30),

    s_id INT,

    FOREIGN KEY (s_id) REFERENCES STREAM(s_id)

);




INSERT INTO STREAM VALUES 
(10, 'PCMC'),
(20, 'PCMB'),
(30, 'PCBH'),
(40, 'HECA'),
(50, 'MECA'),
(60, 'Arts');

INSERT INTO STUDENT VALUES 
(1001, 'Ramesh', 'Babu', 'xii', 'a', 5000, 9988776655, 'Benson Town', 10),
(1002, 'Dipti', 'Rai', 'xii', 'b', 4500, 8877665599, 'Cooke Town', 20),
(1003, 'Rahul', 'Pai', 'xii', 'c', 4000, 7766559988, 'Fraser Town', 30),
(1004, 'Shruthi', 'Saravanan', 'xii', 'c', 4000, 6655998877, 'Benson Town', 30),
(1005, 'George', 'Davis', 'xi', 'a', 4500, 5599887766, 'Cooke town', 10),
(1006, 'Tinu', 'Anna', 'xi', 'b', 4200, 9988776611, 'Benson Town', 20),
(1007, 'Abrar', 'Sultan', 'xi', 'c', 4100, 9988776622, 'Cooke town', 40),
(1008, 'Iffath', 'Khan', 'xii', 'a', 5000, 9988776633, 'Fraser Town', 10),
(1009, 'Hansika', 'Singh', 'xii', 'b', 4500, 9988776644, 'Cleveland Town', 20),
(1010, 'Kailash', 'Dharyanani', 'xii', 'c', 4000, 9988776633, 'Richard Town', 50);


Thursday, 19 June 2025

GRADE 12 : PRACTICE

 Create the below tables : 

DEPARTMENT : 



TEACHER: 


(Include the following constraints : 
> DeptName is PK in Department Table
> No is PK in Teacher table
> Department is a foreign key in TEACHER
> Check that 20<=age<=70
> Sex should take only M and F values) 


SOLUTION 

 CREATE TABLE department (

         DeptName VARCHAR(20) PRIMARY KEY,

         HOD VARCHAR(30),

         RoomNo VARCHAR(10),

         Budget NUMERIC(7)

     );


INSERT INTO department VALUES

         ('Computer', 'Dr. Sen', 'C101', 500000),

         ('History', 'Dr. Malhotra', 'H202', 300000),

         ('Math', 'Dr. Iyer', 'M303', 400000);


CREATE TABLE teacher (

    No INT PRIMARY KEY ,

    Name VARCHAR(20) NOT NULL,

    Age NUMERIC(2) CHECK (Age BETWEEN 20 AND 70),

    Department VARCHAR(20),

    DOJ DATE,

    Salary FLOAT,

    Sex VARCHAR(1) CHECK (Sex IN ('M', 'F')),

    FOREIGN KEY (Department) REFERENCES department(DeptName)

);


INSERT INTO teacher  VALUES

    (111,'Jugal', 34, 'Computer', '1997-01-10', 12000, 'M'),

    (222,'Sharmila', 31, 'History', '1998-03-24', 20000, 'F'),

    (333,'Sandeep', 32, 'Math', '1996-12-12', 30000, 'M'),

    (444,'Sangeeta', 35, 'History', '1999-07-01', 40000, 'F'),

    (555,'Rakesh', 42, 'Math', '1997-09-05', 25000, 'M'),

    (666,'Shyam', 50, NULL, '1998-06-27', 30000, 'M'),

    (777,'Shiv Om', 44, 'Computer', '1997-02-25', 21000, 'M'),

    (888,'Shalaka', 33, 'Math', '1997-07-31', 20000, 'F');


PRACTICE : 

List all teacher names and their departments.

select name,department from teacher;


Display all female teachers.

Select name from teacher where sex='f';


Find teachers who joined before 1998.

Select * from teacher where doj < '1998-01-01';


Show names of teachers with salary above 25,000.

select name,salary from teacher where salary>25000;


List all teachers who do not have a department.

select * from teacher where department is null;


Display the non computer teachers.

select * from teacher where department != 'computer' or department is NULL;


Display the Count of all teachers teachers.

select count(name) as TeacherCount from teacher;


Display the total salary of all teachers.

select sum(Salary) as TotalSalary from teacher;


Display the maximum age among the teachers.

select max(age) as MaxAge from teacher;


Display the earliest DOJ.

select min(doj) as EARLIESTDOJ from teacher;


Display teachers sorted by salary in descending order.

select * from teacher order by salary desc;


List teacher names and their ages who are above 40.

select name,age from teacher where age>40;


Find the average salary of all teachers.

select avg(salary) as AVERAGE_SALARY from teacher ;


Find the average salary of all male teachers.

select avg(salary) from teacher where sex='m';


Find the youngest teacher. 

select * from teacher order by age limit 1;

select * from teacher where age = (select min(age) from teacher);


Find the teacher with the maximum salary.

select * from teacher where salary = (select max(salary) from teacher);


List all departments and their HODs.

 select deptname,hod from department;


Display departments with a budget over ?4,00,000.

select * from department where budget>400000;


Show department names and room numbers.

select deptname,roomno from department


Find the department with the lowest budget.

select deptname,budget from department 

where budget= (select min(budget) from department);


Display the total budget of all departments.

select sum(budget) from department;


List departments in alphabetical order.

select deptname from department order by deptname;


List names that start with s

select name from teacher where name like 's%';


List names that contain and a or an e

select name from teacher where name like '%a%' or '%e%';


List names that contain an a and an e

select name from teacher where name like '%a%' and '%e%';


List names that contain 2 a's

select name from teacher where name like '%a%a%';


List names that contain 'sh' in their name

select name from teacher where name like '%sh%';


List names that have 3rd letter as 'a'

select name from teacher where name like '__a%';


List names that have exactly one letter as 'a'

select name from teacher where name like '%a%' and name not like '%a%a%' ;


List the 8 letter names that end with a.

select name from teacher where name like '_ _ _ _ _ _ _ a';


Display the average salary of teacher : 

select avg(salary) from teacher;


 Display the department name and the corresponding average salaries:

select department,avg(salary) from teacher group by department;


Display the non-null department name and the corresponding average salaries:

select department,avg(salary) 

from teacher 

where department is not null 

group by department;


Display the non-null department name and the corresponding average salaries, in increasing order of their average salaries.

select department,avg(salary) as AvgSal 

from teacher 

where department is not null 

group by department 

order by avgsal;


Display the average salary per sex, in descending order of the average salary

select sex, avg(salary)

from teacher

group by sex

order by avg(salary) desc;



Thursday, 12 June 2025

Revision worksheet

1. This method returns an integer that specifies the current position of the file object.
a) seek() b) load() c) position() d) tell()

2.Answer the below ASSERTION(A) AND REASONING(R) based questions. (5x1=5)
Mark the correct choice as :
(a)Both A and R are true and R is the correct explanation for A.
(b)Both A and R are true and R is not the correct explanation for R.
(c)A is true but R is false.
(d)A is false but R is true.
Assertion (A): For a binary file opened using 'rb' mode, the pickle.dump() displays an error. 
Reason (R): The pickle.dump() method is used to read from a binary file. 

3. Predict the output of :
myfile=open('rhyme.txt','w+')
myfile.write("""Hot cross buns
Hot cross buns
One a penny
Two a penny
Hot cross buns""")
myfile.seek(0)
s=myfile.read(5) ; print(s)
s=myfile.readline(5) ; print(s)
s=myfile.read(5) ; print(s)
s=myfile.readlines() ; print(s)
myfile.close()

4. What will be the output of the following code ?
a) def   fun1( ):
            x=15
            print(x)
   x = 12
   fun1( )
   print(x)
 b)def   test( ):
        print(age)
        age=17
  age = 16
  test( )
  print(age)
c) def grade(a,b,c):
       print(b,a,c)
   a,b,c=9,5,2
   grade(c,a,b)
   print(b,c,a)

5. Which of the following is not a correct statement for binary files?
a. Easy for carrying data into buffer    b. Much faster than other file systems
c. Characters translation is not required d. Every line ends with new line character ‘\n’

6. To read at the most 2 bytes of a line from a file object infile, we use _________
a. infile.read(2) b. infile.readline() c. infile.readline(2) d. infile.readlines(2)

7.Write a program to accept five sports name from the user into a list sname and write them in a file “sports.txt”.Make sure that they’re written on separate lines in the file. Further , display the contents of the file.
8. Write a function that accepts two parameters : a dictionary and a number; and prints only the keys that have values more than the passed number.

9. Rewrite the following code snippet to accept the value of a and to pass it to a function to compute the following. The default value of a should be 25,if no argument has been sent. Also, replace the while loop with a for loop. Predict the output if the value entered by the user is 50.
  a=25
while a < 500:
print a
a = a + 25

10.a)Predict the output for the following python program.  Fill in the blank to ensure that no  new local variable wand is used in the function magic().Predict the new output.
wand=25
def magic(w):
__________________
    wand =w*4
    print("wand : ",wand)
print("wand : ",wand)
magic(wand)
print("wand : ",wand)  

b) What is the output of ?
total=0 
def add(a,b): 
    global total
    print(total)  
    total=a+b 
    print(total)
add(6,6) 
print(total)

11. Read the code given below and answer the questions
f1=open('main.txt','w+') 
f1.write('Bye') 
f1.close() 
If the file contains 'Good ' before execution(cursor on the same line), what will be the content of the file after execution of the code ?

12. Find output generated by the following code:
Str='Computer'
print(Str[-2::-1])
Str=Str[-6:6]*2
print(Str)

13.Answer the following:
i) By default python names the segment with top-level statement as __________________
a) def main() b) main() c) _ _main_ _ d) _main_

ii)Suppose lst is [2,4,6,8,10,12,24], What is  the list after lst.pop(3)?
a)[2,4,6,8] b)[8,10,12,24] c)[2,4,8,10,12,24] d)[2,4,6,10,12,24]

iii)What will happen if you try to open a binary file using a text ?
i) Default value ii) Advance value iii)Garbage value iv)Parameter value

14. What is the output of the following code? 
print("We don\t talk any more")
print(len("We don\t talk any more"))
print("We don\t talk any more"[5:10])
b)


15. Write a function called authenticate(users,loginid,password) which takes following 3 parameters:
> users : a dictionary storing login ids and corresponding password values
> loginid : a string for a login name and 
> password : a string for a password

The function should do the following: 
i) if the user exists and the password is correct for the given loginid, it should print ‘Access granted
ii) if the user exists and the password is incorrect for the given loginid , it should print ‘Incorrect password’.
iii)If the user does not exist for the given loginid , it should print ‘wrong credentials’

16. Write a program to read following details (as long as the user wants) of sports’ performance (sport, competitions, prizes-won) of your school and store into a csv file delimited with tab character, where :
sport => sports name the school participated in
competitions => no. Of interschool competitions participated in
prizes-won=> no. Of competitions won

17. Write a program that copies a  text file ‘source.txt’ onto ‘target.txt’ barring the lines starting with an ‘@’ sign.

18. A binary file "Student.dat" has the structure :
{'stud_id':int, 'stud_name': string,  'percentage': float}
Write a menu driven program using functions for the following:
a> Accept the details of a student.
b> Display the details of all students and the average percentage in tabular format
c> Display the name and marks of students scoring 75% and above

Wednesday, 4 June 2025

GRADE 12 PRACTICAL 13

'''A stack named StuStack contains records of students.Each record is represented as a list,eg [101, 'Ravi', 87.5]

Write the following user-defined functions :

(i) pushStu(StuStack,StuList):

this takes StuStack and a list of students as arguments, and pushes the list into the stack.

(ii) popStu(StuStack):

This function pops the topmost student record from the stack and returns it. If empty, display the message

"Stack Underflow" and return None.

(iii) display(StuStack):

This function should display the student by popping it one by one. Also display the record with the highest marks. If the stack is empty, display "No student records".

'''


StuStack=[]


def menu():

    while True:

        ch=int(input("""MENU :

1.PUSH

2.POP

3.DISPLAY

4.EXIT"""))

        if ch==1:

            StuList=[]

            n=int(input("Enter no of records you want to add : "))

            for i in range(n):

                stuid=int(input('Enter student id : '))

                name=input("Enter name : ")

                marks=float(input('Enter marks : '))

                l=[stuid,name,marks]

                StuList.append(l)

            pushStu(StuStack,StuList)

        elif ch==2:

            rec=popStu(StuStack)

            if rec:

                print(rec)

            else:

                print('Empty')

        elif ch==3:

            display(StuStack)

        else:

            break


def pushStu(StuStack,StuList):

    for i in StuList:

        StuStack.append(i)


def popStu(StuStack):

    if len(StuStack)>0:

        return StuStack.pop()

    else:

        print('STACK UNDERFLOW')

        return None

    

def display(StuStack):

    maxx=0

    if len(StuStack)==0:

        print('Empty Stack')

        return

    while StuStack:

        rec = StuStack.pop()

        print(rec)

        if rec[2]>maxx:

            maxx=rec[2]

            Student=rec

        

    print('Student with maximum marks : ',Student)


menu()

    

    



Tuesday, 3 June 2025

GRADE 12 - PRACTICAL 12

'''GRADE 12: PRACTICAL 12

Create a list Library to implement a STACK data structure

in Python. The Library stack can hold a maximum of 5 records,

each representing a book. Each book is represented as :

{"BookID": <int>, "Title": <str>,

"Author": <str>,"Year": <str>}

Write a menu-driven program to perform :

a) push- Add a new book record to the stack.

b) pop- Delete the topmost book record from the stack.

c) display - To display all book records

d) peek- To view the topmost record of the stack

Handle Underflow and Overflow conditions for the stack .

'''

Library=[]

limit=5

def menu():

    while True:

        ch=int(input("""MENU:

1.PUSH

2.POP

3.DISPLAY

4.PEEK

5.EXIT

"""))

        if ch==1:

            bid=int(input("Enter book id "))

            title=input("enter title")

            auth=input("Enter author")

            year=input("Enter year")

            book={"BookID":bid, "Title":title,

"Author":auth,"Year":year}

            push(Library,book)

            

        elif ch==2:

            book=pop(Library)

            if book:

                print('Popped record : ',book)

                        

        elif ch==3:

            display(Library)


        elif ch==4:

            book=peek(Library)

            if book:

                print('Peekeed record : ',book)

            

        else:

            break


  

def push(Library,book):

    if len(Library)<limit:

        Library.append(book)

        print('Pushed record : ',book)

    else:

        print('OVERFLOW')



def pop(Library):

    if len(Library)>0:

        return Library.pop()

    else:

        print('UNDERFLOW')

        return None


def display(Library):

    print('Contents of Library')

    if len(Library)>0:

        for i in Library[::-1]:

            print(i)

    else:

        print('EMPTY')


def peek(Library):

    if Library:

        return Library[-1]

    else:

        print('UNDERFLOW')

        return None



menu()