12 TH Practical File
12 TH Practical File
age = 30 # Integer
print("Name:", name)
print("Age:", age)
print("Height:", height)
Output:
Name: Alice
Age: 30
Height: 5.5
Is student: False
a = 10
b = 3
Expected Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3.3333333333333335
a // b = 3
a % b = 1
a ** b = 1000
age_int = int(age_str)
Output:
Enter your name: Bob
Hello, Bob!
grade = "A"
grade = "B"
grade = "C"
grade = "D"
else:
grade = "F"
Example Interaction:
Enter your score (0-100): 85
for i in range(5):
print(i)
print(num)
Expected Output:
Numbers from 0 to 4:
4
Even numbers from 2 to 8:
count = 1
print(count)
count += 1
print("Loop finished.")
Expected Output:
Counting up to 5 using a while loop:
Loop finished.
def greet_user():
user_name = "Guest"
greet_user()
greet_user()
Expected Output:
Hello, Guest! Welcome.
return total
result2 = add_numbers(-3, 8)
Expected Output:
Sum 1: 15
Sum 2: 5
try:
num_int = int(num_input_str)
except ValueError:
file_name = "my_notes.txt"
try:
except IOError:
Expected Output:
Successfully wrote to ' my_notes.txt '
Adding a number: 42
file_name = "my_notes.txt"
try:
lines = file_object.readlines()
except FileNotFoundError:
except IOError:
Expected Output:
--- Content of ' my_notes.txt ' ---
Adding a number: 42
import math
number = 16
square_root = math.sqrt(number)
value = 3.7
Expected Output:
The square root of 16 is 4.0
file_name = "student_data.csv"
data_to_write = [
try:
csv_writer = csv.writer(csvfile)
csv_writer.writerows(data_to_write)
except IOError:
Expected Output:
Data successfully written to ' student_data.csv '
Alice,10,A
Bob,11,B+
Charlie,10,A-
import csv
file_name = "student_data.csv"
try:
csv_reader = csv.reader(csvfile)
print("\n--- Content of '", file_name, "' ---")
print(row)
except FileNotFoundError:
except IOError:
Expected Output:
--- Content of ' student_data.csv ' ---
import random
random_float = random.random()
random_choice = random.choice(my_list)
stack = []
# Push operations
stack.append("Book A")
stack.append("Book B")
stack.append("Book C")
# Pop operation
if stack:
item_removed = stack.pop()
else:
# Peek operation
if stack:
top_item = stack[-1]
else:
Expected Output:
Stack after pushes: ['Book A', 'Book B', 'Book C']
queue = []
# Enqueue operations
queue.append("Person 1")
queue.append("Person 2")
queue.append("Person 3")
# Dequeue operation
if queue:
item_removed = queue.pop(0)
else:
# Peek operation
if queue:
front_item = queue[0]
else:
Expected Output:
Queue after enqueues: ['Person 1', 'Person 2', 'Person 3']
age INT,
grade VARCHAR(2),
marks INT
);
Output:
Table 'students' created successfully.
-- Drop attribute
DESCRIBE students;
Output:
Column 'email' added successfully.
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
Output:
1 row updated.
2 rows updated.
1 row updated.
+------------+---------+-----+-------+-------+
+------------+---------+-----+-------+-------+
| 1 | Alice | 20 | A | 85 |
| 2 | Bob | 19 | A | 88 |
| 3 | Charlie | 21 | A | 92 |
| 4 | Diana | 21 | C | 65 |
| 5 | Eve | 22 | B | 80 |
+------------+---------+-----+-------+-------+
Output:
Ascending order by marks:
+------------+---------+-----+-------+-------+
+------------+---------+-----+-------+-------+
| 4 | Diana | 21 | C | 65 |
| 5 | Eve | 22 | B | 80 |
| 1 | Alice | 20 | A | 85 |
| 2 | Bob | 19 | A | 88 |
| 3 | Charlie | 21 | A | 92 |
+------------+---------+-----+-------+-------+
+------------+---------+-----+-------+-------+
+------------+---------+-----+-------+-------+
| 3 | Charlie | 21 | A | 92 |
| 2 | Bob | 19 | A | 88 |
| 1 | Alice | 20 | A | 85 |
| 5 | Eve | 22 | B | 80 |
| 4 | Diana | 21 | C | 65 |
+------------+---------+-----+-------+-------+
Output:
1 row deleted.
+------------+---------+-----+-------+-------+
+------------+---------+-----+-------+-------+
| 1 | Alice | 20 | A | 85 |
| 2 | Bob | 19 | A | 88 |
| 3 | Charlie | 21 | A | 92 |
| 5 | Eve | 22 | B | 80 |
+------------+---------+-----+-------+-------+
+-------------------+
| remaining_students |
+-------------------+
| 4 |
+-------------------+
SELECT
grade,
COUNT(*) as student_count,
MIN(marks) as min_marks,
MAX(marks) as max_marks,
SUM(marks) as total_marks,
AVG(marks) as average_marks
FROM students
GROUP BY grade;
SELECT
CASE
END as age_group,
COUNT(*) as count
FROM students
GROUP BY age_group;
Output:
Statistics by grade:
+-------+---------------+-----------+-----------+-------------+---------------+
+-------+---------------+-----------+-----------+-------------+---------------+
| A | 3 | 85 | 92 | 265 | 88.33 |
| B | 1 | 80 | 80 | 80 | 80.00 |
+-------+---------------+-----------+-----------+-------------+---------------+
Statistics by age group:
+-----------+-------+
| age_group | count |
+-----------+-------+
| Under 20 | 1 |
| 20-21 | 2 |
| Above 21 | 1 |
+-----------+-------+
import sqlite3
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('''
age INTEGER,
grade TEXT,
marks INTEGER
''')
# Close connection
conn.close()
Output:
Database connected successfully!
Students table created successfully!
import sqlite3
# Connect to database
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('''
VALUES (?, ?, ?, ?)
students_data = [
cursor.executemany('''
VALUES (?, ?, ?, ?)
''', students_data)
# Commit changes
conn.commit()
conn.close()
Output:
Single record inserted successfully!
import sqlite3
# Connect to database
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
all_students = cursor.fetchall()
print("All Students:")
print("-" * 60)
print("-" * 60)
a_grade_students = cursor.fetchall()
print("-" * 25)
for student in a_grade_students:
# Close connection
conn.close()
Output:
All Students:
------------------------------------------------------------
------------------------------------------------------------
1 Alice Johnson 20 A 85
2 Bob Smith 19 B 78
3 Charlie Brown 21 A 92
4 Diana Prince 20 C 65
5 Eve Adams 22 B 80
-------------------------
import sqlite3
# Connect to database
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('''
UPDATE students
SET marks = ?
WHERE name = ?
cursor.execute('''
UPDATE students
''')
# Delete record
remaining_students = cursor.fetchall()
print("\nRemaining Students:")
print("-" * 30)
# Commit changes
conn.commit()
# Close connection
conn.close()
Output:
Updated 1 record(s)
Deleted 1 record(s)
Remaining Students:
------------------------------
import sqlite3
# Connect to database
conn = sqlite3.connect('school.db')
cursor = conn.cursor()
cursor.execute('''
SELECT
grade,
COUNT(*) as student_count,
MIN(marks) as min_marks,
MAX(marks) as max_marks,
AVG(marks) as avg_marks,
SUM(marks) as total_marks
FROM students
GROUP BY grade
ORDER BY grade
''')
grade_stats = cursor.fetchall()
print("Statistics by Grade:")
print("-" * 80)
print("-" * 80)
# Overall statistics
cursor.execute('''
SELECT
COUNT(*) as total_students,
AVG(age) as avg_age,
AVG(marks) as avg_marks,
MIN(marks) as lowest_marks,
MAX(marks) as highest_marks
FROM students
''')
overall_stats = cursor.fetchone()
print(f"\nOverall Statistics:")
print("-" * 40)
# Close connection
conn.close()
Output:
Statistics by Grade:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
A 3 85 92 88.33 265
B 1 80 80 80.00 80
Overall Statistics:
----------------------------------------
Total Students: 4
Lowest Marks: 80
Highest Marks: 92