0% found this document useful (0 votes)
5 views5 pages

Python Modules

The document explains Python modules, which are files containing Python code that define variables, functions, and classes, aimed at organizing and reusing code. It details the creation of custom modules, the use of built-in modules like math, and how to import and utilize them in scripts. Additionally, it describes methods to explore module functionalities using dir() and help() functions.

Uploaded by

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

Python Modules

The document explains Python modules, which are files containing Python code that define variables, functions, and classes, aimed at organizing and reusing code. It details the creation of custom modules, the use of built-in modules like math, and how to import and utilize them in scripts. Additionally, it describes methods to explore module functionalities using dir() and help() functions.

Uploaded by

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

Python Modules

A module in Python is a file containing Python code that defines variables,


functions, and classes. These files typically have a .py extension. The primary
purpose of modules is to organize code into reusable and logically
structured units.
Modules serve several key purposes:
1. Code Organization: Modules help organize code by grouping related
functionality together. This enhances the maintainability and readability
of large codebases.
2. Code Reusability: Functions, variables, and classes defined in a module
can be reused in other Python scripts or modules. This promotes a
modular and efficient coding style.
3. Encapsulation: Modules allow encapsulation of related code, hiding the
internal details of the implementation. This supports the principle of
information hiding and abstraction.
4. Importing: Modules are brought into a Python script using the import
statement. This statement makes the names defined in the module
accessible in the importing script, providing a way to use external code
seamlessly.

In Python we can define modules in 3 ways:


1. Python enables the direct creation of modules.
2. Built-in modules, are inherently included in the interpreter.
3. Similar to the re (regular expression) module, a module can be initially
authored in the C programming language and dynamically inserted at
runtime.

1. Creation of Python modules: Let’s create a module named mymodule and


import it.
If you're using Jupyter Notebook and want to save a file with a .py
extension, you can use the %%writefile magic command in a code cell.
1. Create a New Code Cell: In a Jupyter Notebook, create a new code cell.

1|Page
2. Use %%writefile Magic Command: In the code cell, use the %%writefile
magic command followed by the file name with the .py extension.
Then, write the Python code below.

3. Run the cell by clicking on it and then either clicking the "Run" button in
the toolbar or using the keyboard shortcut (usually Shift + Enter).

%%writefile [Link]

def greet(name):
return "Hello, " + name + "!"

def add_numbers(a, b):


return a + b
Output
Writing [Link]

This command will save the content of the cell to a file named
[Link] in the same directory as your Jupyter Notebook. You can
also choose the location where you want to save the file.

Now, you can import and use this module in other cells or notebooks using
the import keyword.

import mymodule
print([Link]("Alice"))
print(mymodule.add_numbers(5, 7))
Output
Hello, Alice!
12

2. Python Built-in Module: In Python, a built-in module is a module that


comes pre-installed with the Python interpreter, providing a set of
functionalities that can be readily used in your programs. Let us consider
an example of Python math module.

# Importing the built-in math module


import math

# Example 1: Using [Link]() to calculate the square root


num = 25
sqrt_result = [Link](num)

2|Page
print(f"The square root of {num} is: {sqrt_result}")

# Example 2: Using [Link]() to calculate the factorial


factorial_result = [Link](5)
print(f"The factorial of 5 is: {factorial_result}")

In this example, the math module is imported, and two of its functions (sqrt
and factorial) are used. The sqrt function calculates the square root of a
given number, and the factorial function computes the factorial of a given
integer. These functionalities are provided by the built-in math module,
which is why we can use them without installing any additional packages.

You can also rename a module while importing it. Let us learn with an
example code.

# Importing the math module and renaming it as 'mts'


import math as mts

# Printing the value of Euler's number (e) from the math module
print('The value of Euler number is:', mts.e)

Output
The value of Euler number is: 2.718281828459045

This Python program imports the math module and renames it as 'mts' using
the as keyword. It then prints the value of Euler's number (e) from the math
module using the alias 'mts'. The program provides a concise way to refer to
the math module with a shorter name for brevity in the code.

Python provides a variety of built-in modules that offer additional


functionalities. To explore these modules, you can utilize the help() method.
Simply type help() in a code cell and execute it, as illustrated in the
accompanying image. This method not only allows you to view a list of
available modules but also provides assistance on specific keywords, built-in
functions or any other topic when needed.

3|Page
Alt-text: Usage of help() method

Alt-Text: List of Python modules

Some of the important modules which you will be using mostly are-
datetime, random and math modules. You can use the help feature to
browse the built-in functions of these modules.
4|Page
Available functions in a module
In Python, you can check the list of available functions in a module using the
dir() function or by using the help() function.

a) Using dir() function: The dir() function returns a list of names in the
current local scope or the names of a specified object (e.g., a module).
You can use it to inspect the available functions and attributes of a
module.

import module_name

# List all functions and attributes of the module


print(dir(module_name))

Replace module_name with the actual name of the module you're


interested in.

b) Using help() function: The help() function provides interactive help for
objects, including modules. It can display a concise list of the module's
contents.

import module_name

# Display help for the module


help(module_name)

Replace module_name with the actual name of the module you want to
know about. This will print a summary of the module's documentation,
including the list of functions and their descriptions.

5|Page

You might also like