Python Series β Part 7: Functions in Python (Full Detailed Guide)
π§ Objective:
- Functions kya hote hain
- Function syntax
- Parameters, return value
- Default arguments, *args, **kwargs
- Functions ka real-world use
π Introduction
"Socho agar har baar tumhe ek hi code baar baar likhna padeβ¦
Jaise ek chai banane ki recipe β har time same steps, same ingredientsβ¦
Toh har baar likhna bore nahi hoga?
Bas isi dikkat ka solution hai β Functions!
Aaj ke video me hum seekhenge Functions in Python β from basic to advanced level tak!
Let's gooooo!"
π§Ή Section 1: What is a Function?
"Function ek block of code hota hai jo specific task perform karta hai.
Jaise ek machine β input do, processing kare, output de."
Example:
def greet():
print("Hello, welcome to Python ka Pitara!")
"Is function ko jab call karoge, ye greeting dega."
β Section 2: Why Use Functions?
Without functions:
print("Chai ready hai")
print("Chini daali")
print("Doodh daala")
With function:
def make_tea():
print("Chini daali")
print("Doodh daala")
print("Chai ready hai")
make_tea()
"Function se code reusable, organized aur error-free banta hai."
π§± Section 3: Function Syntax
def function_name(parameters):
# code block
return something
def
β function banane ke liyefunction_name
β meaningful naamparameters
β inputs (optional)return
β output (optional)
π Section 4: Types of Functions
- Without Parameters, Without Return
def say_hello():
print("Hello")
- With Parameters, Without Return
def greet(name):
print(f"Hello {name}")
- With Parameters, With Return
def add(a, b):
return a + b
"Return ka matlab output lekar function se bahar nikalna."
π§ Section 5: How Return Works
"Return se value milti hai aur function wahi ruk jaata hai."
Example:
def is_even(num):
return num % 2 == 0
Important:
return
ke baad ka code nahi chalta- Agar
return
nahi diya toNone
return hota hai
π§ββοΈ Section 6: Default Parameters, Keyword Arguments
Default Parameter:
def greet(name="Guest"):
print(f"Hello {name}")
Keyword Arguments:
def intro(name, age):
print(f"{name} is {age} years old.")
intro(age=25, name="Rahul")
"Keyword arguments me order matter nahi karta."
β‘ Section 7: *args and **kwargs
*args
β multiple positional arguments
def total(*numbers):
return sum(numbers)
print(total(2, 3, 5))
**kwargs
β multiple keyword arguments
def info(**data):
for key, value in data.items():
print(f"{key} = {value}")
info(name="Aman", age=22)
"Dynamic inputs ke liye yeh bahut kaam aate hain."
π Section 8: Functions are First-Class Citizens
"Python me function ko variables me store, function ke andar pass ya return bhi kar sakte hain."
Example:
def greet():
return "Hello"
msg = greet
print(msg())
π Section 9: Real-World Examples
len()
,sorted()
,input()
,print()
β sab functions hi hain- Python libraries = functions ka collection
π§ͺ Section 10: Practice Zone
Practice Questions:
- Write a function to calculate factorial.
- Write a function to reverse a string.
- Write a function to check prime number.
- Write a function to return sum of even numbers in a list.
π§βπ Outro
"Functions ka magic samajh aaya?
Ab agla part aur bhi deep jaane wala hai β Scope aur Execution Context (LEGB Rule)!
Channel ko subscribe karo, practice karo, aur comment me apna favourite concept likhna mat bhoolna! π"