Unit-3
Unit-3
✅ 1. Functions in Python
🔎 Example:
def greet(name):
print(f"Hello, {name}!")
✅ 2. Advantages of Functions
✅ 3. Built-in Functions
● Example:
print(len("Python")) # Output: 6
✅ 4. User-Defined Functions
You can define your own functions using the def keyword.
🔎 Example:
🔎 Example:
square = lambda x: x * x
print(square(5)) # Output: 25
🔎 Example:
def modify_value(x):
x = 10
print("Inside function:", x)
a=5
modify_value(a)
def modify_list(lst):
lst.append(100)
print("Inside function:", lst)
my_list = [1, 2, 3]
modify_list(my_list)
🟢 Recursion in Python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
🔎 Example:
x = 10 # Global variable
def my_function():
y = 5 # Local variable
print("Inside function:", x, y)
my_function()
print("Outside function:", x)
🟢 Python Modules
✅ 1. What is a Module?
🔎 Example:
def greet(name):
import my_module
● Easier maintenance.
python
CopyEdit
import math
🔎 Example:
python
CopyEdit
import sys
print(sys.path)
✅ 5. Module Reloading
python
CopyEdit
import importlib
importlib.reload(my_module)
🟢 Python Packages
markdown
CopyEdit
my_package/
├── __init__.py
├── module1.py
└── module2.py
🔎 Example:
python
CopyEdit
# Inside module1.py
def say_hello():