Python Modules and Libraries

Hey friends 👋 welcome back, In the last lesson, we learned about dictionaries, Today, we are going to learn about modules and libraries, which allow us to use code written by others to make our programs more powerful, Python has many built-in modules, and you can also install third-party libraries to extend Python’s capabilities.

What is a Module?

A module is a file containing Python code, such as functions, variables, and classes, You can use a module in your program by importing it, Example:

import math print(math.sqrt(16)) # prints the square root of 16

Output:

4.0

Importing Specific Functions

You can import only the functions you need from a module, Example:

from math import pi, pow print(pi) # prints 3.141592653589793 print(pow(2,3)) # prints 2 to the power of 3

Aliasing Modules

You can give a module a shorter name using as, Example:

import math as m print(m.sqrt(25))

Output:

5.0

Using Third-Party Libraries

You can install external libraries using pip, Example:

pip install requests

Then use it in your code:

import requests response = requests.get("https://www.example.com") print(response.status_code)

Practice Time

Create a Python file called modules_libraries.py and try these exercises, 1, Use the math module to calculate the square root of a number, 2, Use math functions to calculate powers and absolute values, 3, Install a library like requests and fetch the status code of a website, 4, Import a function from a module and use it in your program

Great job, You just learned how to use modules and libraries in Python to make your programs more powerful, In the next lesson, we will explore Python File Handling, where we will learn how to read and write files, We will soon be launching our YouTube channel, where we are going to post step-by-step video tutorials on Python and other tech skills, These videos will make learning easier and more visual, helping beginners follow along confidently.