Python Tutorial - Modules
๐ฆ Modules - Ready-Made Tools
๐ Real Life Mein:
Tools jo already available hain:
๐ง "Toolkit box" - Har tool already ready
๐ "Library" - Books already likhi hui
๐ฝ๏ธ "Ready-to-eat food" - Khud banana nahi padta
๐ฑ "Apps" - Download aur use karo
Python Modules = Pre-written code import karo!
๐ Module Kya Hai?
Module ek Python file hai jisme:
- Ready-made functions
- Useful variables
- Pre-written code
Faada: Wheel reinvent nahi karna padta!
Module Import Karna
import math
# Math functions available
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.14159...
print(math.pow(2, 3)) # 8.0
๐ฒ Popular Built-in Modules
| Module | Use | Example |
|---|---|---|
random | Random values | random.randint(1,6) |
math | Math operations | math.sqrt(25) |
datetime | Date/Time | datetime.now() |
os | Operating System | os.getcwd() |
๐ฒ Dice Game
import random
# 1 se 6 beech random number
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
print(f"Dice 1: {dice1}")
print(f"Dice 2: {dice2}")
print(f"Total: {dice1 + dice2}")
๐
Current Date/Time
import datetime
# Aaj ka date/time
now = datetime.datetime.now()
print("Today:", now.date())
print("Time:", now.time())
print("Year:", now.year)
Install External Modules:
pip install module_name command se naye modules install kar sakte ho!
Example
import random
x = random.randint(1, 100)
print(x)