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.
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.
Dates aur times ko represent karna, manipulate karna, aur format karna.
strftime() se date ko string mein badalna aur strptime() se string ko date mein.
sqrt, ceil, floor, pow jaise mathematical functions for scientific calculations.
HOW: datetime aur math in action
Datetime module
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
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.0math.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.
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
import datetimeke baaddatetime.datetime.now()likhna padta hai, sirfdatetime.now()nahi.strftime()format codes case-sensitive hain �%mmonth hai,%Mminute hai.- Date objects mein direct string se compare nahi kar sakte; pehle
strptime()se convert karna padta hai. - Math module functions integers aur floats dono ke saath kaam karte hain.
Ab JSON data format ke baare mein seekho � web APIs se data exchange ka standard tarika.