Google News
logo
Python Program to Use a module
To use a module in Python, you can import it using the `import` statement. Here's an example:

Suppose you have a file named `my_module.py` with the following code :
Program :
def greet(name):
    print(f"Hello, {name}!")

def add(a, b):
    return a + b

You can import this module and use its functions in another file as follows :

import my_module

my_module.greet("Alice")  # prints "Hello, Alice!"
result = my_module.add(2, 3)
print(result)  # prints 5

You can also import specific functions from a module, like this :

from my_module import greet, add

greet("Bob")  # prints "Hello, Bob!"
result = add(5, 7)
print(result)  # prints 12
You can also give a module an alias when importing it, like this:
import my_module as mm

mm.greet("Charlie")  # prints "Hello, Charlie!"
result = mm.add(4, 6)
print(result)  # prints 10​