0% found this document useful (0 votes)
63 views51 pages

Aaryan Cs Practical Record

Uploaded by

projectgowri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views51 pages

Aaryan Cs Practical Record

Uploaded by

projectgowri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

SARASWATHY VIDYANIKETHAN

CENTRAL SCHOOL, KODAKARA

CLASS XII
COMPUTER SCIENCE
PRACTICAL RECORD 2023-24
Certified that this the Bonafede record of
Practical Work done by ……………………..

With register No:…………………….. year ……………………

Submitted for the practical exam held on ………………………

Principal Teacher in charge Examiner


NO Python Programs Date Teacher's
Signature
1 Fibonacci series using function.

2 String is a palindrome or not using function.


3 Random number generator.
4 Occurrence of any word in a string using function.

5 Double the odd values and half the even values of a list
using function.
6 Sum of all elements of a list using function.

7 Search an element in a list and display the frequency of


element.
8 Input N numbers in tuple and pass it to function to count
how many even and odd numbers are entered.
9 Dictionary menu driven program to add, remove and
display the details of book.
10 To read a text file line by line and display each word
separated by a#.
11 To read a text file line by line and display the number of
vowels/consonants/uppercase/lowercase characters in
the file.
12 Create a binary file with name and roll number. Search
for a given roll number and update the marks.
13 To create a binary file with roll number, name and marks.
Input a roll number and update the marks.
14 To remove all the lines that contains the character 'a' in a
file and write it to another file.
15 CSV file search.

16 Stack operations.

17 String reversal.

18 Employee using stack.


NO Programs Date Teacher’s
Signature

1 SQL Set 1

2 SQL Set 2

3 SQL Set 3

4 SQL Set 4

5 SQL Set 5

6 Python SQL connectivity 1

7 Python SQL connectivity 2

8 Python SQL connectivity 3

9 Python SQL connectivity 4


PYTHON
PROGRAMS
Program No:1

Aim:
To print the Fibonacci series using function
Program
def fibonacci(n):
a=0
b=1
count=0
print(a,b,sep='\n')
while count<n-2:
count=count+1
c=a+b
print(c)
a,b=b,c
num=int(input("How many term you want to display:"))
print("Fibonacci sequence:")
fibonacci(num)

OUTPUT
How many term you want to display? 5
Fibonacci sequence:
0
1
1
2
3
Program No: 2

AIM:
To check whether the string is palindrome or not using function.

PROGRAM

def pal(s):
s = s.lower() # Convert to lowercase to make it case-insensitive
s = s.replace(" ", "") # Remove spaces
return s == s[::-1]
string = input("Enter a string: ")
if pal(string):
print("String is a palindrome")
else:
print("String is not a palindrome")

OUTPUT
Enter a string: racecar
String is a palindrome
Program No: 3
Aim:
Write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).
Program
import random
def roll():
s = random.randint(1,6)
return s
c = 'y'
while c == 'y':
cont = input("Press 'q' to quit or press any key to continue: ")
if cont.lower() == 'q':
print('Thanks for playing!')
break
else:
c = input("You rolled a " + str(roll()) + ". Do you want to roll again?
(y/n): ")

OUTPUT
Press 'q' to quit or press any key to continue: y
You rolled a 3. Do you want to roll again? (y/n): y
Press 'q' to quit or press any key to continue: y
You rolled a 5. Do you want to roll again? (y/n): y
You rolled a 1. Do you want to roll again? (y/n): y
Press 'q' to quit or press any key to continue: q
Thanks for playing !
Program No: 4

Aim:
To find the occurrance of a word in string

Program
def countocc(sentence,word):
lis=sen.split()
return lis.count(wor)

sen=input("Enter a sentence:")
wor=input('Enter the word to be searched:')
countocc=countocc(sen,wor)

if countocc==0:
print("Your choice doesn't contain in the sentence")
else:
print(f"Your choice occurs '{countocc}' times")

OUTPUT

Enter a sentence: This is a simple example sentence. It is


a simple example.
Enter the word to be searched: simple
Your choice occurs '2' times
Program No: 5

Aim:
Write a program to pass list to a function and double the odd
values and half even values of a list and display the list elements after
changing.

Program
def display(list1):
for i in range(len(list1)):
if list1[i]%2==0:
list1[i]=list1[i]//2
else:
list1[i]=list1[i]*2
print(list1)
List1=eval(input("Enter a list:"))
display(List1)

OUTPUT
Enter a list:[1,2,3,4,5]
[2, 1, 6, 2, 10]
Program No: 6

Aim:
To find the sum of all elements of a list using function.

Program

def lisum(lst):
return sum(lst)

n = int(input("Enter the limit:"))


lst = []

for i in range(n):
item = int(input("Enter the element:"))
lst.append(item)
print("The list is:", lst)
print("Sum of all elements:", lisum(lst))

OUTPUT
Enter the limit:5
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:4
Enter the element:5
The list is [1, 2, 3, 4, 5]
Sum of all element: 15
Program No: 7
Aim:
Write a program to search an element in a list and display the
frequency of an element present in the list and their location using
linear search.

Program
def fun(list,x):
count=0
for i in range(len(list)):
if list[i]==x:
print("Found location",i)
count+=1
print("Frequency of element",count)
a=eval(input("Enter a list:"))
b=eval(input("Element to be searched:"))
fun(a,b)

OUTPUT
Enter a list: [1,2,3,4,5,1,2]
Element to be searched:1
Found at index: 0
Found at index: 5
Frequency of element: 2
>>>
Program No: 8
Aim:
Write a program to input n numbers in a tuple and pass it to
function to count the occurrence of even and odd numbers in the tuple.

Program
def fun(tup):
even=0
odd=0
for i in range(len(tup)):
if tup[i]%2==0:
even+=1
else:
odd=odd+1
print("number of even number:",even)
print("number of odd number:",odd)
x=eval(input("enter a tuple"))
fun(x)

OUTPUT
Enter a tuple: (1,2,3,4,5,6,7)
Number of even number: 3
Number of odd number: 4
>>>
Program No: 9
Aim:
Write a menu driven program having menu options:
⮚ Add()-to add book code, name and price of books to a
dictionary.
⮚ Dispall()-to display details of all books.
⮚ Remv()-to remove details of a book for a given book code.
Program
def options():
print('''1.Enter book details\n2.Display book details\n3.Remove
book\n4.Exit''')

det={}
def add():
while True:
code=input("Enter the book code:")
name=input("Enter the book name:")
price=input("Enter the price:")
inf=name+":Rs"+price
det[code]=inf
ch=input("Want to add more:")
if ch in 'noNO':
break

def display():
if det=={}:
print("empty list")
else:
for i in det.items():
print(i)
def remove():
rem=input("Enter the bookcode whose details to be deleted:")
del det[rem]

while True:
options()
ch=int(input("Enter the option:"))
if ch==1:
add()
elif ch==2:
display()
elif ch==3:
remove()
elif ch==4:
break
else:
print("Not included thank you")
OUTPUT
1.Enter book details
2.Display book details
3.Remove book
4.Exit
Enter the option: 1
Enter the book code: 1
Enter the book name: java
Enter the price: 100
Want to add more: y
Enter the book code: 2
Enter the book name: c++
Enter the price: 200
Want to add more: n
1.Enter book details
2.Display book details
3.Remove book
4.Exit
Enter the option:2
('1', 'java:rs100')
('2', 'c++:rs200')
1.Enter book details
2.Display book details
3.Remove book
4.Exit
Enter the option: 3
Enter the bookcode whose details to be deleted: 2
1.Enter book details
2.Display book details
3.Remove book
4.Exit
Enter the option: 4
>>>
Program No: 10

Aim:
To read a text file line by line and display each word separated by
a#.

Program

f=open("D:/DATA/record.txt","r")
for line in f:
words=line.split()
for w in words:
print(w+"#",end=' ')
f.close()

OUTPUT
python# program# data# file#
Program No: 11
Aim:
To read a text and display the number of vowels/ consonants/
uppercase/ lowercase characters in the file.

Program
f=open("D:/DATA/file.txt","r")
v=c=u=l=o=0
data=f.read()
vowels=['a','e','i','o','u']
print(type(data))
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' 'and ch!=' \n':
o+=1
print("Total vowels in file:",v)
print("Total consonants in file:",c)
print("Total capital letters in file:",u)
print("Total small letters in file:",l)
print("Total other than letters:",o)
f.close()
INPUT

OUTPUT
Total vowels in file: 6
Total consonants in file: 4
Total capital letters in file: 3
Total small letters in file: 7
Total other than letters: 2
>>>
Program No: 12

Aim:
Program to create a binary file with name and roll number. Search
for a given roll number and display the name, if not found display
appropriate message

Program
import pickle
student=[]
total=int(input("Enter no of students:"))
for i in range(total):
roll=int(input("Enter rollno:"))
name=input("Enter name:")
student.append([roll,name])
f=open("D:/DATA/file 2.dat","wb")
pickle.dump(student,f)
print("Record Added")
f.close()
f=open("D:/DATA/file 2.dat","rb")
student=[ ]
student=pickle.load(f)
ans="y"
while ans=="y":
rno=int(input("Enter the rollno to search:"))
y=False
for R in student:
if R[0]==rno:
y=True
print("Successful search",R[1],"found")
if y==False:
print("Sorry not found")
ans=input("Wish to search more records(y/n)?")
f.close()

OUTPUT
Enter no of students:2
Enter rollno:1
Enter name:deepa
Enter rollno:2
Enter name:alan
Record added
Enter the rollno to search:2
Successful search alan found
Wish to search more records(y/n)?y
Enter the rollno to search:3
Sorry not found
Wish to search more records(y/n)?n
>>>
Program No: 13

Aim:
To create a binary file with roll number, name and marks. Input a
roll number and update the marks

Program
import pickle
student=[]
f=open("D:/DATA/file 3.dat","wb")
choice="y"
while choice=="y":
roll=int(input("Enter rollno:"))
name=input("Enter name:")
marks=int(input("Enter marks:"))
student.append([roll,name,marks])
choice=input("Wish to enter more records(y/n)?:")
pickle.dump(student,f)
print("Record added")
f.close()
f=open("D:/DATA/file 3.dat","rb")
student=[ ]
student=pickle.load(f)
ans='y'
while ans=='y':
found=0
rno=int(input("Enter the rollno to update:"))
for r in student:
if r[0]==rno:
print("Name is:",r[1]," ")
print("Current marks is :",r[2]," ")
m=int(input("Enter new marks"))
r[2]=m
print("Record updated")
print("Name is:",r[1]," ")
print("Updated marks is:",r[2]," ")
found=1
break
if not found:
print("Sorry Roll Number not found")
ans=input("Update more(y/n?:")
f.close()

OUTPUT
Enter rollno:1
Enter name:alan
Enter marks:100
Wish to enter more records(y/n)?:y
Enter rollno:2
Enter name:arjun
Enter marks:150
Wish to enter more records(y/n)?:n
Record added
Enter the rollno to update:2
Name is: arjun
Current marks is : 150
Enter new marks: 200
Record updated
Name is: arjun
Updated marks is: 200
Update more(y/n?:n
>>>
Program No: 14
Aim:
To remove all lines contain the character ‘a’ in the file and write it
to another file.

Program
f1=open("D:/file1.txt")
f2=open("D:/f2.txt","w+")
for line in f1:
if 'a' not in line:
f2.write(line)
print("File copied successfully")
f1.close()
f2=open("D:/DATA/file 5.txt")
lines=f2.readlines()
print("After copy the content to f2.txt is")
for line in lines:
print(line,end=' ')
f1.close()
f2.close()

OUTPUT
File copied successfully
After copy the content to f2.txt is
Program No: 15

Aim:
To create CSV file and store empno, name, salary and search any
empno and display name, salary and if not found appropriate message

Program
import csv
with open("D:\\Employee.csv","w")as csvfile:
mywriter=csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
empno=int(input("Enter employee number:"))
name=input("Enter employee name:")
salary=int(input("Enter employee salary:"))
mywriter.writerow([empno,name,salary])
print("Data saved....")
ans=input("Add more")
ans='y'
with open("D:\\Employee.csv",'r')as csvfile:
myreader=csv.reader(csvfile,delimiter=',')
ans='y'
while ans=='y':
found=False
csvfile.seek(0,0)
e=int(input("Enter employee number to search:"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("Name:",row[1])
print("Salary:",row[2])
found=True
break
if not found:
print("Empno not found")
ans=input("Search more?(y)")

OUTPUT
Enter employee number:1
Enter employee name:alan
Enter employee salary:20000
Data saved....
Add more: y
Enter employee number:2
Enter employee name:arjun
Enter employee salary:30000
Data saved....
Add more: n
Enter employee number to search:1
Name: alan
Salary: 20000
Search more?(y)y
Enter employee number to search:2
Name: arjun
Salary: 30000
Search more?(y)y
Enter employee number to search:3
Empno not found
Search more?(y)n
>>>
Program No: 16

Aim:
Program to implement all basic operations of stack, such as
adding element(PUSH), removing the elements(TRAVERSAL) using list.

Program
def push(stack,item):
stack.append(item)
def pop(stack):
if (stack==[ ]):
print("Stack is empty")
else:
print("Deleted element is :",stack.pop())
def display(stack):
length=len(stack)
if length==0:
print("stack is empty")
else:
print("Stack contents (from top to bottom)\n------")
for i in range(length-1,-1,-1):
print(stack[i])
stack=[]
c='y'
while c=='y':
print("Stack Operations\n*************************")
print("1.PUSH")
print("2.POP")
print("3.DISPLAY")
choice=int(input("Enter your choice:"))
if choice==1:
item=input("Enter any number:")
push(stack,item)
elif choice==2:
pop(stack)
elif choice==3:
display(stack)
else:
print("Wrong choice")
c=input("Do you want to continue?(y/n)")
OUTPUT
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:1
Enter any number:10
Do you want to continue?(y/n)y
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:2
Deleted element is : 10
Do you want to continue?(y/n)y
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:3
stack is empty
Do you want to continue?(y/n)n
>>>
Program No: 17
Aim:
Program to input a string to a stack and output the string in
reverse order

Program
def push(stack,item):
stack.append(item)
def pop(stack):
if stack==[ ]:
print("Stack is empty")
else:
return stack.pop()
def reverse(str):
n=len(str)
stack=[ ]
for i in range(0,n):
push(stack,str[i])
str=' '
for i in range(0,n):
str+=pop(stack)
return str
str=input("Enter a string:")
str=reverse(str)
print("Reversed string is"+str)

OUTPUT
Enter a string:COMPUTER
Reversed string is RETUPMOC
>>>
Program No:18

Aim:
Program to create a stack called Employee and perform addition,
deletion and display of employee details such as employee number and
employee name.
Program
def push(employee,record):
employee.append(erecord)
def pop(employee):
if(employee==[ ]):
print("Stack is empty")
else:
print("Deleted element is:",employee.pop())
def display(employee):
length=len(employee)
if length==0:
print("Stack is empty")
else:
print("Stack contents(from top to bottom)\n------------------------------
-------")
for i in range(length-1,-1,-1):
print(employee[i])
employee=[]
c='y'
while c=='y':
print("Stack operations\n*************************")
print("1.PUSH")
print("2.POP")
print("3.DISPLAY")
choice=int(input("Enter your choice:"))
if choice==1:
empid=input("Enter employee no:")
empname=input("Enter employee name:")
erecord=(empid,empname)
push(employee,erecord)
elif choice==2:
pop(employee)
elif choice==3:
display(employee)
else:
print("Wrong choice")
c=input("Do you want to continue?(y,n)")

OUTPUT
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:1
Enter employee no:1
Enter employee name:Arjun
Do you want to continue?(y,n)y
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:1
Enter employee no:2
Enter employee name:Alan
Do you want to continue?(y,n)y
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:2
Deleted element is: ('2', 'Alan')
Do you want to continue?(y,n)y
Stack operations
*************************
1.PUSH
2.POP
3.DISPLAY
Enter your choice:3
Stack contents(from top to bottom)
-------------------------------------
('1', 'Arjun')
Do you want to continue?(y,n)n
>>>
SQL
PROGRAMS
1) Write SQL Commands for questions 1 to 3 on the basis of table
Student and give the output for queries 4 and 5.

TABLE:STUDENT
Rollno Name Gender Marks DOB Stream
1 Manu M 98 01/10/97 Commerce
2 Sonia F 80 24/03/98 Science
3 Pranav M 60 12/12/98 Commerce
4 Mythra F 95 07/01/98 Science
5 Michal M 85 08/05/97 Science

QUERIES

CREATE TABLE Student(Rollno int primary key Not Null, Name varchar (20),
Gender char(1), Marks int, DOB date, Stream varchar(15));

Insert into student values(1,”Manu”,’M’,98,”1997-10-01”,”Commerce”);

Insert into student values(2,”Sonia”,’F’,80,”1996-03-24”,”Science”);

Insert into student values(3,”Pranav”,’M’,60,”1998-12-12”,”Commerce”);

Insert into student values(4,”Mythra”,’F’,95,”1999-01-07”,”Science”);

Insert into student values(5,”Michal”,’M’,85,”1997-05-08”,”Science”);

1.To show all information about the students whose mark is greater than 80.

SELECT*FROM Student WHERE MARKS>80;

Output:
2.To list all Male Students who are from Science stream

SELECT*FROM STUDENT WHERE GENDER=”M” AND MARKS>95;

Output:

3. To list all names of Students beginning with ‘M’ sorted by Name in descending
order.

SELECT NAME FROM STUDENT WHERE NAME LIKE ‘M%’ ORDER BY NAME DESC;

Output:

4. SELECT COUNT(*) FROM STUDENT WHERE MARKS<70:

Output:

5. SELECT max (Marks) FROM Student;

Output:
2.Write SQL Commands for questions 1 to 3 on the basis of table
Student and give the output for queries 4 and 5.

TABLE:STUDENT
Rollno Name Gender Marks DOB Stream
1 Manu M 98 01/10/97 Commerce
2 Sonia F 80 24/03/98 Science
3 Pranav M 60 12/12/98 Commerce
4 Mythra F 95 07/01/98 Science
5 Michal M 85 08/05/97 Science

QUERIES

CREATE TABLE Student(Rollno int primary key Not Null, Name varchar (20),
Gender char(1), Marks int, DOB date, Stream varchar(15));

Insert into student values(1,”Manu”,’M’,98,”1997-10-01”,”Commerce”);

Insert into student values(2,”Sonia”,’F’,80,”1996-03-24”,”Science”);

Insert into student values(3,”Pranav”,’M’,60,”1998-12-12”,”Commerce”);

Insert into student values(4,”Mythra”,’F’,95,”1999-01-07”,”Science”);

Insert into student values(5,”Michal”,’M’,85,”1997-05-08”,”Science”);

1. To alter the table new coloumn City

Char(6).

ALTER TABLE STUDENT ADD CITY CHAR(6);

Output:
2. To update table Student by giving all students city as Delhi
UPDATE STUDENT SET CITY=”Delhi”;
Output:

3.To display the records in the descending order of student name


SELECT*FROM STUDENT ORDER BY NAME DESC;
Output:

4 To count all names of Students beginning with ‘M’ sorted by Name


SELECT COUNT(*) FROM STUDENT WHERE NAME LIKE”M%”;
Output:

5 To count all names of Students


SELECT STREAM, COUNT(*) FROM STUDENT GROUP BY STREAM;
Output:
3. Write SQL Commands for questions 1 to 3 on the basis of table
Student and give the output for queries 4 and 5.

TABLE:STUDENT
Rollno Name Gender Marks DOB Stream
1 Manu M 98 01/10/97 Commerce
2 Sonia F 80 24/03/98 Science
3 Pranav M 60 12/12/98 Commerce
4 Mythra F 95 07/01/98 Science
5 Michal M 85 08/05/97 Science

QUERIES

CREATE TABLE Student(Rollno int primary key Not Null, Name varchar (20),
Gender char(1), Marks int, DOB date, Stream varchar(15));
Insert into student values(1,”Manu”,’M’,98,”1997-10-01”,”Commerce”);
Insert into student values(2,”Sonia”,’F’,80,”1996-03-24”,”Science”);
Insert into student values(3,”Pranav”,’M’,60,”1998-12-12”,”Commerce”);
Insert into student values(4,”Mythra”,’F’,95,”1999-01-07”,”Science”);
Insert into student values(5,”Michal”,’M’,85,”1997-05-08”,” Science”);

1.Insert a record into table student.

Output:

2.Display all records of table Student.


Output:
3.Delete record from student where Rollno=5.

DELETE FROM STUDENT WHERE ROLLNO=5;

Output:

4.UPDATE student SET Stream=”Commerce” where CODE=6;

Output:

5.Select Name,Marks from student where Marks between 80 AND 100;

Output:
4.Write SQL Commands for questions 1 to 3 on the basis of table
Student and give the output for queries 4 and 5.

TABLE:STUDENT
Rollno Name Gender Marks DOB Stream
1 Manu M 98 01/10/97 Commerce
2 Sonia F 80 24/03/98 Science
3 Pranav M 60 12/12/98 Commerce
4 Mythra F 95 07/01/98 Science
5 Michal M 85 08/05/97 Science

QUERIES

CREATE TABLE Student(Rollno int primary key Not Null, Name varchar (20),
Gender char(1), Marks int, DOB date, Stream varchar(15));

Insert into student values(1,”Manu”,’M’,98,”1997-10-01”,”Commerce”);

Insert into student values(2,”Sonia”,’F’,80,”1996-03-24”,”Science”);

Insert into student values(3,”Pranav”,’M’,60,”1998-12-12”,”Commerce”);

Insert into student values(4,”Mythra”,’F’,95,”1999-01-07”,”Science”);

Insert into student values(5,”Michal”,’M’,85,”1997-05-08”,”Science”);

QUERIES

1.Display the highest marks of male and female student names.

SELECT Name,Max(Marks),Gender from Student group by gender;

Output:
2. Display Student details in the ascending order of Marks.
SELECT* FROM Student ORDER by Marks;
Output:

3.Display the total Marks,highest Marks,lowest Marks and average Marks.


SELECT SUM(Marks),MAX(Marks),MIN(Marks),AVG(Marks) FOM STUDENT ;
Output:

4.SELECT Name from student where Name like”a%_”;


Output:

5.SELECT COUNT(*) FROM Student WHERE GENDER=”F”;


Output:
5.Write SQL Commands for questions 1 to 3 on the basis of table
Student and give the output for queries 4 and 5.

TABLE:STUDENT
Rollno Name Gender Marks DOB Stream
1 Manu M 98 01/10/97 Commerce
2 Sonia F 80 24/03/98 Science
3 Pranav M 60 12/12/98 Commerce
4 Mythra F 95 07/01/98 Science
5 Michal M 85 08/05/97 Science

QUERIES
CREATE TABLE Student(Rollno int primary key Not Null, Name varchar (20),
Gender char(1), Marks int, DOB date, Stream varchar(15));
Insert into student values(1,”Manu”,’M’,98,”1997-10-01”,”Commerce”);
Insert into student values(2,”Sonia”,’F’,80,”1996-03-24”,”Science”);
Insert into student values(3,”Pranav”,’M’,60,”1998-12-12”,”Commerce”);
Insert into student values(4,”Mythra”,’F’,95,”1999-01-07”,”Science”);
Insert into student values(5,”Michal”,’M’,85,”1997-05-08”,” Science”);
Table fees
Rollno Fee Concession
2 5000 Yes
3 4500 No
4 5500 No
QUERIES

Create table fees(Rollno int,Fee int, Concession char(3);


Insert into Fees values(2,5000,”Yes”),(3,4500,”No”),(4,5500,”No”;)
1.Display the details of the student who paid fees.

SELECT student.Rollno, Name from student,Fees where


student.Rollno=Fees.Rollno;
Output:

2.Display the details of students who paid fees and concession=”yes”

SELECT student.Rollno, Name from student,Fees where


student.Rollno=Fees.Rollno and concession=”Yes”;
Output:

3.Display the details of Science stream Student who paid fees

SELECT student.Rollno, Name from student,Fees where


student.Rollno=Fees.Rollno and stream=”Science”;
Output:
4SELECT student.Rollno, Name from student,Fees where
student.Rollno=Fees.Rollno and concession=”No”;
Output:

5.Select count(*) from fees group by concession;


Output:
Python & MySQL Connectivity
TABLE:STUDENT
Rollno Name Gender Marks DOB Stream
1 Manu M 98 01/10/97 Commerce
2 Sonia F 80 24/03/98 Science
3 Pranav M 60 12/12/98 Commerce
4 Mythra F 95 07/01/98 Science
5 Michal M 85 08/05/97 Science

QUERIES
CREATE TABLE Student(Rollno int primary key Not Null, Name varchar (20),
Gender char(1), Marks int, DOB date, Stream varchar(15));

Insert into student values(1,”Manu”,’M’,98,”1997-10-01”,”Commerce”);

Insert into student values(2,”Sonia”,’F’,80,”1996-03-24”,”Science”);

Insert into student values(3,”Pranav”,’M’,60,”1998-12-12”,”Commerce”);

Insert into student values(4,”Mythra”,’F’,95,”1999-01-07”,”Science”);

Insert into student values(5,”Michal”,’M’,85,”1997-05-08”,”Science”);


Program 1
Program to connect with database and store record of student and display record

Program Code
import mysql.connector as db
con=db.connect(host='localhost',user='root',passwd='123’,database='SCHOOL')
cur = con.cursor()
cur.execute(“insert into student values(6,’AKSHAY’,’M’,70,’1998-02-
03,’Commerce’)”)

cur.execute(“select*from student”)

rec=cur.fetchall()

for r in rec:

print(r)

con.commit()

con.close()

OUTPUT

6,’AKSHAY’,’M’,70,’1998-02-03,’Commerce’
Program 2
Program to connect with database and search student number in table student
and display record, if student not found display appropriate message.

Program Code
import mysql.connector as db
con=db.connect(host='localhost',user='root',passwd='123’,database='SCHOOL')
cur = con.cursor()
sid=int(input(“Enter Rollno to be searched:”))
cur.execute(“select*from student where Rollno=’’+str(sid)”)

rec=cur.fetchall()

if len(rec)!=0:

print(rec)

else:

print(“Record added”)

con.commit()

con.close()

Enter stud_id to be searched:1


(1,”Manu”,98,”1997-10-01”,”Commerce”)
Enter stud_id to be searched:102
Record not found
OUTPUT
Program 3
Program to connect with database and insert a new record into the table student

Program Code
import mysql.connector as db
con=db.connect(host='localhost',user='root',password="123",database
='studen')
#if (con is connected()):
print("Successfully connected")
cur=con.cursor()
rno=int(input("Enter the roll no:"))
name=input("Enter the new name:")
query="update student set name='{}' where rno={}".format(name,rno)
#update into SQL table
cur.execute(query)
con.commit()
print("Record updated successfully")
cur.close()
con.close()
OUTPUT
Successfully connected
Enter the roll no:1
Enter the new name:Alan
Record updated successfully
>>>
Program 4
Program to connect with database and delete a record from the table

Program Code
import mysql.connector as db

con=db.connect(host='localhost',user='root',password="123",database
='studen')

#if (con is connected()):

print("Successfully connected")

cur=con.cursor()

name=input("Enter the name to be deleted:")

query="delete from student where name='{}'".format(name)

#deletion of record

cur.execute(query)

con.commit()

print("Record deleted successfully")

cur.close()

con.close()
OUTPUT
Successfully connected

Enter the name to be deleted:Arjun

Record deleted successfully

>>>

******************************

You might also like