Lesson 15 � Foundation

DATES & MATH:
REAL-WORLD DATA.

Dates real-world mein har jagah hain � timestamps, deadlines, age calculation. Math module scientific calculations ke liye zaroori hai. Python ke built-in modules se date aur math operations easy ho jaate hain.

? 18 min✓ BeginnerPrerequisite: Modules

WHY: dates aur math kyun zaroori hain?

Socho aap ek application bana rahe ho jisme user ka age calculate karna hai, ya kisi deadline ka remaining time dikhana hai. Ya scientific calculations ke liye square root, ceiling, floor jaise functions chahiye. Bina modules ke yeh sab manually karna mushkil hai.

DATETIME

Dates aur times ko represent karna, manipulate karna, aur format karna.

FORMAT

strftime() se date ko string mein badalna aur strptime() se string ko date mein.

MATH

sqrt, ceil, floor, pow jaise mathematical functions for scientific calculations.

HOW: datetime aur math in action

Datetime module

python
import datetime

# Current date/time
now = datetime.datetime.now()
print(now)
print(now.year, now.month, now.day)

# Format date
print(now.strftime("%d/%m/%Y")) # 01/01/2026
print(now.strftime("%B %d, %Y")) # January 01, 2026

# Date arithmetic
from datetime import timedelta
tomorrow = now + timedelta(days=1)
print(tomorrow)

datetime.datetime.now() current date aur time deta hai. strftime() se date ko custom format mein convert kar sakte hain. timedelta se dates ko add/subtract kar sakte hain.

Math module

python
import math
print(math.sqrt(144)) # 12.0
print(math.ceil(4.2)) # 5
print(math.floor(4.8)) # 4
print(math.pow(2, 10)) # 1024.0

math.sqrt() square root deta hai, math.ceil() next integer tak round karta hai, math.floor() previous integer tak. math.pow() power calculation ke liye hai.

Try it: Age calculatorBirthdate se age calculate karo
Code ko apni birthdate se update karke run karein

Quick check

Import datetime module aur current year print karo.

Pehle datetime import karo, phir datetime.datetime.now().year ya datetime.date.today().year use karo.

Common mistakes

Dates & Math clear?

Ab JSON data format ke baare mein seekho � web APIs se data exchange ka standard tarika.