Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]
AD.PYTHON W2023
Q.1 (a) What is Dictionary? Explain with example. (3 Marks)
A dictionary in Python is an unordered collection of data values used to store data in
key:value pairs. It is mutable and does not allow duplicate keys.
Example:
python
CopyEdit
student = {"name": "Arjun", "age": 20, "course": "Python"}
print(student["name"]) # Output: Arjun
Q.1 (b) Explain Tuple Built-in functions and methods. (4 Marks)
Tuples are immutable, and some commonly used built-in functions/methods are:
1. len(tuple) – returns the length of the tuple.
2. max(tuple) – returns the maximum value.
3. min(tuple) – returns the minimum value.
4. sum(tuple) – returns the sum of numeric elements.
5. tuple.count(x) – returns the count of element x.
6. tuple.index(x) – returns the first index of element x.
Example:
python
CopyEdit
t = (1, 2, 3, 2)
print(t.count(2)) # Output: 2
Q.1 (c) Write a python program to demonstrate set operations. (7 Marks)
python
CopyEdit
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print("Union:", A | B)
print("Intersection:", A & B)
print("Difference A-B:", A - B)
print("Symmetric Difference:", A ^ B)
Q.2 (a) Distinguish between Tuple and List in Python. (3 Marks)
1. Mutability: List is mutable, Tuple is immutable.
2. Syntax: List uses [], Tuple uses ().
3. Functions: More functions available for lists.
4. Performance: Tuples are faster than lists.
1|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]
5. Use case: Lists for dynamic data, Tuples for fixed data.
Q.2 (b) What is the dir() function in python? Explain with example. (4 Marks)
dir()
is a built-in function that returns all valid attributes of the object.
Example:
python
CopyEdit
print(dir([])) # returns all list methods like append, pop, etc.
Q.2 (c) Write a program to define a module to find the area and
circumference of a circle. Import module to another program. (7 Marks)
circle_module.py
python
CopyEdit
import math
def area(r):
return math.pi * r * r
def circumference(r):
return 2 * math.pi * r
main.py
python
CopyEdit
import circle_module
print("Area:", circle_module.area(5))
print("Circumference:", circle_module.circumference(5))
Q.3 (a) Describe Runtime Error and Syntax Error. Explain with example. (3
Marks)
Syntax Error: Error due to incorrect syntax.
Example: if True print("Hello")
Runtime Error: Occurs during program execution.
Example: print(10 / 0) → ZeroDivisionError
Q.3 (b) What is Exception handling in Python? Explain with example. (4
Marks)
2|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]
Exception handling is managing runtime errors using try-except blocks.
Example:
python
CopyEdit
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
Q.3 (c) Create a function for division of two numbers, raise error if non-
integer or division by zero. (7 Marks)
python
CopyEdit
def divide(a, b):
if not (isinstance(a, int) and isinstance(b, int)):
raise TypeError("Only integers are allowed")
if b == 0:
raise ZeroDivisionError("Division by zero not allowed")
return a / b
try:
result = divide(10, 0)
print("Result:", result)
except Exception as e:
print("Error:", e)
Q.4 (a) Write five points on difference between Text File and Binary File. (3
Marks)
1. Text files store characters; binary files store bytes.
2. Text files are human-readable; binary are not.
3. Use mode t for text, b for binary.
4. Encoding matters in text; not in binary.
5. Text uses read(), binary uses readbytes().
Q.4 (b) Program to read file and separate uppercase and lowercase chars into
two files. (4 Marks)
python
CopyEdit
with open("input.txt", "r") as f:
data = f.read()
with open("upper.txt", "w") as u, open("lower.txt", "w") as l:
for char in data:
if char.isupper():
u.write(char)
elif char.islower():
l.write(char)
3|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]
Q.4 (c) Describe dump() and load() method. Explain with example. (7 Marks)
dump()and load() are used with pickle module to serialize and deserialize Python objects.
Example:
python
CopyEdit
import pickle
data = {"name": "Rahul", "age": 25}
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
with open("data.pkl", "rb") as f:
loaded = pickle.load(f)
print(loaded)
Q.5 (a) Draw Circle and rectangle shapes using Turtle and fill them with red
color. (3 Marks)
python
CopyEdit
import turtle
t = turtle.Turtle()
t.fillcolor("red")
t.begin_fill()
t.circle(50)
t.end_fill()
t.penup()
t.goto(100, 0)
t.pendown()
t.begin_fill()
for _ in range(2):
t.forward(100)
t.right(90)
t.forward(50)
t.right(90)
t.end_fill()
Q.5 (b) Explain inbuilt methods to change Turtle direction. (4 Marks)
1. right(angle) – turns turtle clockwise.
2. left(angle) – turns turtle counter-clockwise.
3. setheading(angle) – sets the direction to a specific angle.
4. towards(x, y) – points towards a position.
5. goto(x, y) – moves turtle in specific direction.
4|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]
Q.5 (c) Write a python program to draw a rainbow using Turtle. (7 Marks)
python
CopyEdit
import turtle
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
t = turtle.Turtle()
t.width(10)
t.penup()
t.goto(0, -200)
t.pendown()
for color in colors:
t.color(color)
t.circle(200)
t.left(10)
5|Page