Table of Contents
Python Module has built-in functions, classes, and variables. There are several Python modules and each has their specific work. This article has listed the Python modules.
What is a Python Module?
The Python module consists of a file that has Python definitions and statements. A module will then define functions, classes, and variables. Additionally, the module has a runnable code.
Hence, categorizing the module will make the code easier to analyze. Therefore, the code will be more organized.
Create a Module
a.Create a new file, e.g., my_module.py
.
b.Define your module content inside this file.
# File: my_module.py
def greet(name):
"""A simple function to greet the user."""
return f"Hello, {name}! Welcome to my_module."
The syntax is given below:
import module
Use a Module
You will use the module that was created through the import statement.
# File: main_script.py
import my_module
# Using the greet function from my_module
name = "User"
message = my_module.greet(name)
print(message)
When you run main_script.py, it will import the my_module and take the greet function to print a welcome message.
These two files (my_module.py and main_script.py) are placed in the same directory where Python can find them.
Output
Hello, User! Welcome to my_module.
What are the examples of Importing Modules in Python?
The example illustrated below will import the calc for executing the add operation.
# File: main_script.py
import my_module
# Using the greet function from my_module
name = "User"
greeting_message = my_module.greet(name)
print(greeting_message)
# Using the add_numbers function from my_module
result = my_module.add_numbers(5, 7)
print(f"The result of addition is: {result}")
Output
Hello, User! Welcome to my_module.
The result of addition is: 12
How to Import Specific Attributes From a Python Module?
Let us look into the example given below :
# File: main_script.py
from math import sqrt, factorial
# Using the sqrt function from math
number = 25
square_root = sqrt(number)
print(f"The square root of {number} is: {square_root}")
# Using the factorial function from math
fact_number = 5
factorial_result = factorial(fact_number)
print(f"The factorial of {fact_number} is: {factorial_result}")
Output
The square root of 25 is: 5.0
The factorial of 5 is: 120
What is * Import symbol?
The * symbol is used to import the statement function to import all the names from the module to the current namespace.
The syntax is given below
from module_name import *
Note:
The * symbol has its benefits and drawbacks. Suppose the purpose is known to you. However, it is not advised to use *.
# File: main_script.py
from math import sqrt, factorial
# Using the sqrt function from math
number = 25
square_root = sqrt(number)
print(f"The square root of {number} is: {square_root}")
# Using the factorial function from math
fact_number = 5
factorial_result = factorial(fact_number)
print(f"The factorial of {fact_number} is: {factorial_result}")
Output
The square root of 25 is: 5.0
The factorial of 5 is: 120
Locate Python Module
- Current Directory:
- First, Python checks if the module is in the same folder where your Python script is located.
- PYTHON PATH:
- If it’s not in the current folder, Python checks other places listed in the PYTHON PATH variable.
- PYTHON PATH is like a list of folders where Python looks for modules. You can add more folders to this list if needed.
- Installation-Dependent Directories:
- If the module is still not found, Python checks places to set up when Python was installed.
- These are special folders that come with Python and contain important modules that are needed for many programs.
What is the Directories List For Modules?
The sys. path has a built-in variable within the sys module. It has a list of directories, in which the interpreter will search in the particular module.
# File: main_script.py
import sys
# Now you can use functions and variables from the sys module
print("Python version:")
print(sys.version)
print("")
print("System platform:")
print(sys.platform)
Output
Python version:
3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0]
System platform:
linux
How to Rename the Python Module?
The Python module will rename the module by importing it with the keyword.
The syntax is provided below
Syntax: Import Module_name as Alias_name
# File: main_script.py
import math
# Using the sqrt function from math
number = 18 # Change the number to 18
square_root = math.sqrt(number)
print(f"The square root of {number} is: {square_root}")
# Using the factorial function from math
fact_number = 5
factorial_result = math.factorial(fact_number)
print(f"The factorial of {fact_number} is: {factorial_result}")
Output
The square root of 18 is: 4.242640687119285
The factorial of 5 is: 120
What is Python Built-in Modules?
Python consists of built-in modules that allow them to import whenever required.
# File: main_script.py
import math
# Using the sqrt function from math
number = 25
square_root = math.sqrt(number)
print(f"The square root of {number} is: {square_root}")
# Using the factorial function from math
fact_number = 5
factorial_result = math.factorial(fact_number)
print(f"The factorial of {fact_number} is: {factorial_result}")
# Using the pi function from math
pi_value = math.pi
print(f"The value of pi is: {pi_value}")
# Converting 2 radians to degrees
radians_to_degrees = math.degrees(2)
print(f"2 radians in degrees is: {radians_to_degrees}")
# Converting 60 degrees to radians
degrees_to_radians = math.radians(60)
print(f"60 degrees in radians is: {degrees_to_radians}")
# Calculating sine of 2 radians
sine_value = math.sin(2)
print(f"The sine of 2 radians is: {sine_value}")
# Calculating cosine of 0.5 radians
cosine_value = math.cos(0.5)
print(f"The cosine of 0.5 radians is: {cosine_value}")
# Calculating tangent of 0.23 radians
tangent_value = math.tan(0.23)
print(f"The tangent of 0.23 radians is: {tangent_value}")
Output
The square root of 25 is: 5.0
The factorial of 5 is: 120
The value of pi is: 3.141592653589793
2 radians in degrees is: 114.59155902616465
60 degrees in radians is: 1.0471975511965979
The sine of 2 radians is: 0.9092974268256817
The cosine of 0.5 radians is: 0.8775825618903728
The tangent of 0.23 radians is: 0.23414336235146526
Conclusion
To conclude, this article has offered the information about the Python Modules. It has also included how to create the module, Python import from the module, Renaming the Python Module, and several others.
Python Modules-FAQs
Q1.What are the modules in Python?
Ans. Modules are referred to as the files with the “. py” extension and have Python code that can be imported inside another Python Program.
Q2.How many modules of Python are there?
Ans.200
Q3.What are the five modules in Python?
Ans. Those five modules are – collections, datetime, logging, math, numpy, os, pip, sys, and time
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together