What is a module in Python?

What is a module in Python?

a) A built-in function
b) A file containing Python code that can be imported and used in other programs
c) A variable that stores multiple values
d) A loop that iterates over multiple sequences

Answer:

b) A file containing Python code that can be imported and used in other programs

Explanation:

A module in Python is a file that contains Python code, such as functions, classes, and variables, which can be imported and used in other programs. Modules help to organize and reuse code across multiple programs by allowing you to import the same code into different scripts.

# contents of my_module.py
def greet(name):
    print(f"Hello, {name}!")

# In another script
import my_module
my_module.greet("Alice")  # Output: Hello, Alice!

In this example, the my_module module contains a function that is imported and used in another script. Modules make it easier to manage and maintain code by separating functionality into distinct, reusable units.

Python also provides a rich standard library of modules that cover a wide range of functionality, from mathematics to file handling, networking, and more.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top