PWP Ut
PWP Ut
Ans:
In Python, file modes determine how a file is opened and what operations can be performed on it. The four
common file modes are:
These modes can be combined (e.g., 'rb', 'wb', 'r+') to support different read/write/binary
combinations.
Ans:
The from...import statement in Python is used to import specific attributes (like functions, classes, or
variables) from a module.
This is helpful when you don't want to import the whole module or when you want to directly use an item
without prefixing it with the module name.
Syntax:
Example:
Classes are the foundation of object-oriented programming in Python and support concepts like inheritance,
polymorphism, and encapsulation.
Syntax:
class Student:
def __init__(self, name):
self.name = name
def display(self):
print("Student name:", self.name)
Here, Student is a class, and __init__() is the constructor method used to initialize the object.
Ans:
A package is a collection of related Python modules grouped together in a directory. It helps in organizing
large programs and makes code modular and manageable.
A package must contain an __init__.py file (can be empty), which tells Python that the directory should
be treated as a package.
myapp/
├── __init__.py
├── student.py
└── teacher.py
Ans:
A user-defined function is a block of reusable code created by the programmer to perform a specific task.
Functions help reduce redundancy and break large programs into smaller, manageable parts. They improve
code clarity and reuse.
Syntax:
def function_name(parameters):
# function body
return value
Example:
User-defined functions can take arguments, return values, and even call other functions.
4 MARKS
Ans:
A module in Python is a file containing Python definitions and statements. The filename of the module should have a
.py extension. Modules are used to break large programs into smaller, manageable, and organized files.
Modules improve code reusability, maintainability, and modular programming by allowing developers to reuse
functions, classes, and variables across multiple programs.
Creating a Module:
python
CopyEdit
# mymodule.py
def greet(name):
return f"Hello, {name}!"
Using a Module:
You can import the module in another Python program using the import or from...import statement.
python
CopyEdit
import mymodule
print(mymodule.greet("Arpit"))
Or:
python
CopyEdit
from mymodule import greet
print(greet("Arpit"))
Advantages of Using Modules:
Python also provides a large number of built-in modules like math, os, random, datetime, etc.
Ans:
The reduce() function belongs to the functools module and is used to apply a specified function cumulatively
to the elements of a sequence, reducing it to a single value.
It performs operations like summing up a list of numbers, finding the product, etc., in a functional programming
style.
Syntax:
python
CopyEdit
from functools import reduce
reduce(function, sequence)
Here, the function must take two arguments. The reduce() function applies this function cumulatively to the
items of the sequence.
Example:
python
CopyEdit
from functools import reduce
numbers = [2, 3, 4, 5]
result = reduce(multiply, numbers)
print(result) # Output: 120
Working:
Step-by-step:
multiply(2, 3) → 6
multiply(6, 4) → 24
multiply(24, 5) → 120
Use Cases:
It’s commonly used in data processing pipelines and when dealing with collections of data.
Q3. Explain open() and close() methods for opening and closing a file.
Ans:
Python provides built-in functions for file handling, which allows reading, writing, and modifying files on the system.
open() Function:
Syntax:
python
CopyEdit
file = open(filename, mode)
Common Modes:
Example:
python
CopyEdit
f = open("data.txt", "r")
close() Function:
Closes the file and frees up system resources. Failing to close a file can lead to memory leaks or data loss.
Syntax:
python
CopyEdit
file.close()
Example:
python
CopyEdit
f = open("data.txt", "r")
data = f.read()
f.close()
Best Practice:
Use the with statement to handle files, which automatically closes the file:
python
CopyEdit
with open("data.txt", "r") as f:
content = f.read()
Q4. Explain how to create a user-defined exception and raise it manually with an example.
Ans:
Python allows users to create custom (user-defined) exceptions to handle specific errors in a program that are not
covered by built-in exceptions.
Example:
python
CopyEdit
class AgeTooSmallError(Exception):
"""Exception raised when age is less than 18"""
pass
Output:
This helps programmers to control program flow and prevent invalid operations.
Ans: