🌟 Diplomatech Academy 🌟
Manual Solutions and outputs
Python Programming Course Code: 314004
-Mohade Sir©
Practical No. 16: Write a python program to create and use a
user defined module for a given problem
ir
eS
ad
oh
IX Conclusion:
Ans
M
We have successfully created and used a user-defined module in Python. This
approach enhances code reusability, modularity, and maintainability by
organizing related functions into separate files. Importing modules helps in
keeping the main code clean and efficient while improving readability. This
concept is widely used in large-scale software development to manage complex
projects effectively.
X Practical related questions
1.State different types of python modules.
Ans.
Types of Python Modules:
1. Built-in Modules – Pre-installed modules in Python (e.g., math, os, sys).
2. User-Defined Modules – Custom modules created by users to reuse code.
ir
3. Third-Party Modules – External modules installed via pip (e.g., numpy,
pandas).
eS
4. Package Modules – A collection of modules organized in directories using
__init__.py.
2.Explain how to import multiple modules in python script with example.
Ans.
ad
Importing Multiple Modules in Python
Theory Points:
oh
1. Multiple modules can be imported separately using individual import statements.
2. Modules can be imported in a single line using commas (e.g., import
math, os).
3. Specific functions from modules can be imported using from module
M
import function.
4. Wildcard * can import all functions, but it is not recommended for large
modules due to potential conflicts.
5. Aliases (as keyword) can be used to shorten module names for
convenience.
Example:
import math, os # Importing multiple modules in one line
print(math.sqrt(16)) # Output: 4.0
print(os.getcwd()) # Output: Current working directory
3.State advantages of modules in Python.
Ans.
Advantages of Modules in Python
ir
1. Code Reusability – Modules allow code to be written once and reused in
multiple programs.
2. Better Organization – Large programs can be split into smaller,
eS
manageable modules.
3. Namespace Management – Helps avoid naming conflicts by organizing
functions and variables.
4. Improved Maintainability – Easier to update and debug specific parts of
the code.
ad
5. Built-in Functionality – Many useful modules (like math, os, random)
are pre-installed in Python.
oh
4.Write a Python program to create a user defined module that will ask your
college name and will
display the name of the college.
Ans.
M
Step 1: Create a module file (college_module.py)
# college_module.py
def ask_college_name():
college_name = input("Enter your college name: ")
return college_name
def display_college_name(college):
print(f"Your college name is: {college}")
Step 2: Create a main program (main.py) to use the module
# main.py
import college_module # Importing the user-defined module
ir
# Ask for college name
college = college_module.ask_college_name()
eS
# Display the college name
college_module.display_college_name(college)
How It Works?
ad
1. The college_module.py contains functions to ask and display the
college name.
2. The main.py imports college_module and calls its functions to take
input and show output.
oh
.5.Write a Python program to define a module to find Fibonacci Numbers and
import the module to
M
another program.
Ans.
Here’s how you can create and use a user-defined module in Python to find
Fibonacci numbers.
Step 1: Create a module file (fibonacci_module.py)
# fibonacci_module.py
def fibonacci(n):
fib_series = [0, 1]
for i in range(2, n):
next_num = fib_series[-1] + fib_series[-2]
ir
fib_series.append(next_num)
eS
return fib_series[:n]
Step 2: Create a main program (main.py) to use the module
ad
# main.py
oh
import fibonacci_module # Importing the user-defined module
num = int(input("Enter the number of Fibonacci terms: "))
M
fib_series = fibonacci_module.fibonacci(num)
print("Fibonacci Series:", fib_series)
M
oh
ad
eS
ir