PYTHON BASICS • LESSON 16
Python Functions
Learn how to create reusable blocks of Python code using functions. You will work with parameters, arguments, return values, default parameters, and practical data analytics examples.
1. What is a Function?
A function is a reusable block of code that performs a specific task. Instead of writing the same code repeatedly, we can put it inside a function and call that function whenever we need it.
def greet():
print("Hello!")
greet()The def keyword is used to define a function.
CHECK YOUR UNDERSTANDING
Which keyword is used to define a function in Python?
2. Defining and Calling a Function
Defining a function does not automatically execute it. You must call the function.
def show_message():
print("Data Analytics")
show_message()Here show_message() calls the function.
CHECK YOUR UNDERSTANDING
What happens when show_message() is written after the function definition?
3. Predict the Output
def welcome():
print("Welcome to Python")
welcome()
welcome()How many times will the message be printed?
CHECK YOUR UNDERSTANDING
How many times will the message be printed?
4. Parameters and Arguments
Functions can receive information through parameters.
def greet(name):
print("Hello", name)
greet("Rahul")name is the parameter. "Rahul" is the argument passed to the function.
CHECK YOUR UNDERSTANDING
In greet("Rahul"), what is "Rahul"?
Complete the function definition: ___ calculate_total(price, quantity):
5. Multiple Parameters
A function can accept more than one parameter.
def calculate_total(price, quantity):
return price * quantity
total = calculate_total(500, 4)
print(total)The function receives both price and quantity.
CHECK YOUR UNDERSTANDING
What is the output of calculate_total(500, 4)?
6. Return Values
The return statement sends a result back from a function.
def add(a, b):
return a + b
result = add(10, 20)
print(result)The returned value can be stored in a variable and used in another calculation.
CHECK YOUR UNDERSTANDING
Which keyword sends a value back from a function?
CHECK YOUR UNDERSTANDING
What is the result of this function? def double(x): return x * 2 ; double(15)
Complete the function: def square(x): ___ x * x
7. Default Parameters
A parameter can have a default value.
def greet(name="Student"):
print("Hello", name)
greet()
greet("Aman")If no argument is supplied, Python uses the default value.
CHECK YOUR UNDERSTANDING
What will greet() print when using the function above?
8. Local Variables
Variables created inside a function normally belong to that function. They are called local variables.
def calculate():
total = 500
return total
print(calculate())The variable total is created inside the function.
9. Debugging Functions
Find the problem:
def calculate_total(price, quantity): return price * quantity print(calculate_total(100, 5))
The function body must be indented.
CHECK YOUR UNDERSTANDING
What is wrong with the function above?
10. Functions in Data Analytics
Functions are extremely useful in analytics because the same calculation may need to be performed many times.
def calculate_revenue(price, quantity):
return price * quantity
laptop_revenue = calculate_revenue(50000, 3)
phone_revenue = calculate_revenue(20000, 5)
print(laptop_revenue)
print(phone_revenue)Instead of rewriting the revenue calculation for every product, we reuse the same function.
CHECK YOUR UNDERSTANDING
What is the laptop revenue in the example?
Complete: def calculate_profit(revenue, cost): ___ revenue - cost
CHECK YOUR UNDERSTANDING
What is the output? def profit(revenue, cost): return revenue - cost ; print(profit(100000, 65000))
11. return vs print
print() displays a value. return sends a value back so the calling code can use it.
def add(a, b):
return a + b
result = add(10, 20)
final_result = result * 2
print(final_result)CHECK YOUR UNDERSTANDING
Why is return useful in analytics functions?
A function is defined using the ___ keyword.
MINI CHALLENGE
Build a Discount Function
You have a product price of ₹10,000 and want to calculate a 10% discount.
def discount(price):
return price * 0.10What will discount(10000) return?
CHECK YOUR UNDERSTANDING
What will discount(10000) return?
FINAL CHALLENGE
Create a Sales Performance Function
A sales analyst wants a reusable function that calculates profit from revenue and cost.
def calculate_profit(revenue, cost):
return revenue - costWhat is the profit when revenue is ₹250,000 and cost is ₹175,000?
CHECK YOUR UNDERSTANDING
What is the profit?
Lesson 16 Recap
- ✓ Functions are reusable blocks of code.
- ✓ Use def to define a function.
- ✓ Call a function using parentheses.
- ✓ Parameters receive information.
- ✓ Arguments are values passed to parameters.
- ✓ return sends a result back.
- ✓ Default parameters provide fallback values.
- ✓ Functions make analytics calculations reusable.