Python Module is a code library. Python modules are like toolkits or collections of pre-made tools that you can use in your Python programs. Each tool in the toolkit (module) serves a specific purpose, such as performing mathematical operations, interacting with files, or connecting to the internet.
Imagine you’re building something with LEGO bricks. Instead of making every single piece from scratch, you can use pre-made LEGO pieces from different kits. Similarly, Python modules provide pre-written code that you can reuse in your programs, saving you time and effort.
For example, if you’re working on a project that involves handling dates and times, you can use the “datetime” module. If you need to work with files, you can use the “os” module. These modules contain ready-to-use functions and classes that perform common tasks related to their respective areas.
By importing these modules into your Python scripts, you gain access to their tools, allowing you to focus on solving the specific problems you’re interested in, rather than reinventing the wheel for every task.
Create a Python Module
To create a module just save your code file with .py extension.
# Save this code in a file named new_module.py
def my_name(name):
"""
This function greets the user with a given name.
Parameters:
name (str): The name of the person to greet.
Returns:
None
"""
print("Hello, " + name)
# Example usage of the function
if __name__ == "__main__":
my_name("John")PythonIn this code:
- The
my_namefunction takes a name as input and prints a greeting message. - There’s a comment indicating that this code should be saved in a file named
new_module.py. - Additionally, there’s a conditional block that executes the
my_namefunction with the name “John” only if the script is run directly (not imported as a module). This is commonly used to include example usage or test cases when the script is run directly.
Use a Python Module
We can use our created module by using an import statement.
# We can use our created module by using import statement.
import new_module
new_module.my_name("Huma")Python| Output Hello, Huma |
Variables in the Python Module
As in the above examples, we use modules containing functions but we can also use all types of variables in our modules.
Save this code in file new_module.py.
student = {
"name": "Huma",
"department": "Computer Science",
"Roll": "14020204-050"
}
print(student)Python| Output {‘name’: ‘Huma’, ‘department’: ‘Computer Science’, ‘Roll’: ‘14020204-050’} |
Import the module new_module.py and access the student dictionary:
# Inside new_module.py
student = {
"name": "Huma",
"department": "Computer Science",
"Roll": "14020204-050"
}PythonNow, let’s assume new_module.py contains the correct definition of the student dictionary. You would then run the following code:
import new_module
a = new_module.student["department"]
print(a)PythonThis code will import the new_module module, access the student dictionary inside it, and retrieve the value associated with the key "department". Finally, it will print the value, which in this case is "Computer Science".
Module Naming
We can choose any name for our module file, but our module file extension should be .py.
Built-in Modules
Python facilitates us with several built-in modules, we can import these built-in modules whenever in our code.
Example
Here is an example of importing a built-in module called platform module.
import platform
x = platform.system()
print(x)PythonNow, when you run this code, it will print out the name of the operating system you’re using. For example, on Windows, it will print "Windows", on Linux, it will print "Linux", and so on.
DateTime in Python Modules
Dates
Python does not have its data type for date, but we can import a built-in module called datetime.
Let’s import the datetime module to display the current date:
Example
import datetime
x = datetime.datetime.now()
print(x)Python| Output 2024-03-29 15:25:37.123456 |
This code imports the datetime module, accesses the datetime class within it, calls the now() method to get the current date and time, and then prints it out. When you run this code, it will display the current date and time.
The output will vary depending on the current date and time when you execute the code.
Author: TCF Editorial
Copyright The Cloudflare.
You can also read :

