Importing and Using Built-in Modules

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.

Key Points:

  • Modules are collections of related functions and variables that can be reused across programs.
  • Python provides many built-in modules for common tasks like mathematics, working with files, dates, and more.
  • You can import an entire module, specific functions, or use an alias for convenience.

Importing Modules

To use a module, you must first import it into your program. You can import entire modules or specific functions from a module.

Example: Importing an Entire Module

import math

# Using a function from the math module
result = math.sqrt(16)
print(result)

Example: Importing Specific Functions

from math import sqrt

# Directly using the sqrt function without the module prefix
result = sqrt(16)
print(result)

Aliasing Modules

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)

Built-in Modules Examples

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 Module

The 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)
Info

The math module is especially useful when performing advanced mathematical operations. It provides trigonometric functions, logarithmic functions, and more.

datetime Module

The 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 Module

The 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 Module

The 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)
  • Importing Modules: Use import module_name to import an entire module, or from module_name import function_name to import specific functions.
  • Aliasing: You can use as to give a module or function a shorter name.
  • Built-in Modules: Python includes many useful built-in modules like math, datetime, random, and os that extend the functionality of Python without needing external libraries.
Task

Practice: Using Built-in Modules

Objective: Practice importing and using built-in Python modules.

  1. Create a Python File: Create a file named modules_practice.py.

  2. 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)
    
  3. 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)
    
  4. 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.

Copyright © 2025 Devship. All rights reserved.

Made by imParth