🏆 Revision & Practice · Lesson 19
Important Programs & SQL Queries
How to prepare the practical file
- 1Write the aim of each program in one line.
- 2Type and run the code yourself; change the sample values to your own.
- 3Paste or write the exact output below the code.
- 4For SQL, first create the STUDENT table from the SQL lessons, then run each query.
Python: basic programs
Python
# 1. Bill with GST
items = {'Notebook': (45, 4), 'Pen': (10, 6), 'Bag': (650, 1)}
total = 0
for name, (price, qty) in items.items():
amount = price * qty
total += amount
print(f'{name:<9}{qty:>3} x {price:>4} = {amount:>5}')
gst = total * 18 / 100
print('Total:', total, '| GST 18%:', gst, '| Pay:', total + gst)Notebook 4 x 45 = 180
Pen 6 x 10 = 60
Bag 1 x 650 = 650
Total: 890 | GST 18%: 160.2 | Pay: 1050.2
Python
# 2. Simple interest and EMI-style total
p, r, t = 50000, 9, 2
si = p * r * t / 100
print('Interest:', si, '| Amount:', p + si, '| Per month:', round((p + si) / (t * 12), 2))Interest: 9000.0 | Amount: 59000.0 | Per month: 2458.33
Python
# 3. Profit or loss
cp, sp = 1200, 1350
if sp > cp:
print('Profit of', sp - cp, '=', round((sp - cp) / cp * 100, 2), '%')
elif cp > sp:
print('Loss of', cp - sp, '=', round((cp - sp) / cp * 100, 2), '%')
else:
print('No profit no loss')Profit of 150 = 12.5 %
Python
# 4. Grade calculator for many students
marks = {'Asha': 91, 'Ravi': 76, 'Neel': 58, 'Zoya': 29}
for name, m in marks.items():
g = 'A' if m >= 90 else 'B' if m >= 75 else 'C' if m >= 50 else 'D' if m >= 33 else 'E'
print(name, m, g)Asha 91 A
Ravi 76 B
Neel 58 C
Zoya 29 E
Python: lists
Python
# 5. Sum and average of numbers in a list without sum()
nums = [23, 45, 12, 67, 34]
total = 0
for n in nums:
total += n
print('Sum =', total, '| Average =', total / len(nums))Sum = 181 | Average = 36.2
Python
# 6. Largest and smallest without max()/min()
nums = [23, 45, 12, 67, 34]
big = small = nums[0]
for n in nums[1:]:
if n > big: big = n
if n < small: small = n
print('Largest:', big, '| Smallest:', small)Largest: 67 | Smallest: 12
Python
# 7. Count even, odd, positive, negative
nums = [12, -7, 0, 33, -40, 18, 5]
even = sum(1 for n in nums if n % 2 == 0)
pos = sum(1 for n in nums if n > 0)
neg = sum(1 for n in nums if n < 0)
print('Even:', even, 'Odd:', len(nums) - even, 'Positive:', pos, 'Negative:', neg)Even: 4 Odd: 3 Positive: 4 Negative: 2
Python
# 8. Search an element and its frequency
L = [4, 9, 2, 9, 7, 9, 1]
x = 9
if x in L:
print(x, 'found first at index', L.index(x), 'and occurs', L.count(x), 'times')9 found first at index 1 and occurs 3 times
Python: statistics
Python
# 9. Mean, median, mode without the statistics module
data = [7, 3, 9, 3, 5, 3, 8, 6]
s = sorted(data)
n = len(s)
mean = sum(s) / n
median = (s[n // 2 - 1] + s[n // 2]) / 2 if n % 2 == 0 else s[n // 2]
mode = max(s, key=s.count)
print('Sorted:', s)
print('Mean:', mean, 'Median:', median, 'Mode:', mode)Sorted: [3, 3, 3, 5, 6, 7, 8, 9]
Mean: 5.5 Median: 5.5 Mode: 3
Python
# 10. Standard deviation step by step
data = [10, 12, 23, 23, 16, 23, 21, 16]
m = sum(data) / len(data)
var = sum((x - m) ** 2 for x in data) / len(data)
print('Mean:', m, '| Variance:', var, '| SD:', round(var ** 0.5, 2))Mean: 18.0 | Variance: 24.0 | SD: 4.9
Python: dictionaries
Python
# 11. Dictionary of countries and capitals – search
capitals = {'India': 'New Delhi', 'Japan': 'Tokyo', 'France': 'Paris', 'Kenya': 'Nairobi'}
for c in ('Japan', 'Brazil'):
print(c, '->', capitals.get(c, 'not in dictionary'))Japan -> Tokyo
Brazil -> not in dictionary
Python
# 12. Word frequency in a sentence
text = 'data is new oil and data is power'
freq = {}
for w in text.split():
freq[w] = freq.get(w, 0) + 1
print(freq)
print('Most frequent:', max(freq, key=freq.get)){'data': 2, 'is': 2, 'new': 1, 'oil': 1, 'and': 1, 'power': 1}
Most frequent: data
Python
# 13. Employees earning above average
sal = {'E1': 42000, 'E2': 35000, 'E3': 58000, 'E4': 39000}
avg = sum(sal.values()) / len(sal)
print('Average salary:', avg)
print('Above average:', [e for e, s in sal.items() if s > avg])Average salary: 43500.0
Above average: ['E3']
Python
# 14. Merge two dictionaries of stock
shop1 = {'pen': 40, 'pencil': 25}
shop2 = {'pen': 15, 'eraser': 30}
total = dict(shop1)
for item, q in shop2.items():
total[item] = total.get(item, 0) + q
print(total){'pen': 55, 'pencil': 25, 'eraser': 30}
NumPy programs
Python
# 15. NumPy: marks of 4 students in 3 subjects
import numpy as np
m = np.array([[78, 85, 90], [66, 72, 58], [92, 88, 95], [55, 61, 70]])
print('Student totals :', m.sum(axis=1))
print('Subject average:', m.mean(axis=0).round(2))
print('Topper index :', m.sum(axis=1).argmax())Student totals : [253 196 275 186]
Subject average: [72.75 76.5 78.25]
Topper index : 2
Python
# 16. NumPy: filter, count and replace
import numpy as np
t = np.array([28, 35, 41, 30, 44, 39, 26])
print('Above 35:', t[t > 35], '| count:', (t > 35).sum())
t[t > 40] = 40 # cap values at 40
print('Capped:', t)Above 35: [41 44 39] | count: 3
Capped: [28 35 40 30 40 39 26]
Python
# 17. NumPy: reshape, transpose, concatenate import numpy as np a = np.arange(1, 7).reshape(2, 3) print(a) print(a.T) print(np.concatenate((a, a * 10), axis=0))
[[1 2 3]
[4 5 6]]
[[1 4]
[2 5]
[3 6]]
[[ 1 2 3]
[ 4 5 6]
[10 20 30]
[40 50 60]]
Python
# 18. NumPy statistics
import numpy as np
runs = np.array([45, 12, 78, 102, 33, 67])
print('Total:', runs.sum(), 'Mean:', round(runs.mean(), 2), 'Median:', np.median(runs))
print('Max:', runs.max(), 'Min:', runs.min(), 'SD:', round(runs.std(), 2))Total: 337 Mean: 56.17 Median: 56.0
Max: 102 Min: 12 SD: 29.74
Python
# 19. Save and load an array with a CSV file
import numpy as np
scores = np.array([[1, 55], [2, 78], [3, 91]])
np.savetxt('scores.csv', scores, delimiter=',', fmt='%d')
back = np.loadtxt('scores.csv', delimiter=',', dtype=int)
print(back)
print('Average score:', back[:, 1].mean())[[ 1 55]
[ 2 78]
[ 3 91]]
Average score: 74.66666666666667
Menu-driven program
In the practical, replace the list choices with input() inside a while True loop.
Python
# 20. Menu-driven program on a list of marks
marks = [56, 78, 91]
choices = ['1', '2', '3', '4'] # simulated menu choices
for ch in choices:
if ch == '1':
marks.append(64); print('Added 64 ->', marks)
elif ch == '2':
print('Average:', round(sum(marks) / len(marks), 2))
elif ch == '3':
print('Sorted:', sorted(marks, reverse=True))
else:
print('Exit')Added 64 -> [56, 78, 91, 64]
Average: 72.25
Sorted: [91, 78, 64, 56]
Exit
SQL queries
All queries use the STUDENT table (RollNo 101–107) and run in order.
Q1
SQL
SELECT Name, Marks FROM STUDENT WHERE Class = 'XI-A' ORDER BY Marks DESC;
Output
+--------+-------+ | Name | Marks | +--------+-------+ | Sana | 92.0 | | Aarav | 88.5 | | Ishaan | 54.0 | | Rohan | 39.5 | +--------+-------+
Q2
SQL
SELECT Name, City FROM STUDENT WHERE City NOT IN ('Jaipur', 'Delhi');Output
+------+---------+ | Name | City | +------+---------+ | Sana | Lucknow | | Diya | Pune | +------+---------+
Q3
SQL
SELECT Name, DOB FROM STUDENT WHERE DOB BETWEEN '2009-01-01' AND '2009-06-30';
Output
+--------+------------+ | Name | DOB | +--------+------------+ | Aarav | 2009-04-15 | | Diya | 2009-02-11 | | Ishaan | 2009-06-30 | +--------+------------+
Q4
SQL
SELECT RollNo, Name FROM STUDENT WHERE Name LIKE '%a%' AND Gender = 'M';
Output
+--------+--------+ | RollNo | Name | +--------+--------+ | 101 | Aarav | | 103 | Kabir | | 105 | Ishaan | | 107 | Rohan | +--------+--------+
Q5
SQL
SELECT DISTINCT Class FROM STUDENT ORDER BY Class;
Output
+-------+ | Class | +-------+ | XI-A | | XI-B | | XI-C | +-------+
Q6
SQL
SELECT Name, Marks, Marks * 0.9 AS Scaled FROM STUDENT WHERE Marks >= 75;
Output
+-------+-------+--------+ | Name | Marks | Scaled | +-------+-------+--------+ | Aarav | 88.5 | 79.65 | | Sana | 92.0 | 82.80 | | Diya | 75.0 | 67.50 | | Meher | 81.0 | 72.90 | +-------+-------+--------+
Q7
SQL
SELECT * FROM STUDENT WHERE City IS NULL OR DOB IS NULL;
Output
+--------+-------+--------+------------+-------+-------+------+ | RollNo | Name | Gender | DOB | Class | Marks | City | +--------+-------+--------+------------+-------+-------+------+ | 106 | Meher | F | 2009-10-05 | XI-C | 81.0 | NULL | | 107 | Rohan | M | NULL | XI-A | 39.5 | NULL | +--------+-------+--------+------------+-------+-------+------+
Q8
SQL
SELECT Name FROM STUDENT WHERE Gender = 'F' AND (City = 'Pune' OR Class = 'XI-C');
Output
+-------+ | Name | +-------+ | Diya | | Meher | +-------+
Q9
SQL
UPDATE STUDENT SET Marks = 60 WHERE RollNo = 105; SELECT RollNo, Name, Marks FROM STUDENT WHERE RollNo = 105;
Output
+--------+--------+-------+ | RollNo | Name | Marks | +--------+--------+-------+ | 105 | Ishaan | 60.0 | +--------+--------+-------+
Q10
SQL
DELETE FROM STUDENT WHERE City IS NULL AND Marks < 50; SELECT RollNo, Name, City FROM STUDENT;
Output
+--------+--------+---------+ | RollNo | Name | City | +--------+--------+---------+ | 101 | Aarav | Jaipur | | 102 | Sana | Lucknow | | 103 | Kabir | Jaipur | | 104 | Diya | Pune | | 105 | Ishaan | Delhi | | 106 | Meher | NULL | +--------+--------+---------+
Frequently Asked Questions
How many programs should the Class 11 IP practical file have?
Follow your teacher’s list. Usually around 15–20 Python programs and 10–15 SQL queries are expected, covering lists, dictionaries, NumPy and basic SQL commands.
Can I copy these outputs into my file?
Run each program yourself first. Your output should match; if it does not, find out why — that is the best practice for the viva.
