0% found this document useful (0 votes)
6 views17 pages

Python Part 5

This document provides an overview of functions in Python programming, explaining their definition, usage, syntax, and various types such as functions with parameters, return statements, default arguments, and recursion. It emphasizes the benefits of using functions for code reusability and modular programming. Additionally, it includes practical examples and practice questions to reinforce learning.

Uploaded by

satyamanand638
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

Python Part 5

This document provides an overview of functions in Python programming, explaining their definition, usage, syntax, and various types such as functions with parameters, return statements, default arguments, and recursion. It emphasizes the benefits of using functions for code reusability and modular programming. Additionally, it includes practical examples and practice questions to reinforce learning.

Uploaded by

satyamanand638
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

---

🐍 PYTHON PROGRAMMING
📘 PART 5: FUNCTIONS (FULL
DETAIL + REAL PROGRAMS)

> Is part ke baad tum:

Code repeat nahi karoge ❌


Big programs easily likh paoge ✅
🚀
OOP aur advanced Python ke liye
ready ho jaoge

---

1️⃣ Function kya hota hai?

📌 Simple definition:
Function ek aisa block of code
hota hai jo koi specific kaam karta
hai
aur jab chaaho tab use call kar
sakte ho
---

🧠 Real-life example:
Calculator me add button

Fan ka switch

👉 Ek baar banaya, baar-baar use


kiya

---
2️⃣ Function kyun use karte hain?

❌ Same code baar-baar likhna


✅ Ek baar likho, baar-baar call
karo

✅ Code:
Short hota hai

Easy to understand

Easy to debug
---

3️⃣ Function ka syntax

def function_name():
code

def → define

function_name → function ka
naam

() → parameters

: → function start
Indentation compulsory

---

4️⃣ Simple Function Example

def greet():
print("Hello Python")

greet()

🧠 Explanation:
Function define hua

greet() → function call

Output print hua

---

5️⃣ Function with Parameters

📌 Jab function ko data dena ho


def greet(name):
print("Hello", name)
greet("Satyam")
greet("Aman")

👉 name = parameter
👉 "Satyam" = argument
---

6️⃣ Function with Multiple


Parameters

def add(a, b):


print(a + b)
add(10, 20)

---

7️⃣ return statement (Very Important)

📌 return kya karta hai?


Value wapas bhejta hai

Function ko end karta hai

def add(a, b):


return a + b
result = add(5, 7)
print(result)

---

8️⃣ return vs print

print​ return

Screen pe dikhata​Value deta


Temporary​Reusable
Chain nahi​Use in calculation
---

9️⃣ Default Arguments

def greet(name="Student"):
print("Hello", name)

greet()
greet("Satyam")

---

🔟 Keyword Arguments
def student(name, age):
print(name, age)

student(age=15, name="Satyam")

👉 Order matter nahi karta


---

1️⃣1️⃣ User Input with Function

def square():
n = int(input("Enter number: "))
print(n*n)
square()

---

1️⃣2️⃣ Recursion (Important Concept)

📌 Function jo khud ko call kare


Example: Factorial

def fact(n):
if n == 1:
return 1
return n * fact(n-1)
print(fact(5))

👉 5! = 120
---

1️⃣3️⃣ Lambda Function (Short


Function)

📌 One-line function
square = lambda x: x*x
print(square(5))
---

1️⃣4️⃣ Local & Global Variable

x = 10 # global

def test():
y = 5 # local
print(y)

test()

👉 Local variable function ke


bahar kaam nahi karta
---

🧪 Practice Questions (Must Do)


1️⃣ Function banao jo 2 numbers ka
sum return kare
2️⃣ User input leke even/odd check
kare
3️⃣ Function se table print karo
4️⃣ Recursive function se factorial
5️⃣ Lambda se cube find karo

---

🎯 PART 5 RESULT
Ab tum:

Modular programming kar sakte ho

Code reuse karna seekh gaye

🔥
Advanced Python ke liye ready ho

You might also like