What is the difference between a module and a package in Python?
Packages help organize code logically and make it reusable and easier to manage.
What does the import statement do in Python?
It allows you to reuse code from other files or libraries
This promotes the use of existing solutions and helps build modular, scalable programs.
What is the purpose of the __init__.py file in a package?
It initializes a package and/or its sub-packages
The file may be empty but must not be absent for the directory to be recognized as a package.
What does the dir() function do?
Shows a list of the entities contained inside an imported module
Example: dir(os) will list all attributes, functions, and classes defined in the os module.
What is the role of the sys.path variable?
Controls where Python looks for modules when using an import statement
It determines the search path Python follows to locate the files that define the modules you’re importing.
List the functions provided by the math module.
These functions perform various mathematical operations.
What does the math.ceil(x) function do?
Rounds up to the nearest integer (toward +∞)
Example: math.ceil(3.1) returns 4.
What does the random.random() function return?
A random float in the range (0.0, 1.0)
Example output: 0.5738…
What is the purpose of the random.seed(x) function?
Initializes the random number generator for reproducibility
Always use seed() before random() if you want repeatable output.
What does the platform.platform() function return?
A single string describing the full platform
Example output: ‘Windows-10-10.0.19041-SP0’.
What is the significance of the __name__ variable in Python?
Indicates if a file is run directly or imported as a module
If run directly, __name__ is set to __main__; if imported, it is set to the file’s name.
True or false: The finally branch of the try statement is executed only if an exception occurs.
FALSE
The finally branch is always executed, regardless of whether an exception occurred.
What are abstract expressions in the context of Python exceptions?
Base classes in the exception hierarchy not meant to be raised directly
They provide categorization or grouping of related errors.
List three examples of concrete built-in Python exceptions.
These represent actual error conditions that can occur.
What does the except (e1, e2) syntax do?
Catches multiple types of exceptions in one block
e1 and e2 must be exception types like ValueError or TypeError.
What is the purpose of the __pycache__ directory?
Stores semi-compiled .pyc files for imported modules
This improves performance by avoiding recompilation on subsequent imports.
What is a namespace in Python?
A mapping between names and objects (like variables or functions)
Different types include local, global, nonlocal, and built-in namespaces.
Fill in the blank: A module is designed to couple together some related entities such as functions, variables, and _______.
constants
This helps in organizing code logically.
Name some concrete built-in Python exceptions.
These exceptions are part of Python’s built-in exception hierarchy.
What is the purpose of the raise statement in Python?
The raise statement can be used to trigger exceptions intentionally.
True or false: Assertions are used for handling runtime errors.
FALSE
Assertions are primarily used for debugging and can be disabled when Python is run with the -O flag.
What are event classes in Python exceptions?
Event classes include exceptions like KeyboardInterrupt and SystemExit.
What does the syntax except E as e: do?
Catches a specific exception and assigns it a name
This allows you to access the details of the exception after catching it.
What is the args property in Python exceptions?
A tuple containing the arguments passed to the exception
You can access individual arguments using ex.args[index].