Python Tutorial - Functions
โ๏ธ Functions - Apna Code Robot Banao
๐ Real Life Mein:
Reusable tasks har jagah hain:
๐ณ "Recipe" - Same steps repeat, alag ingredients
๐ญ "Factory machine" - Input do, output milta hai
๐งฎ "Calculator" - Numbers do, answer milta hai
๐ค "Coffee machine" - Button press, coffee ready
Function = Ek baar likho, baar-baar use karo!
๐ฏ Function Kyu Zaroori Hai?
Functions ke fayde:
- Code reuse kar sakte ho
- Code organized rahta hai
- Testing aur debugging easy
- Ek jagah change karo, har jagah update!
Simple Function
# Function define karo
def greet():
print("Hello!")
print("Welcome to Python!")
# Function call karo
greet() # Output dono lines
greet() # Dobara use kar sakte ho!
๐ฅ Parameters - Input Lena
๐ Birthday Greeting Generator
def birthday_wish(name, age):
print(f"Happy Birthday {name}!")
print(f"Aaj tum {age} saal ke ho gaye!")
print("Party karo! ๐")
# Different people ke liye use karo
birthday_wish("Rohan", 15)
birthday_wish("Simran", 14)
birthday_wish("Amit", 16)
๐ค Return - Output Dena
๐งฎ Calculator Functions
def add(a, b):
result = a + b
return result
def calculate_bill(price, quantity, tax_percent):
subtotal = price * quantity
tax = subtotal * (tax_percent / 100)
total = subtotal + tax
return total
# Use karo
sum_result = add(10, 20)
print(sum_result) # 30
bill = calculate_bill(100, 3, 18)
print(f"Total bill: โน{bill}")
Function vs Method:
Function:
Method:
Function:
len(list) - IndependentMethod:
list.append() - Object se attached
Example
def hello():
print("Hi")
hello()