Popular Libraries
Take a friendly tour of Python's rich ecosystem and discover which libraries can supercharge your projects.
One of Python's biggest superpowers is its ecosystem. Thousands of free, open-source libraries exist so you don't have to build everything from scratch. Want to fetch data from the internet? There's a library for that. Draw a chart? Library. Train an AI model? Library.
Think of libraries like apps on your phone — each one is purpose-built to solve a specific problem, and you just install what you need.
See it in action
— step through the idea, then dive into the details below.Python's Secret Weapon: Libraries
Python comes with thousands of free, ready-made libraries — so you don't have to build everything from scratch. Need to fetch data, draw charts, or train an AI? There's a library for that.
How to install a library
Almost every library listed here is installed with one command in your terminal:
`` pip install library-name ``
Then you import it at the top of your Python file and start using it. That's it!
#Working With Data
NumPy and Pandas are the two most popular data libraries in Python — and they go hand in hand.
- NumPy gives you a super-fast array type for doing math on large lists of numbers. Think of it as a turbocharged spreadsheet column.
- Pandas builds on top of NumPy and gives you a
DataFrame— a full table with rows and columns, similar to Excel — that you can filter, sort, group, and analyse in just a few lines.
import pandas as pd
data = {
"name": ["Alice", "Bob", "Carol"],
"score": [88, 95, 72]
}
df = pd.DataFrame(data)
print(df)
print("Average score:", df["score"].mean())#Drawing Charts With Matplotlib
Matplotlib is the classic charting library. You give it your data and it draws line charts, bar charts, scatter plots, histograms, and more. It pairs naturally with Pandas — once you have a DataFrame, plotting is often just one method call.
import matplotlib.pyplot as plt
weeks = [1, 2, 3, 4]
users = [10, 45, 130, 310]
plt.plot(weeks, users, marker="o")
plt.title("Weekly Sign-ups")
plt.xlabel("Week")
plt.ylabel("Users")
plt.show() # Opens a window with the chartSeaborn for prettier charts
If you find Matplotlib charts look a bit plain, try Seaborn (pip install seaborn). It wraps Matplotlib with nicer default styles and makes complex charts (like heatmaps or regression plots) much easier to build.
#Talking to the Internet With Requests
Requests is the friendliest way to fetch data from the web. Every time your browser loads a page, it sends an HTTP request. Requests lets your Python code do the same — get JSON from an API, download a file, submit a form, and more.
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # 200 means success
print(type(response.json())) # The response body as a dict#Scraping Web Pages With Beautiful Soup
Beautiful Soup (package name: beautifulsoup4) helps you pull information out of HTML pages. Pair it with Requests: use Requests to download the HTML, then use Beautiful Soup to pick out the pieces you care about — headlines, prices, links, table rows, whatever you need.
from bs4 import BeautifulSoup
html = "<h1>Hello</h1><p>Welcome to <b>Python</b>!</p>"
soup = BeautifulSoup(html, "html.parser")
print(soup.h1.text) # The <h1> tag's text
print(soup.p.text) # The <p> tag's textAlways check a website's terms before scraping
Some websites explicitly forbid automated scraping in their Terms of Service. Always check robots.txt and the site's terms, be polite (add a small time.sleep between requests), and never scrape personal or sensitive data without permission.
#Building Web Apps
Python has three major web frameworks — your choice depends on project size:
- Flask — tiny and minimal. Great for small APIs, personal projects, and learning. You write only what you need.
- FastAPI — modern, fast, and auto-generates documentation. Best for building APIs that other code or apps will consume.
- Django — a full-featured framework with a built-in admin panel, authentication, and database tools. Best for large, complex web apps.
# A complete Flask web app in 7 lines!
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello from Python!"
# Run with: flask runFramework size analogy
Think of Flask as a bicycle (simple, you provide the effort), FastAPI as a scooter (faster, built-in extras), and Django as a car (lots of features and safety equipment, but more to learn upfront).
#Working With Images Using Pillow
Pillow (the modern fork of the old PIL library) lets you open, edit, and save images in Python. Resize photos, convert formats, crop, add text overlays, apply filters — all without opening an image editor.
from PIL import Image
img = Image.open("photo.jpg")
print(img.size) # e.g. (1920, 1080)
small = img.resize((200, 200))
small.save("thumbnail.jpg")#Databases With SQLAlchemy
SQLAlchemy lets you talk to relational databases (SQLite, PostgreSQL, MySQL, and more) using Python objects instead of raw SQL strings. You define your tables as Python classes and SQLAlchemy handles the SQL for you — making your code cleaner and less error-prone.
#Testing With pytest
pytest is the most popular testing framework for Python. You write small functions that check your code does what you expect, then run pytest in the terminal — it discovers and runs all your tests automatically and gives you a clear pass/fail report.
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
# Run with: pytest test_math.py#Machine Learning and AI
Python dominates the AI/ML world, and three libraries lead the pack:
- scikit-learn — the best starting point for classic ML (classification, regression, clustering). Great documentation and beginner-friendly API.
- PyTorch — flexible deep learning framework favoured in research. You build neural networks by writing Python directly, so it's easy to debug.
- TensorFlow / Keras — Google's deep learning framework. Keras (included in TensorFlow) offers a high-level API that's very beginner-friendly for building neural networks.
Which AI library should you start with?
If you're brand new to ML, start with scikit-learn — it covers most practical ML tasks and has outstanding tutorials. Once you want to build neural networks, try Keras (part of TensorFlow) for structured projects or PyTorch if you want more control.
#Picking the Right Library
Here's a quick cheat-sheet by goal:
- Analyse data / spreadsheets → Pandas + NumPy
- Draw charts → Matplotlib (or Seaborn)
- Call web APIs → Requests
- Scrape web pages → Requests + Beautiful Soup
- Build a web app or API → Flask (small) / FastAPI (APIs) / Django (large)
- Edit images → Pillow
- Store data in a database → SQLAlchemy
- Write automated tests → pytest
- Machine learning / AI → scikit-learn → Keras/PyTorch
Don't install libraries you don't need
It's tempting to pip install everything that sounds interesting. Instead, install a library only when you have a real project that needs it. Too many installed packages can cause version conflicts. Use a virtual environment (python -m venv venv) for each project to keep things isolated.
You want to download a JSON response from a public API. Which library is the best fit?
Key takeaways
- Python's ecosystem means you rarely have to build common functionality from scratch — just install and import.
- For data work, start with Pandas and NumPy; for charts, use Matplotlib.
- Requests handles HTTP calls; Beautiful Soup parses HTML for web scraping.
- Choose your web framework by project size: Flask (small), FastAPI (APIs), Django (large).
- For machine learning, scikit-learn is the best starting point before moving to Keras or PyTorch.
What does this code print?
import pandas as pd
data = {"name": ["Alice", "Bob"], "score": [80, 100]}
df = pd.DataFrame(data)
print(df["score"].mean())This code tries to parse HTML with Beautiful Soup, but it crashes. What is wrong?
from bs4 import BeautifulSoup
html = "<h1>Hello</h1>"
soup = BeautifulSoup(html)
print(soup.h1.text)Complete the code so it fetches a web page and prints whether the request was successful (status code 200).
import response = requests.get("https://api.github.com") print(response.)
Put these lines in the right order to create a tiny Flask web app.
return "Hello from Python!"
@app.route("/")from flask import Flask
def home():
app = Flask(__name__)
Write a short Python script that uses the requests library to fetch data from the public JSONPlaceholder API (https://jsonplaceholder.typicode.com/todos/1). Print the response status code, then print the 'title' field from the JSON response.
Try it live — edit the code and hit Run to execute real Python: