0% found this document useful (0 votes)
15 views17 pages

Grade 12 Python Practical Codes

The document contains practical codes for Grade 12 CBSE Computer Science for the academic year 2025-26, submitted by Moksh Chaudhary. It includes a list of 17 practical tasks, each with a brief description and corresponding Python code examples, covering various programming concepts such as input/output functions, arithmetic operations, file handling, and database interactions. Additionally, it features a mini project for a Student Management System utilizing SQLite for data management.

Uploaded by

tawewo3796
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)
15 views17 pages

Grade 12 Python Practical Codes

The document contains practical codes for Grade 12 CBSE Computer Science for the academic year 2025-26, submitted by Moksh Chaudhary. It includes a list of 17 practical tasks, each with a brief description and corresponding Python code examples, covering various programming concepts such as input/output functions, arithmetic operations, file handling, and database interactions. Additionally, it features a mini project for a Student Management System utilizing SQLite for data management.

Uploaded by

tawewo3796
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

Grade 12 CBSE Computer Science Practical Codes

Academic year: 2025-26

Submitted By:
Name: Moksh Chaudhary
Class: 12B
Roll No.: 17
Sub. Code:083
Under the guidance of:

Mr. Sahil Gajjar


SR NO. PRACTICALS
1. Write a python program to demonstrate input and output function.
2. Write a program to perform arithmetic operations in python.
3. Create a code to perform logical operators in python
4. Write a program to perform all relational operators and conditional
statements in python.
5. Generates the patterns.
6. Input a number and check if the number is prime or not.
7. Determine whether a number is a perfect number, an Armstrong number
or a palindrome.
8. Read a text file line by line and display each word separated by a #.
9. Read a text file and display the number of
vowels/consonants/uppercase/Lowercase characters in the file.
10. Create a binary file with name and roll number, search for a given roll
number and display the name.
11. Create a binary file with roll number, name and marks. Input a roll
number and update the marks.
12. Write a random number generator that generates random number
between 1 and 6 (simulates a dice).
13. Write a program to implement a stack using a list.
14. Write a program that calculates students grade.
15. Write a program that calculates simple interest and compound interest.
16. Create a CSV file by entering user-id and password, read and search the
password for given user id.
17. Mini project.
1. Write a python program to demonstrate input and output function.
# Program to demonstrate input and output functions
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "! You are", age, "years old.")

Output:

2. Write a program to perform arithmetic operations in python.


# Program for arithmetic operations
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)

Output:
3. Create a code to perform logical operators in python
# Program for logical operators
a = True
b = False
print("a and b:", a and b)
print("a or b:", a or b)
print("not a:", not a)

Output:

4. Write a program to perform all relational operators and conditional statements in


python.
# Program for relational operators and conditional statements
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("a is greater than b")
elif a < b:
print("a is less than b")
else:
print("a is equal to b")

Output:
5. Generates the following patterns.
# Pattern 1: Decreasing stars
for i in range(5, 0, -1):
print("* " * i)

Output:

# Pattern 2: Increasing stars


for i in range(1, 6):
print("* " * i)

Output:

# Pattern 3: Number pattern


for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()

Output:
6. Input a number and check if the number is prime or not.
n = int(input("Enter a number: "))
if n > 1:
for i in range(2, n):
if n % i == 0:
print(n, "is not a prime number")
break
else:
print(n, "is a prime number")
else:
print(n, "is not a prime number")

Output:
7. Determine whether a number is a perfect number, an Armstrong number or a
palindrome.
n = int(input("Enter a number: "))

# Perfect number
sum1 = 0
for i in range(1, n):
if n % i == 0:
sum1 += i
if sum1 == n:
print(n, "is a perfect number")
else:
print(n, "is not a perfect number")

# Armstrong number
num = n
sum2 = 0
while num > 0:
digit = num % 10
sum2 += digit ** 3
num //= 10
if n == sum2:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")

# Palindrome
if str(n) == str(n)[::-1]:
print(n, "is a palindrome")
else:
print(n, "is not a palindrome")

Output:
8. Read a text file line by line and display each word separated by a #.
with open("[Link]", "r") as f:
for line in f:
words = [Link]()
print("#".join(words))

Output:

The Sample File:

9. Read a text file and display the number of


vowels/consonants/uppercase/Lowercase characters in the file.
vowels = "aeiouAEIOU"
vc = cc = uc = lc = 0
with open("[Link]", "r") as f:
text = [Link]()
for ch in text:
if [Link]():
if ch in vowels:
vc += 1
else:
cc += 1
if [Link]():
uc += 1
elif [Link]():
lc += 1
print("Vowels:", vc)
print("Consonants:", cc)
print("Uppercase:", uc)
print("Lowercase:", lc)
Output:

The Sample File:

10. Create a binary file with name and roll number, search for a given roll number
and display the name.
import pickle

# Create binary file


f = open("[Link]", "wb")
n = int(input("Enter number of records: "))
for i in range(n):
name = input("Enter name: ")
roll = int(input("Enter roll number: "))
[Link]([roll, name], f)
[Link]()

# Search record
f = open("[Link]", "rb")
r = int(input("Enter roll number to search: "))
found = False
try:
while True:
data = [Link](f)
if data[0] == r:
print("Name:", data[1])
found = True
except EOFError:
pass
if not found:
print("Record not found")
[Link]()

Output:

11. Create a binary file with roll number, name and marks. Input a roll number and
update the marks.
import pickle

# Create binary file


f = open("[Link]", "wb")
n = int(input("Enter number of records: "))
for i in range(n):
roll = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = float(input("Enter marks: "))
[Link]([roll, name, marks], f)
[Link]()

# Update marks
roll_no = int(input("Enter roll number to update marks: "))
records = []
f = open("[Link]", "rb")
try:
while True:
[Link]([Link](f))
except EOFError:
pass
[Link]()

f = open("[Link]", "wb")
for rec in records:
if rec[0] == roll_no:
rec[2] = float(input("Enter new marks: "))
[Link](rec, f)
[Link]()
print("Record updated successfully.")

Output:

12. Write a random number generator that generates random number between 1
and 6 (simulates a dice)
import random
print("Dice rolled:", [Link](1, 6))

Output:

13. Write a program to implement a stack using a list.


stack = []
def push():
[Link](input("Enter element to push: "))
print("Stack:", stack)
def pop():
if len(stack) == 0:
print("Stack is empty!")
else:
print("Popped:", [Link]())
while True:
ch = int(input("[Link] [Link] [Link]: "))
if ch == 1:
push()
elif ch == 2:
pop()
else:
break

Output:

14. Write a program that calculates students grade.


marks = int(input("Enter marks: "))
if marks >= 90:
grade = "A+"
elif marks >= 80:
grade = "A"
elif marks >= 70:
grade = "B"
elif marks >= 60:
grade = "C"
else:
grade = "Fail"
print("Grade:", grade)

Output:
15. Write a program that calculates simple interest and compound interest.
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = float(input("Enter time: "))
si = (p * r * t) / 100
ci = p * (1 + r / 100) ** t - p
print("Simple Interest:", si)
print("Compound Interest:", ci)

Output:

16. Create a CSV file by entering user-id and password, read and search the
password for given user id.
import csv

# Create CSV file


f = open("[Link]", "w", newline="")
w = [Link](f)
for i in range(3):
uid = input("Enter user ID: ")
pwd = input("Enter password: ")
[Link]([uid, pwd])
[Link]()

# Search for user ID


uid = input("Enter user ID to search: ")
found = False
f = open("[Link]", "r")
r = [Link](f)
for row in r:
if row[0] == uid:
print("Password:", row[1])
found = True
break
if not found:
print("User ID not found")
[Link]()

Output:

17. mini project.


Student Management System
students = {}
while True:
print("\[Link] Student [Link] [Link]")
ch = int(input("Enter choice: "))
if ch == 1:
roll = int(input("Enter roll: "))
name = input("Enter name: ")
students[roll] = name
elif ch == 2:
for r, n in [Link]():
print(r, ":", n)
else:
break

Output:
Program 2 — Insert a Record
import sqlite3

con = [Link]("[Link]")
cur = [Link]()

r = int(input("Enter roll: "))


n = input("Enter name: ")

[Link]("INSERT INTO student VALUES(?, ?)", (r, n))


[Link]()

print("Record inserted.")
[Link]()

Output:
Program 3 — Display All Records
import sqlite3

con = [Link]("[Link]")
cur = [Link]()

[Link]("SELECT * FROM student")


data = [Link]()

for row in data:


print(row)

[Link]()

Output:

Program 4 — Search a Record


import sqlite3

con = [Link]("[Link]")
cur = [Link]()

r = int(input("Enter roll to search: "))

[Link]("SELECT * FROM student WHERE roll = ?", (r,))


rec = [Link]()

if rec:
print("Record found:", rec)
else:
print("Record not found.")
[Link]()
Output:

Program 5 — Update a Record


import sqlite3

con = [Link]("[Link]")
cur = [Link]()

r = int(input("Enter roll to update: "))


new_name = input("Enter new name: ")

[Link]("UPDATE student SET name = ? WHERE roll = ?", (new_name, r))


[Link]()

print("Record updated (if roll existed).")


[Link]()

Output:

You might also like