← Back to Python Course | Chapter 8: Modules & Packages | Lesson 3 of 6

Python Standard Library

Standard library उपयोगी tools का एक बड़ा समूह है जो Python के साथ मुफ़्त में आता है, जैसे पहले से बर्तनों से भरी हुई रसोई। यह dates, math, files और भी बहुत कुछ संभालती है।

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

python
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

python
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

python
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

python
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

python
import math
print(math.sqrt(16))  # square root
print(math.floor(4.7))  # rounds down
print(math.ceil(4.2))  # rounds up
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.