0% found this document useful (0 votes)
17 views8 pages

Python 2025

The document provides solutions to various Python programming questions, covering topics such as Sets, Dictionaries, Lists, Tuples, Packages, Modules, Error handling, File operations, and Turtle graphics. It includes definitions, examples, and code snippets for each topic, illustrating key concepts and functionalities in Python. The content is structured in a question-answer format, suitable for exam preparation.

Uploaded by

rehanboiizzz
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)
17 views8 pages

Python 2025

The document provides solutions to various Python programming questions, covering topics such as Sets, Dictionaries, Lists, Tuples, Packages, Modules, Error handling, File operations, and Turtle graphics. It includes definitions, examples, and code snippets for each topic, illustrating key concepts and functionalities in Python. The content is structured in a question-answer format, suitable for exam preparation.

Uploaded by

rehanboiizzz
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
You are on page 1/ 8

Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

AD.PYTHON W2025

Q.1

(1) Give the difference between Set and Dictionary in Python. (3 Marks)

1. A Set is a collection of unordered, unindexed unique elements.


2. A Dictionary is a collection of key-value pairs.
3. Sets have only values, but Dictionaries have keys and values.
4. Set syntax: {1, 2, 3}; Dictionary syntax: {"a": 1, "b": 2}
5. Sets do not support key-based access; Dictionaries do.

(2) Explain List in Python with example. (4 Marks)


A list is an ordered, mutable collection of elements in Python. Lists can store items of
different data types.
Example:

python
CopyEdit
my_list = [10, "apple", 3.14]
print(my_list[1]) # Output: apple
my_list.append("banana")
print(my_list)

(3) What is Tuple in Python? Write a Python program to swap two tuple values. (7
Marks)
A tuple is an immutable sequence in Python used to store multiple items. Once created, tuple
values cannot be changed.
Example:

python
CopyEdit
a = (1, 2)
b = (3, 4)

# Swapping tuples
a, b = b, a

print("a:", a) # Output: a: (3, 4)


print("b:", b) # Output: b: (1, 2)

(OR)
What is Dictionary in Python? Write a Python program to traverse a dictionary using
loop. (7 Marks)
A dictionary is a built-in Python collection that stores data in key-value pairs.
Example:

1|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

python
CopyEdit
student = {"name": "Amit", "age": 21, "course": "Python"}

for key, value in student.items():


print(key, ":", value)

Q.2

(1) What is Package? List out advantages of using Package. (3 Marks)


A package is a directory containing multiple related Python modules and an __init__.py
file.
Advantages:

1. Organizes code better


2. Promotes code reuse
3. Makes code more manageable
4. Supports modularity
5. Helps in large project development

(2) Explain any two package import methods with example. (4 Marks)

1. import package.module

python
CopyEdit
import math
print(math.sqrt(16))

2. from package import module

python
CopyEdit
from math import pow
print(pow(2, 3))

(3) Explain about intra-package reference with example. (5 Marks)


Intra-package reference means a module inside a package can import another module in the
same package using relative import.
Example:
In my_package/module2.py:

python
CopyEdit
from . import module1
module1.some_function()

2|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

(OR)

(4) What is Module? List out advantages of using Module. (3 Marks)


A module is a .py file that contains Python functions, classes, or variables.
Advantages:

1. Code reuse
2. Easier testing and debugging
3. Logical structure
4. Better project organization
5. Efficient teamwork

(5) Explain any two module import methods with example. (4 Marks)

1. Full module import

python
CopyEdit
import math
print(math.sqrt(9))

2. Specific function import

python
CopyEdit
from math import sqrt
print(sqrt(9))

(6) Write a program to define a module to find the area and circumference of a circle.
(i) import the module to another program
(ii) import a specific function from a module to another program. (7 Marks)

circle_module.py

python
CopyEdit
import math

def area(radius):
return math.pi * radius * radius

def circumference(radius):
return 2 * math.pi * radius

main1.py (full import)

python
CopyEdit
import circle_module
print("Area:", circle_module.area(7))
print("Circumference:", circle_module.circumference(7))

3|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

main2.py (specific import)

python
CopyEdit
from circle_module import area, circumference
print("Area:", area(7))
print("Circumference:", circumference(7))

Q.3

(1) Explain the types of error in Python. (3 Marks)

1. Syntax Error: Caused due to wrong syntax (e.g., missing :).


2. Runtime Error: Occurs while program is running (e.g., division by zero).
3. Logical Error: Program runs, but output is incorrect due to logic mistake.

(2) Explain user-defined exception using raise statement with example. (4 Marks)
Custom exceptions can be created using raise keyword.
Example:

python
CopyEdit
def check(age):
if age < 18:
raise Exception("You are underage!")
check(16)

(3) Explain try-except-finally clause with example. (7 Marks)


try block checks error, except handles it, finally executes always.
Example:

python
CopyEdit
try:
x = 10 / 0
except ZeroDivisionError:
print("Error: Division by zero")
finally:
print("This is finally block")

(OR)

(4) What is built-in exception? List out any two with their meaning. (3 Marks)
Built-in exceptions are predefined error types in Python.

1. ZeroDivisionError: Dividing number by zero

4|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

2. ValueError: Wrong value passed to a function

(5) Explain try-except clause with example. (4 Marks)


Handles runtime errors gracefully.
Example:

python
CopyEdit
try:
a = int("abc")
except ValueError:
print("Invalid input")

(6) Write a program to catch Divide by zero Exception with finally clause. (7 Marks)

python
CopyEdit
try:
a = int(input("Enter a number: "))
print(100 / a)
except ZeroDivisionError:
print("You cannot divide by zero")
finally:
print("Execution finished")

Q.4

(1) Define: File, Binary File, Text File. (3 Marks)

 File: Storage medium to store data permanently.


 Text File: Stores plain readable text.
 Binary File: Stores data in binary format (0s and 1s), not human-readable.

(2) Explain write() and writelines() functions with example. (4 Marks)

 write() writes a single string.


 writelines() writes multiple lines (as a list).
Example:

python
CopyEdit
f = open("demo.txt", "w")
f.write("Hello\n")
f.writelines(["Line1\n", "Line2\n"])
f.close()

5|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

(3) Explain tell() and seek() function with example. (5 Marks)

 tell()returns current position of file pointer.


 seek()changes the file pointer position.
Example:

python
CopyEdit
f = open("demo.txt", "r")
print(f.tell())
f.seek(5)
print(f.read())
f.close()

(OR)

(4) What is Absolute and Relative file path? (3 Marks)

 Absolute Path: Complete path from root directory (e.g., C:/Users/file.txt)


 Relative Path: Path relative to the current directory (e.g., ./file.txt)

(5) Explain about various mode to open binary and text file. (4 Marks)

 'r': read (text)


 'w': write (text)
 'rb': read binary
 'wb': write binary
 'a': append

(6) Write a Python program to write student's subject record like branch name,
semester, subject code and subject name in the binary file. (5 Marks)

python
CopyEdit
import pickle

record = {
"branch": "CE",
"semester": 2,
"subject_code": 4321602,
"subject": "Python"
}

with open("record.bin", "wb") as file:


pickle.dump(record, file)

6|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

Q.5

(1) Define: GUI, CLI. (3 Marks)

 GUI (Graphical User Interface): Uses images, icons, buttons for interaction (e.g.,
Windows).
 CLI (Command Line Interface): Uses typed commands (e.g., Terminal, CMD).

(2) Write a Python program to draw square shape using for and while loop using
Turtle. (4 Marks)

python
CopyEdit
import turtle
t = turtle.Turtle()

# Using for loop


for _ in range(4):
t.forward(100)
t.right(90)

# Using while loop


count = 0
while count < 4:
t.forward(100)
t.right(90)
count += 1

(3) Write a Python program to draw a chessboard using Turtle. (5 Marks)

python
CopyEdit
import turtle
t = turtle.Turtle()
t.speed(0)

square_size = 30
for row in range(8):
for col in range(8):
x = col * square_size
y = row * square_size
if (row + col) % 2 == 0:
t.penup()
t.goto(x, -y)
t.pendown()
for _ in range(4):
t.forward(square_size)
t.right(90)

(OR)

7|Page
Made by ABDU VORAJEE [PYTHON 2024&2023 PAPER SOLUTION]

(4) How many types of shapes in Turtle? Explain any one shape with example. (3
Marks)
There are 6 default shapes in turtle: 'arrow', 'turtle', 'circle', 'square', 'triangle',
'classic'.
Example:

python
CopyEdit
import turtle
turtle.shape("turtle")

(5) Explain about four basic methods of Turtle module. (4 Marks)

1. forward(x): Moves turtle forward by x units


2. backward(x): Moves turtle backward
3. right(angle): Turns turtle clockwise
4. left(angle): Turns turtle counter-clockwise

(6) Write a Python program to draw square, rectangle, and circle using Turtle. (5
Marks)

python
CopyEdit
import turtle
t = turtle.Turtle()

# Square
for _ in range(4):
t.forward(100)
t.right(90)

# Rectangle
t.penup()
t.goto(150, 0)
t.pendown()
for _ in range(2):
t.forward(120)
t.right(90)
t.forward(60)
t.right(90)

# Circle
t.penup()
t.goto(-150, 0)
t.pendown()
t.circle(50)

8|Page

You might also like