Python includes a wide range of built-in modules that provide additional functionality. These modules allow you to perform tasks like handling dates, math operations, file I/O, and much more. In this lesson, you will learn how to import and use these modules in your programs.
To use a module, you must first import it into your program. You can import entire modules or specific functions from a module.
import math
# Using a function from the math module
result = math.sqrt(16)
print(result)
from math import sqrt
# Directly using the sqrt function without the module prefix
result = sqrt(16)
print(result)
You can give a module or function a shorter name using the as
keyword. This is useful when working with long module names.
import numpy as np
# Using the numpy module with an alias
array = np.array([1, 2, 3, 4])
print(array)
Python comes with several built-in modules that can simplify your development. Below are a few commonly used modules. Python All Built-in Modules List
We can run the below command to get the all available modules in Python:
help('modules')
math
ModuleThe math
module provides access to mathematical functions. For example, it includes functions like sqrt()
for square roots, pow()
for exponentiation, and constants like pi
.
import math
print("Square root of 16:", math.sqrt(16))
print("Value of pi:", math.pi)
The math
module is especially useful when performing advanced mathematical operations. It provides trigonometric functions, logarithmic functions, and more.
datetime
ModuleThe datetime
module allows you to work with dates and times.
import datetime
# Get the current date and time
now = datetime.datetime.now()
print("Current date and time:", now)
# Extract specific components
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
random
ModuleThe random
module provides functions to generate random numbers. It is commonly used in games, simulations, and testing.
import random
# Generate a random number between 1 and 10
random_number = random.randint(1, 10)
print("Random number between 1 and 10:", random_number)
os
ModuleThe os
module provides a way to interact with the operating system, allowing you to work with files, directories, and environment variables.
import os
# Get the current working directory
current_directory = os.getcwd()
print("Current working directory:", current_directory)
import module_name
to import an entire module, or from module_name import function_name
to import specific functions.as
to give a module or function a shorter name.math
, datetime
, random
, and os
that extend the functionality of Python without needing external libraries.Objective: Practice importing and using built-in Python modules.
Create a Python File:
Create a file named modules_practice.py
.
Use the math
Module:
Use the math
module to calculate the square root of a number and the value of pi
.
Example:
import math
print("Square root of 25:", math.sqrt(25))
print("Value of pi:", math.pi)
Use the datetime
Module:
Use the datetime
module to print the current date and time.
Example:
import datetime
now = datetime.datetime.now()
print("Current date and time:", now)
Use the random
Module:
Use the random
module to generate a random number between 1 and 100.
Example:
import random
random_number = random.randint(1, 100)
print("Random number between 1 and 100:", random_number)
This lesson includes explanations and examples of using some of Python's most popular built-in modules, followed by a practice task to help students solidify their understanding.