Python Standard Library
In this page:
os और sys Modules
os module आपको filesystem और operating system के साथ interact करने देता है (directories list करना, paths जोड़ना, environment variables पढ़ना), जबकि sys interpreter-level details जैसे command-line arguments और module search path को उजागर करता है -- ये दोनों मिलकर आपकी script और उसके environment के बीच की कड़ी हैं।
उदाहरण: The os and sys Modules
import os
import sys
print(os.getcwd()) # current working directory
print(sys.platform) # identifies the operating system
datetime Module
datetime module समय के किसी क्षण को दर्शाने के लिए date, time, और datetime objects देता है, साथ ही duration दर्शाने के लिए timedelta -- यह महीने की सीमा पार करते हुए दिन जोड़ने या दो dates की सही तुलना करने जैसी आश्चर्यजनक रूप से पेचीदा arithmetic को संभालता है।
उदाहरण: The datetime Module
from datetime import datetime, timedelta
now = datetime(2024, 1, 1)
print(now + timedelta(days=10)) # timedelta handles the date arithmetic correctly
random Module
random.random() और random.randint() pseudo-random numbers generate करते हैं, और random.choice()/random.shuffle() सीधे sequences पर काम करते हैं -- यह dice roll simulate करने से लेकर quiz question का order shuffle करने तक किसी भी काम के लिए उपयोगी है, हालाँकि यह cryptographically secure नहीं है।
उदाहरण: The random Module
import random
print(random.randint(1, 6))
json Module
json.dumps() किसी Python dict या list को JSON-formatted string में बदल देता है (encoding), और json.loads() इसका उल्टा करता है, JSON string को वापस native Python objects में parse करता है -- यही वह standard तरीका है जिससे Python programs JSON exchange करने वाले web APIs से बात करते हैं।
उदाहरण: The json Module
import json
data = {"name": "Alex"}
text = json.dumps(data) # dict -> JSON string
print(text)
print(json.loads(text)) # JSON string -> Python object again
math Module
math module वे functions देता है जो standard arithmetic operators नहीं देते, जैसे math.sqrt(), trigonometric functions, math.log(), और math.floor()/math.ceil() जैसे rounding helpers -- जब भी आपको +, -, *, और / से ज़्यादा कुछ चाहिए, इसका उपयोग करें।
उदाहरण: The math Module
import math
print(math.sqrt(16)) # square root
print(math.floor(4.7)) # rounds down
print(math.ceil(4.2)) # rounds up
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: