Python Vol 1 Technical Publication
Python Vol 1 Technical Publication
Module: A module is a file containing Python definitions and statements. It helps in organizing code logically and can be
imported into other Python scripts using the import statement.
Example: import math
Package: A package is a collection of modules organized in directories containing a special __init__.py file. It allows
hierarchical structuring of the module namespace.
2) Define Exception.
An exception is a runtime error that occurs during the execution of a program. It disrupts the normal flow of the program
and can be handled using try-except blocks. Handling exceptions prevents program crashes and allows for graceful
error messages.
Example:
try:
a = 10 / 0
except ZeroDivisionError:
print('Cannot divide by zero')
def function_name(parameters):
'''optional docstring'''
# function body
return [expression]
Example:
def add(a, b):
Python Concepts - 4 Marks Answers
return a + b
Example:
def show(name, age=18):
print(name, age)
Use: It allows Python to determine which object name refers to at any point in the program.
Example:
import math
print(math.sqrt(16))
ii) Exception: Exceptions occur during execution and can be handled. Examples include FileNotFoundError,
ZeroDivisionError. They are caught using try-except blocks.
Example:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)