Essential Python Modules πŸ”₯ | time, random, os, secrets & More #15 || Python ka Pitara || DeathCode - DeathCode

πŸ“œ Most-Used Python Modules with Real-World Use Cases


Python beginner ho ya pro… kuch modules hote hai jinke bina kaam hi nahi chalta! Yeh post tumhare liye ek complete ready-reference hai Python ke sabse zyada use hone wale modules ke saath real examples.


πŸ”Ή 1. time Module

πŸ”§ Kya kaam aata hai?

  • Delay ya sleep dene ke liye
  • Timestamp lene ke liye
  • Kisi code ka execution time measure karne ke liye

πŸ’‘ Example:

import time
 
print("Start")
time.sleep(2)
print("End after 2 seconds")
 
print("Current time:", time.time())
 
start = time.time()
# some heavy code
end = time.time()
print("Execution time:", end - start)

βœ… Real use:

  • Web scraping me throttling
  • Game delays
  • Code performance benchmarking

πŸ”Ή 2. datetime Module

πŸ”§ Kya kaam aata hai?

  • Dates aur timestamps manage karne ke liye
  • Reminders, expiry calculations, logging

πŸ’‘ Example:

from datetime import datetime, timedelta
 
now = datetime.now()
print(now)
 
future = now + timedelta(days=5)
print("5 days later:", future)
 
print(now.strftime("%d-%m-%Y %H:%M:%S"))

βœ… Pro Tip:

  • Age calculator ya daily reminder apps banane me kaam aata hai.

πŸ”Ή 3. random Module

πŸ”§ Kya kaam aata hai?

  • Games ke choices banane me
  • OTP ya random number generate karne me
  • Lottery-type logic ke liye

πŸ’‘ Example:

import random
 
print(random.randint(1, 100))
print(random.choice(["rock", "paper", "scissors"]))
 
deck = [1,2,3,4,5,6,7,8,9]
random.shuffle(deck)
print(deck)

⚠️ Tip:

  • Secure OTP ke liye secrets use karo, random predictable hota hai.

πŸ”Ή 4. secrets Module

πŸ”§ Kya kaam aata hai?

  • Secure passwords, OTPs aur tokens generate karna

πŸ’‘ Example:

import secrets
 
print(secrets.randbelow(100))
print(secrets.token_hex(16))

βœ… Jab security zaroori ho, secrets best hai.


πŸ”Ή 5. math Module

πŸ”§ Kya kaam aata hai?

  • Scientific aur accurate math operations

πŸ’‘ Example:

import math
 
print(math.sqrt(16))
print(math.floor(3.9))
print(math.ceil(3.1))
print(math.pow(2, 3))
print(math.pi)

βœ… Tip:

  • math.isclose() float comparison ke liye useful hai.

πŸ”Ή 6. os Module

πŸ”§ Kya kaam aata hai?

  • Files aur folders manage karne ke liye
  • System-level automation ke liye

πŸ’‘ Example:

import os
 
print(os.getcwd())
os.mkdir("test-folder")
print(os.listdir())
 
if os.path.exists("test-folder"):
    os.rmdir("test-folder")

βœ… Advanced:

  • Automate backups, setup scripts, file cleanup.

πŸ”Ή 7. sys Module

πŸ”§ Kya kaam aata hai?

  • Command line arguments lene ke liye
  • Program ko terminate karne ke liye

πŸ’‘ Example:

import sys
 
print(sys.argv)
sys.exit("Exiting the program")

βœ… CLIs aur automation scripts me kaafi use hota hai.


πŸ”Ή 8. platform Module

πŸ”§ Kya kaam aata hai?

  • OS detect karna
  • Platform-specific logic likhna

πŸ’‘ Example:

import platform
 
print(platform.system())
print(platform.processor())

βœ… Useful in cross-platform scripts, installers.


πŸ”Ή 9. collections Module

πŸ”§ Kya kaam aata hai?

  • Special data types provide karta hai

πŸ’‘ Example:

from collections import Counter
 
print(Counter("hello world"))

βœ… Extra tools: deque, defaultdict, namedtuple


πŸ”Ή 10. Bonus Modules

πŸ”Έ webbrowser – URL browser me open karna:

import webbrowser
webbrowser.open("https://deathcode.in")

πŸ”Έ shutil – File copy/move:

import shutil
shutil.copy("file.txt", "backup.txt")

πŸ”Έ calendar – Calendar banana:

import calendar
print(calendar.month(2025, 4))

🎯 What's Next?

Agar tum Python me serious ho, to in modules ka real-world use samajhna aur apply karna bahut zaroori hai. Inse tum scripts likh paoge, automate kar paoge, aur daily life ke tasks solve kar paoge.

Agle part me hum dekhenge Python ki File Handling β€” jo automation ke duniya ka asli gate unlock karta hai!

Stay tuned, and code smart πŸš€

Β© 2024 DeathCode. All Rights Reserved.