Skip to content

Expert · Python in the real world · topic 49 of 50 · 25 min

What Python is used for: web, data and AI

Explanation

Python is used to build websites and APIs (Django, FastAPI), to analyse data (pandas), to build AI and machine learning systems, and to automate boring tasks. These tools are packages built on the same basics you already know: functions, dicts, lists and classes.

One language, many jobs

You have learned core Python. Companies use that same Python with extra packages for specific work. The examples in this lesson need those packages installed with pip, so they do not run in the browser here. Read them to see how familiar they look.

Think of it like this: Core Python is like learning to drive. Django, pandas and PyTorch are different vehicles: a bus, a truck, a tractor. Each has its own controls, but if you can drive, you can learn any of them quickly.

Web: Django and FastAPI

A web framework is a package that handles the repeated parts of a website: reading the address the user visited, talking to the database and sending back a page or data. Django is a large "batteries included" framework. FastAPI is a lighter framework for APIs. An API (application programming interface) is a web address that returns data, usually JSON, for apps to use.

python
# needs: pip install fastapi uvicorn  (does not run here)
from fastapi import FastAPI

app = FastAPI()

@app.get("/price/{item}")
def price(item: str):
    prices = {"chai": 15, "samosa": 20}
    return {"item": item, "price": prices.get(item, 0)}
A tiny FastAPI app. Notice the decorator, the type hint and the dict you already know.
python
# a Django view (inside a Django project; does not run here)
from django.http import JsonResponse

def hello(request):
    return JsonResponse({"message": "Namaste"})
In Django, a view is a function that takes a request and returns a response.

Data: pandas

pandas is the most used package for working with tables of data. Its main object, the DataFrame, is a table with named columns, like an Excel sheet inside Python.

python
# needs: pip install pandas  (does not run here)
import pandas as pd

df = pd.read_csv("expenses.csv")
print(df.groupby("category")["amount"].sum())
Read a CSV file and total the amount for each category in two lines.

Under the hood, groupby does what you can already write with a dict and a loop:

python
expenses = [("food", 250), ("travel", 120), ("food", 90)]
totals = {}
for category, amount in expenses:
    totals[category] = totals.get(category, 0) + amount
print(totals)
Plain Python version of groupby + sum: {'food': 340, 'travel': 120}.

AI and machine learning

Machine learning means the computer learns patterns from examples instead of following rules you write by hand. scikit-learn is used for classic models (for example predicting house prices), and PyTorch for deep learning (images, speech, language). Many apps also call large language models through an API using a few lines of Python.

output
model.fit(examples, answers)    # learn from past data
model.predict(new_examples)     # guess answers for new data
The idea behind most scikit-learn models: fit, then predict.

Automation and scripting

Many people first use Python at work to automate boring jobs: renaming files, filling Excel sheets, sending reports, cleaning data. The standard library (os, pathlib, csv, json, datetime) covers much of this.

Tip: Pick one area and build one small project in it. A working mini project (an expense API, a marks analysis notebook) teaches more and impresses interviewers more than ten half-watched tutorials.
Careful: Do not jump to AI before you are comfortable with lists, dicts, functions and loops. Every AI library expects you to handle data with these basics.
AreaMain packagesTypical job titles
Web back endDjango, FastAPI, FlaskPython developer, back-end developer
Datapandas, NumPy, MatplotlibData analyst
AI / MLscikit-learn, PyTorchML engineer, data scientist
Automation and testingstandard library, pytest, SeleniumAutomation engineer, QA engineer

Watch the lesson

What Python is used for: web, data and AI · 1/6

Web · Data · AI · Automation

You know core Python now. Let us see what people build with it at work.

Prefer a video by a teacher? Find free videos on “what can you do with python web data science ai” on YouTube ↗

In real life: A canteen owner's sales report

Lakshmi runs a college canteen. Every day her billing app saves sales in a CSV file. A short pandas script groups sales by item and shows the bestsellers, a FastAPI app shows today's menu prices to students on their phones, and later a simple model could predict how many samosas to make on exam days. All three are Python.

python
sales = [("samosa", 20), ("chai", 15), ("samosa", 20), ("vada pav", 25), ("chai", 15)]
count = {}
for item, price in sales:
    count[item] = count.get(item, 0) + 1
best = max(count, key=count.get)
print("Bestseller:", best)
Finding the bestseller with a dict, the same idea pandas uses.

Practice (0/3 done)

Task 1 · Code · Python

pandas groupby in plain Python: write a function total_by_category(expenses) that takes a list of dicts like {"category": "food", "amount": 250} and returns a dict of total amount per category.

Task 2 · Code · Python

A web API often filters data. Write a function students_in_city(students, city) that returns the names (in the same order) of students whose "city" matches, ignoring capital letters. Each student is a dict with "name" and "city".

Task 3 · Practice

Choose the area that interests you most (web, data, AI or automation) and write a one-page plan for a small project in it.

Quick quiz

Question 1

1.Which package is mainly used to work with tables of data?

Question 2

2.What is an API?

Question 3

3.Which two are Python web frameworks?

Question 4

4.What does "machine learning" mean?

Question 5

5.Why do these examples not run in this browser?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Virtual environments and pip