R
Rishtaara
Python: Zero to Professional
Lesson 10 of 40Article16 min

Dates, Math & JSON

The datetime module handles dates and times. datetime.now(), strftime(), and timedelta for differences.

Python Dates

The datetime module handles dates and times. datetime.now(), strftime(), and timedelta for differences.

Store dates as ISO strings in JSON; parse with fromisoformat() when needed.

Real-life example: datetime is a calendar plus clock on your phone — one module for today and tomorrow.

Date and time
from datetime import datetime, timedelta

now = datetime.now()
print(now.strftime("%Y-%m-%d"))
print(now + timedelta(days=7))

Python Math

Built-in math module: sqrt, pow, ceil, floor, pi, sin, cos. For heavy stats use statistics (later).

Use ** or math.pow for powers; // and % for integer division and remainder.

Real-life example: math is a calculator drawer — square roots and trig without reinventing formulas.

math module
import math
print(math.sqrt(25), math.pi)
print(math.ceil(4.2), math.floor(4.8))

Python JSON

json.dumps() converts Python dict/list to JSON string. json.loads() parses JSON back.

json.dump() and json.load() read/write JSON files — common for configs and APIs.

Real-life example: JSON is a universal packing box — Python dicts ship safely to JavaScript apps inside.

JSON encode/decode
import json

data = {"course": "python-zero-to-pro", "lesson": 10}
text = json.dumps(data)
parsed = json.loads(text)
print(parsed["course"])