Python: Basics of Modules and Libraries in Python

Introduction

Up to now, you’ve written Python programs using built-in features like variables, loops, and functions. But Python’s real strength lies in its modules and libraries — reusable pieces of code that extend what your program can do. In this tutorial, we’ll learn how to import modules, explore the standard library, and use external libraries to supercharge your projects.

 Previous Tutorial

Step 1: What Are Modules?

A module is simply a file containing Python code (functions, classes, or variables) that you can reuse. Python comes with a rich standard library of modules for everyday tasks.

Example: Using the math module

Code:

import math

 

print(math.sqrt(16))   # Output: 4.0

print(math.pi)         # Output: 3.141592653589793


  • import math loads the module.
  • You can then access its functions and constants with math. prefix.


 

 Step 2: Importing Specific Functions

Instead of importing the whole module, you can import only what you need.

Code:

from math import sqrt, pi

 

print(sqrt(25))   # Output: 5.0

print(pi)         # Output: 3.141592653589793

This makes your code cleaner and avoids typing the module name repeatedly.

 



Step 3: Aliases for Modules

You can give a module a shorter name using as.

Code:

import math as m

 

print(m.sqrt(9))   # Output: 3.0

This is especially useful for large libraries like NumPy or Pandas.



Step 4: Exploring the Standard Library

Python’s standard library includes modules for:

  • Random numbers: import random
  • Dates and times: import datetime
  • File operations: import os

Example:

Code:

import random

print(random.randint(1, 10))   # Random number between 1 and 10

With just imports, you can do powerful tasks without writing everything from scratch.

 



Step 5: Installing External Libraries

Beyond the standard library, Python has thousands of external libraries available via pip (Python’s package manager).

Example: Installing and using requests for web access

Code:

pip install requests

import requests

 

response = requests.get("https://example.com")

print(response.status_code)

 

External libraries let you do advanced tasks like web scraping, data analysis, or machine learning.

 

Step 6: Creating Your Own Module

You can also write your own module. Save this code in a file called mymath.py:

Code:

def square(num):

    return num * num

 


 Then import it in another file:

Code:

import mymath

print(mymath.square(4))   # Output: 16

This is how you organize your own reusable code.

 


Mini Exercise

Try this challenge:

  1. Import the random module.
  2. Write a function roll_dice() that returns a random number between 1 and 6.
  3. Call the function 5 times and print the results.

Hint:

Code:

import random

 

def roll_dice():

    return random.randint(1, 6)

 

for i in range(5):

print(roll_dice())




  

Conclusion

In this tutorial, you learned how to:

  • Import modules and specific functions
  • Use aliases for cleaner code
  • Explore Python’s standard library
  • Install and use external libraries with pip
  • Create your own modules

 

Modules and libraries are the toolbox of Python. They let you extend your programs far beyond the basics, opening doors to web development, data science, automation, and more.

In the next tutorial, we’ll dive into Basics of file handling — how to read, write, and manage files with Python.