0% found this document useful (0 votes)
1 views3 pages

Python Modules Lab

The document explains the concept of modules in Python, highlighting their importance for code organization, reusability, and maintainability. It covers built-in and user-defined modules, along with syntax for importing modules and examples demonstrating their usage. The experiment successfully illustrates how to implement modules in Python using Ubuntu, including the use of import statements and aliases.

Uploaded by

gajulajhansi01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Python Modules Lab

The document explains the concept of modules in Python, highlighting their importance for code organization, reusability, and maintainability. It covers built-in and user-defined modules, along with syntax for importing modules and examples demonstrating their usage. The experiment successfully illustrates how to implement modules in Python using Ubuntu, including the use of import statements and aliases.

Uploaded by

gajulajhansi01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment: Modules in Python

Aim:
To understand the concept of modules in Python and how to use them to organize and reuse
code efficiently.

Theory
A module in Python is a file that contains Python definitions, functions, classes, or variables
that can be imported and used in other programs. It helps in code reusability, organization,
and maintainability.

Need for Modules:


- To divide large programs into smaller manageable files.
- To reuse code across multiple programs.
- To maintain clarity and avoid redundancy.

Types of Modules:
1. Built-in Modules: Predefined modules provided by Python (e.g., math, datetime, os,
random).

2. User-defined Modules: Created by the programmer to organize custom functions and


logic.

Syntax:
Importing Modules:
import module_name

Importing Specific Function from a Module:


from module_name import function_name

Renaming a Module (Alias):


import module_name as alias_name

Example Programs
Program 1: Using Built-in Module
File Name: math_module.py

import math

num = 25
print("Square root of", num, "is:", [Link](num))
print("Value of pi:", [Link])
print("5 raised to 3 is:", [Link](5, 3))
To Run in Ubuntu:
python3 math_module.py

Expected Output:
Square root of 25 is: 5.0
Value of pi: 3.141592653589793
5 raised to 3 is: 125.0

Program 2: Importing Specific Function


File Name: import_specific.py

from math import sqrt, pi

print("Square root of 36 is:", sqrt(36))


print("Value of pi is:", pi)

To Run:
python3 import_specific.py

Expected Output:
Square root of 36 is: 6.0
Value of pi is: 3.141592653589793

Program 3: Using Alias Name


File Name: alias_example.py

import math as m

print("Cosine of 0:", [Link](0))


print("Logarithm of 10:", [Link](10))

To Run:
python3 alias_example.py

Expected Output:
Cosine of 0: 1.0
Logarithm of 10: 2.302585092994046

Program 4: User-defined Module


Step 1 – Create Module File: my_module.py

def greet(name):
return f"Hello, {name}!"
def square(x):
return x * x

Step 2 – Use the Module: use_my_module.py

import my_module

print(my_module.greet("Alice"))
print("Square of 6 is:", my_module.square(6))

To Run:
python3 use_my_module.py

Expected Output:
Hello, Alice!
Square of 6 is: 36

Result:
Successfully understood and implemented built-in and user-defined modules in Python
using Ubuntu. Learned to use import statements, aliases, and modularize programs
effectively.

You might also like