0% found this document useful (0 votes)
14 views12 pages

Python Practical Record for Class XII

The document contains a comprehensive collection of Python practical programs and SQL case studies for Class XII (CBSE) Computer Science. It includes examples of loops, functions, file handling, and database connectivity, along with outputs for each program. Additionally, it covers stack implementation and various operations related to data management in Python and SQL.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Python Practical Record for Class XII

The document contains a comprehensive collection of Python practical programs and SQL case studies for Class XII (CBSE) Computer Science. It includes examples of loops, functions, file handling, and database connectivity, along with outputs for each program. Additionally, it covers stack implementation and various operations related to data management in Python and SQL.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COMPUTER SCIENCE (083)

PYTHON PRACTICAL RECORD – CLASS XII (CBSE)

1. REVIEW OF CLASS XI CONCEPTS


Program 1: For Loop – Print squares of even numbers (1–
10)
even_squares = []
for i in range(2, 11, 2):
even_squares.append(i**2)
print(even_squares)

Output
[4, 16, 36, 64, 100]

Program 2: For Loop – Count vowels in a string


text = "COMPUTER"
vowels = "AEIOU"
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Vowels:", count)

Output
Vowels: 3

Program 3: While Loop – Reverse a number


num = 1234
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
print(rev)

Output
4321
Program 4: While Loop – Sum of digits
num = 456
s = 0
while num > 0:
s += num % 10
num //= 10
print(s)

Output
15

Program 5: List – Remove duplicates


lst = [1,2,2,3,4,4,5]
unique = list(set(lst))
print(unique)

Output
[1, 2, 3, 4, 5]

Program 6: List – Linear search


lst = [10,20,30,40]
key = 30
found = False
for i in lst:
if i == key:
found = True
break
print("Found" if found else "Not Found")

Output
Found

Program 7: String – Check palindrome


s = "LEVEL"
print("Palindrome" if s == s[::-1] else "Not Palindrome")

Output
Palindrome
Program 8: String – Frequency of character
s = "PYTHON"
print([Link]('O'))

Output
1

Program 9: Dictionary – Student marks average


marks = {'Math':80,'CS':90,'English':70}
avg = sum([Link]())/len(marks)
print(avg)

Output
80.0

Program 10: Dictionary – Search key


d = {'A':1,'B':2,'C':3}
print('B' in d)

Output
True

Program 11: Tuple – Max and Min


t = (10,40,20,5)
print(max(t), min(t))

Output
40 5

Program 12: Tuple – Convert to list


t = (1,2,3)
l = list(t)
print(l)

Output
[1, 2, 3]

2. USER DEFINED FUNCTIONS


Program 13: Factorial using function
def factorial(n):
f = 1
for i in range(1, n+1):
f *= i
return f

print(factorial(5))

Output
120

Program 14: Prime number check


def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True

print(is_prime(11))

Output
True

Program 15: Fibonacci series


def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a+b

fibonacci(5)

Output
0 1 1 2 3

Program 16: Count uppercase letters


def count_upper(s):
return sum(1 for ch in s if [Link]())

print(count_upper("PyTHon"))

Output
3

Program 17: Simple calculator


def calc(a, b):
return a+b, a-b, a*b

print(calc(8,4))

Output
(12, 4, 32)

3. SQL CASE STUDY QUESTIONS


Case Study 1: Student Management System
Table: Student
CREATE TABLE Student(
Roll INT PRIMARY KEY,
Name VARCHAR(20),
Marks INT
);

INSERT INTO Student VALUES(1,'Raj',88);

SELECT * FROM Student;

Output
1 Raj 88
Case Study 2: Employee Payroll System
Table: Employee
CREATE TABLE Employee(
EID INT PRIMARY KEY,
Name VARCHAR(20),
Salary INT
);

INSERT INTO Employee VALUES(101,'Rahul',45000);

SELECT Name, Salary FROM Employee;

Output
Rahul 45000

Case Study 3: Library Management System


Table: Books
CREATE TABLE Books(
BookID INT PRIMARY KEY,
Title VARCHAR(30),
Author VARCHAR(20)
);

INSERT INTO Books VALUES(1,'Python Basics','Guido');

SELECT * FROM Books;

Output
1 Python Basics Guido

Case Study 4: Bank Account System


Table: Account
CREATE TABLE Account(
AccNo INT PRIMARY KEY,
Name VARCHAR(20),
Balance INT
);

INSERT INTO Account VALUES(5001,'Amit',10000);

UPDATE Account SET Balance=12000 WHERE AccNo=5001;


SELECT * FROM Account;

Output
5001 Amit 12000

Case Study 5: School Fee System


Table: Fee
CREATE TABLE Fee(
Roll INT,
Name VARCHAR(20),
Amount INT
);

INSERT INTO Fee VALUES(2,'Neha',25000);

SELECT Name, Amount FROM Fee;

Output
Neha 25000

4. FILE HANDLING
Text File Programs
Program 23: Write student data
f = open("[Link]","w")
[Link]("Python")
[Link]()

Output
File Written

Program 24: Read data


f = open("[Link]","r")
print([Link]())
[Link]()

Output
Python
Program 25: Count characters
f = open("[Link]","r")
data = [Link]()
print(len(data))
[Link]()

Output
9

Program 26: Append data


f = open("[Link]","a")
[Link](" CS")
[Link]()

Program 27: Read line by line


f = open("[Link]","r")
for line in f:
print(line)
[Link]()

Binary File Programs


Program 28: Store list using pickle
import pickle
lst = [1,2,3]
f = open("[Link]","wb")
[Link](lst,f)
[Link]()

Program 29: Read binary file


import pickle
f = open("[Link]","rb")
print([Link](f))
[Link]()

Output
[1, 2, 3]

Program 30: Store dictionary


[Link]({'A':10}, open("[Link]","wb"))

Program 31: Read dictionary


print([Link](open("[Link]","rb")))
Output
{'A': 10}

Program 32: Count elements


lst = [Link](open("[Link]","rb"))
print(len(lst))

Output
3

CSV File Programs


Program 33: Write CSV
import csv
with open("[Link]","w",newline="") as f:
w = [Link](f)
[Link](["Roll","Name"])
[Link]([1,"Akshara"])

Program 34: Read CSV


import csv
with open("[Link]","r") as f:
r = [Link](f)
for row in r:
print(row)

Output
['Roll', 'Name']
['1', 'Akshara']

Program 35: Count rows


import csv
with open("[Link]","r") as f:
print(sum(1 for row in f))

Output
2

Program 36: Display only names


import csv
with open("[Link]","r") as f:
r = [Link](f)
next(r)
for row in r:
print(row[1])

Output
Akshara

Program 37: Append record


import csv
with open("[Link]","a",newline="") as f:
w = [Link](f)
[Link]([2,"Maheshwari"])

5. PYTHON SQL CONNECTIVITY


Program 38: Connect to School Database
import [Link]
con = [Link](
host='localhost',
user='root',
password='1234',
database='school'
)
print("Connected")

Output
Connected

Program 39: Connect to Library Database


import [Link]
con = [Link](
host='[Link]',
user='admin',
password='admin123',
database='library'
)
print("Library DB Connected")

Output
Library DB Connected
Program 40: Insert Record into Employee Database
import [Link]
con = [Link](
host='localhost',
user='root',
password='root123',
database='company'
)
cur = [Link]()
[Link]("INSERT INTO employee VALUES(1,'Rohit',40000)")
[Link]()
print("Record Inserted")

Output
Record Inserted

Program 41: Fetch Records from Bank Database


import [Link]
con = [Link](
host='[Link]',
user='bankuser',
password='bank123',
database='bank'
)
cur = [Link]()
[Link]("SELECT * FROM account")
print([Link]())

Output
[(5001, 'Amit', 12000)]

Program 42: Delete Record from Student Database


import [Link]
con = [Link](
host='localhost',
user='root',
password='1234',
database='school'
)
cur = [Link]()
[Link]("DELETE FROM student WHERE roll=1")
[Link]()
print("Record Deleted")
Output
Record Deleted

6. STACK IMPLEMENTATION
Program 43: Create stack
stack = []

Program 44: Push elements


[Link](10)
[Link](20)
print(stack)

Output
[10, 20]

Program 45: Pop element


[Link]()
print(stack)

Output
[10]

Program 46: Peek operation


print(stack[-1])

Output
10

Program 47: Check empty stack


print(len(stack)==0)

Output
False

You might also like