Functions are very important in data science. They help you write reusable, clean, and modular code.
๐น 1. What is a Function?
A function is a block of code that performs a specific task.
๐ Instead of writing the same code again and again, we create a function.
๐ฅ 2. Creating a Function
โ Basic Syntax
def function_name():
# code
โ Example
def greet():
print("Hello Deepak")
greet()
Output: Hello Deepak
๐น 3. Function with Parameters
Parameters allow input to functions.
def greet(name):
print("Hello", name)
greet("Rahul")
# Output: Hello Rahul
๐น 4. Function with Return Value (Very Important โญ)
Instead of printing, functions can return values.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
# Output: 8
๐ return sends value back.
๐น 5. Default Parameters
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Amit")
๐น 6. Why Functions Matter in Data Science?
โ Data cleaning functions
โ Feature engineering functions
โ Reusable ML pipelines
โ Code organization
๐ฏ Todayโs Goal
โ Understand def
โ Use parameters
โ Use return
โ Call functions properly
Double Tap โฅ๏ธ For More