Introduction
Modules help us organize code into small, reusable parts. They make programs easier to understand and maintain.
Jump into concepts and practice - no test required
Modules help us organize code into small, reusable parts. They make programs easier to understand and maintain.
import module_name # Use functions or variables from the module module_name.function_name()
.py extension.math module and uses its sqrt function to find the square root of 16.import math print(math.sqrt(16))
my_module and calls its greet function.import my_module
my_module.greet()This program uses the math module to calculate and print the square root of a number.
import math number = 25 root = math.sqrt(number) print(f"The square root of {number} is {root}")
Using modules keeps your code clean and easier to fix or improve.
Modules let you share useful code with friends or other programs.
Modules organize code into reusable parts.
They help manage big programs by splitting code into files.
Modules let you use code others wrote or share your own code.
math in Python?import.import math. Others are invalid in Python.import math print(math.sqrt(16))
math module and calls sqrt(16) which calculates the square root of 16.import random print(random.rand(5))
random module, so no import error.random module does not have a function named rand. The correct function is randint or random.