Que.
1.Define a dictionary in python show how to create a dictionary. 2marks
2.compare tuple/list/set/dictionary in python. 3marks
Or
2. explain mathematical operations on set. 3marks
3.develope a programme to check if a key is exist in a dictionary? 5marks
Or
3.make use of add( ),update( ),and extend( ),discord( ) method of set. 5marks
Que.2
1.define module in python, how to create module? 2marks
2.explain how to import a module in python program with example. 3marks
Or
2.explain os module. 3marks
3.develope a program to create package and import package. 6marks
Que.3
1.define exception, show how it is different from error. 2marks
2.explain the use of “try-except” block with syntax and example. 2marks
3.develop a python program to handle division by zero exception. 5marks
Or
3.develop a python program to implement user-defined exception. 5marks
Que.1
1. Define a dictionary in python show how to create a dictionary. (2 Marks)
A dictionary in Python is an unordered collection of data values used to store data values like
a map. It consists of key-value pairs where each key is unique.
Example:
python
CopyEdit
my_dict = {
"name": "Alice",
"age": 25,
"city": "New York"
}
Here, "name", "age", and "city" are keys, and their corresponding values are "Alice", 25,
and "New York".
2. Compare tuple/list/set/dictionary in python. (3 Marks)
Feature Tuple List Set Dictionary
Syntax () [] {} {key: value}
Ordered Yes Yes No Yes (as of Python 3.7)
Mutable No Yes Yes Yes
Duplicate Allowed Allowed Not allowed Keys not duplicated
Access Index-based Index-based Not indexable Key-based access
Each structure serves a specific use case in data handling.
OR - Explain mathematical operations on set. (3 Marks)
Python sets support various mathematical operations like union, intersection, difference, and
symmetric difference:
python
CopyEdit
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # Union: {1, 2, 3, 4, 5}
print(A & B) # Intersection: {3}
print(A - B) # Difference: {1, 2}
print(A ^ B) # Symmetric Difference: {1, 2, 4, 5}
These operations help in solving problems involving group memberships or filtering data
efficiently.
3. Develop a program to check if a key exists in a dictionary? (5 Marks)
python
CopyEdit
# Python program to check if a key exists in a dictionary
my_dict = {
"name": "John",
"age": 30,
"city": "Delhi"
}
key_to_check = "age"
if key_to_check in my_dict:
print(f"Key '{key_to_check}' exists with value:
{my_dict[key_to_check]}")
else:
print(f"Key '{key_to_check}' does not exist.")
Output:
sql
CopyEdit
Key 'age' exists with value: 30
The in operator checks if the key is part of the dictionary.
OR - Make use of add( ), update( ), and extend( ), discard( ) method of set. (5 Marks)
Note: extend() is not used with sets, only add(), update(), and discard() apply.
python
CopyEdit
# Set operations
A = {1, 2, 3}
A.add(4) # Adds a single element
A.update([5, 6]) # Adds multiple elements from a list
A.discard(2) # Removes element if present
print("Updated Set:", A)
Output:
css
CopyEdit
Updated Set: {1, 3, 4, 5, 6}
Here, add() inserts an element, update() merges iterable elements, and discard() removes
an element safely.
Que.2
1. Define module in python, how to create module? (2 Marks)
A module in Python is a file containing Python code such as functions, classes, or variables
which can be reused in other programs.
Creating a module: Create a Python file, e.g., my_module.py:
python
CopyEdit
def greet(name):
return f"Hello, {name}"
This file can be imported in other Python files to use the greet() function.
2. Explain how to import a module in python program with example. (3 Marks)
Modules can be imported using the import keyword in Python.
Example:
python
CopyEdit
import math
print(math.sqrt(16)) # Output: 4.0
You can also import specific functions:
python
CopyEdit
from math import pow
print(pow(2, 3)) # Output: 8.0
Modules help reuse code and maintain program structure.
OR - Explain os module. (3 Marks)
The os module in Python provides functions to interact with the operating system.
Common functions:
python
CopyEdit
import os
print(os.name) # Returns OS name
print(os.getcwd()) # Gets current directory
os.mkdir("new_folder") # Creates new directory
It’s useful for file handling, directory navigation, and process management.
3. Develop a program to create package and import package. (6 Marks)
Step 1: Create a Package Folder Structure
markdown
CopyEdit
my_package/
__init__.py
module1.py
module1.py
python
CopyEdit
def add(a, b):
return a + b
Main Program (main.py):
python
CopyEdit
from my_package import module1
result = module1.add(10, 5)
print("Addition:", result)
Output:
makefile
CopyEdit
Addition: 15
Que.3
1. Define exception, show how it is different from error. (2 Marks)
An exception in Python is an event that occurs during program execution and disrupts the normal
flow of instructions. It is usually caused by logical errors.
Difference from error:
Errors are serious problems that a program cannot handle (e.g., syntax errors).
Exceptions can be handled using try-except blocks.
Example: ZeroDivisionError is an exception, while IndentationError is an error.
2. Explain the use of “try-except” block with syntax and example. (2 Marks)
The try-except block is used to catch and handle exceptions in Python.
Syntax:
python
CopyEdit
try:
# Code that may raise an exception
except ExceptionType:
# Code to handle the exception
Example:
python
CopyEdit
try:
x = 5 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
3. Develop a Python program to handle division by zero exception. (5 Marks)
python
CopyEdit
# Division by zero exception handling
try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
result = a / b
print("Result:", result)
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Invalid input.")
Output Example:
yaml
CopyEdit
Enter numerator: 10
Enter denominator: 0
Error: Cannot divide by zero.
This prevents the program from crashing when a zero denominator is entered.
OR - Develop a Python program to implement user-defined exception. (5 Marks)
python
CopyEdit
# User-defined exception example
class AgeTooSmallError(Exception):
pass
age = int(input("Enter your age: "))
try:
if age < 18:
raise AgeTooSmallError
else:
print("You are eligible to vote.")
except AgeTooSmallError:
print("Error: Age is too small to vote.")
Output Example:
vbnet
CopyEdit
Enter your age: 16
Error: Age is too small to vote.
User-defined exceptions are created by extending the Exception class and used to handle custom
error conditions.