Def keyword
def kya hota hai?
def Python ka keyword hai jisse hum function banate hain.
👉 Simple words me:
Function = ek machine jo input leti hai, process karti hai aur output deti hai
🔹 Function ka structure
def function_name(parameters):
# code
return result
Samajhne ka easy tarika:
def → function banana start
function_name → function ka naam
parameters → input
return → output
🔥 Function kaise banate hain (Step-by-step)
1. Decide karo function kya karega
2. Uska naam rakho
3. Input (parameter) decide karo
4. Logic likho
5. Output return karo
🔹 Example 1: Even or Odd
def check_even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
print(check_even_odd(4))
🔹 1. Even / Odd (Input ke saath)
def check_even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
num = int(input("Enter any value: "))
print(check_even_odd(num))
Logic:
% 2 → remainder check karta hai
agar remainder 0 → Even
warna → Odd
Example 2: Square
def square(n):
return n * n
print(square(5))
👉 Logic: number × number
2. Square
def square(n):
return n * n
num = int(input("Enter any value: "))
print("Square is:", square(num))
Example 3: Cube
def cube(n):
return n * n * n
print(cube(3))
👉 Logic: number × number × number
3. Cube
def cube(n):
return n * n * n
num = int(input("Enter any value: "))
print("Cube is:", cube(num))
🔹 Example 4: Addition
def add(a, b):
return a + b
print(add(2, 3))
4. Addition
def add(a, b):
return a + b
a = int(input("Enter first value: "))
b = int(input("Enter second value: "))
print("Sum is:", add(a, b))
🔹 Example 5: Subtraction
def subtract(a, b):
return a - b
print(subtract(5, 2))
5. Subtraction
def subtract(a, b):
return a - b
a = int(input("Enter first value: "))
b = int(input("Enter second value: "))
print("Result is:", subtract(a, b))
🔹 Example 6: Multiplication
def multiply(a, b):
return a * b
print(multiply(3, 4))
6. Multiplication
def multiply(a, b):
return a * b
a = int(input("Enter first value: "))
b = int(input("Enter second value: "))
print("Result is:", multiply(a, b))
🔹 Example 7: Armstrong Number
def is_armstrong(num):
temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp = temp // 10
if sum == num:
return "Armstrong"
else:
return "Not Armstrong"
print(is_armstrong(153))
7. Armstrong Number
def is_armstrong(num):
temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp = temp // 10
if sum == num:
return "Armstrong"
else:
return "Not Armstrong"
num = int(input("Enter any value: "))
print(is_armstrong(num))
Logic:
Har digit ka cube lo
Sabko add karo
original number se compare karo
👉 153 → 1³ + 5³ + 3³ = 153 ✔️
🔹 Example 8: Maximum Number
def find_max(a, b):
if a > b:
return a
else:
return b
print(find_max(10, 20))
8. Maximum Number
def find_max(a, b):
if a > b:
return a
else:
return b
a = int(input("Enter first value: "))
b = int(input("Enter second value: "))
print("Maximum is:", find_max(a, b))
🔹 Example 9: Factorial
def factorial(n):
result = 1
for i in range(1, n + 1):
result = result * i
return result
print(factorial(5))
9. Factorial
def factorial(n):
result = 1
for i in range(1, n + 1):
result = result * i
return result
num = int(input("Enter any value: "))
print("Factorial is:", factorial(num))
Logic:
5! = 5×4×3×2×1
Example 10: Simple Interest
def simple_interest(p, r, t):
return (p * r * t) / 100
print(simple_interest(1000, 5, 2))
10. Simple Interest
def simple_interest(p, r, t):
return (p * r * t) / 100
p = int(input("Enter principal: "))
r = int(input("Enter rate: "))
t = int(input("Enter time: "))
print("Simple Interest is:", simple_interest(p, r, t))
🔥 Important Tips
✅ Function naam meaningful rakho
❌ a(), xyz() mat use karo
✅ Har function ka ek clear kaam hona chahiye
✅ Code repeat ho raha ho → function bana do
🔥 Important baat (must remember)
👉 input() → string deta hai
👉 int(input()) → number me convert karta hai
🔥 Real Understanding (Golden Line)
👉 Function = input → process → output
🔹 Input kaise lete hain?
Python me hum input() function use karte hain:
num = input("Enter any value: ")
👉 Problem:
input() hamesha string deta hai
👉 isliye number ke liye convert karna padta hai:
num = int(input("Enter any value: "))
🔥 Pro Tip (Smart coding 😎)
Agar tum beginner ho to:
Pehle function likho
Phir input add karo
def maximum(a,b,c):
if a>b and a>c:
return a
elif b>c:
return b
else:
return c
a = int(input("Enter number: "))
b = int(input("Enter number: "))
c = int(input("Enter number: "))
print(maximum(a,b,c))
def minimum(a,b,c):
if a<b and a<c:
return a
elif b<c:
return b
else:
return c
a = int(input("Enter number: "))
b = int(input("Enter number: "))
c = int(input("Enter number: "))
print(minimum (a,b,c))