Modules & Packages
Modules are simply Python files with a .py extension. You can use their contents in your own programs using the import statement.

Examples and Syntax
Section titled “Examples and Syntax”To use a module’s functions, you can import the entire module and access its contents with dot notation.
import mathprint(math.sqrt(16)) # Output: 4.0Alternatively, you can import specific functions directly into your program’s namespace, so you don’t need to use the module name.
from math import sqrtprint(sqrt(16)) # Output: 4.0To import all functions and variables from a module, you can use the * wildcard. However, this is generally not recommended as it can lead to name conflicts.
from math import *print(ceil(4.2)) # Output: 5The dir() function can be useful to see all the available attributes and functions within a module.
import mathprint(dir(math))Random Module
Section titled “Random Module”The random module is used for generating random numbers and sequences.
random.choice(seq): Returns a random element from a sequence.random.randrange(start, stop, step): Returns a random integer from a specified range.random.random(): Returns a random float between0.0and1.0.random.seed(x): Sets the seed to ensure that random numbers are reproducible.random.shuffle(lst): Shuffles the items in a list randomly in-place.
Math Module
Section titled “Math Module”The math module provides a wide range of mathematical functions and constants.
math.ceil(x): Rounds a number up to the nearest integer.math.floor(x): Rounds a number down to the nearest integer.math.sqrt(x): Returns the square root of a number.math.pow(x, y): Returnsxraised to the power ofy.abs(x): A built-in function that returns the absolute value of a number.
Do we need to know about all these modules & Its method ??
Section titled “Do we need to know about all these modules & Its method ??”No, We don’t need to learn about these modules. We need to use them as per our need. The fact is, modules are made by some other developers, the team or individual developer who develops the module provides its documentation like how to use this package!
For Example, Explore the Module made by me which got 2.5k Mothly downloads till now - ahcodecompiler and still counting!!!
Packages and pip
Section titled “Packages and pip”A package is a way to organize related modules into a directory structure. A directory becomes a Python package if it contains a special __init__.py file (which can be empty).
Creating a Package
Section titled “Creating a Package”A typical package structure looks like this:
mypackage/├── __init__.py├── math_operations.py└── string_operations.py__init__.pymakes themypackagedirectory recognizable as a package.math_operations.pyandstring_operations.pyare the modules within the package.
To use the package, you can import its modules or specific functions.
# From math_operations.pydef add(a, b): return a + b# From __init__.pyfrom .math_operations import addWith this setup, you can import and use the function like this:
from mypackage import addprint(add(2, 3)) # Output: 5Using pip for Installation
Section titled “Using pip for Installation”pip is the standard package manager for Python. It allows you to install, update, and manage third-party libraries from the Python Package Index (PyPI), such as NumPy for numerical operations or Flask for web development.
- Install a package:
pip install package_name - Install multiple packages:
pip install numpy pandas - List installed packages:
pip list - Upgrade a package:
pip install --upgrade package_name - Uninstall a package:
pip uninstall package_name