Skip to main content
Python for Data Science
CHAPTER 06 Beginner

Functions and Modules in Python

Updated: May 18, 2026
5 min read

# CHAPTER 6

Functions and Modules in Python

1. Chapter Introduction

If you are analyzing data from 5 different cities, you shouldn't write the same cleaning code 5 times. In programming, the DRY principle (Don't Repeat Yourself) is critical. This chapter teaches you how to encapsulate code into reusable "Functions", how to use compact "Lambda" functions, and how to import "Modules" (libraries of code written by others) to expand Python's capabilities.

2. Defining a Function

A function is a named block of code that only runs when it is called. You can pass data (parameters) into it, and it can send data back (return values).

Syntax:

  • Use the def keyword.
  • Add parentheses () and a colon :.
  • Indent the code block.

python
12345678
# 1. Define the function
def greet_user(name):
    """This is a docstring. It documents what the function does."""
    print(f"Hello, {name}! Welcome to the Data Portal.")

# 2. Call the function
greet_user("Alice")
greet_user("Bob")

3. Return Values and Default Parameters

Often, you don't want a function to just print something; you want it to calculate a value and hand it back to you so you can store it in a variable. We use return for this.

You can also assign default values to parameters, making them optional.

python
12345678910111213
# tax_rate has a default value of 0.05
def calculate_total(price, tax_rate=0.05):
    tax_amount = price * tax_rate
    final_price = price + tax_amount
    return final_price # Hands the data back

# Using default tax (5%)
order_one = calculate_total(100)
print(order_one) # 105.0

# Overriding the default tax (e.g., 20%)
order_two = calculate_total(100, 0.20)
print(order_two) # 120.0

4. Scope: Local vs. Global Variables

Variables created *inside* a function only exist inside that function (Local scope). Variables created *outside* exist everywhere (Global scope).

python
123456789
global_var = "I am everywhere"

def my_func():
    local_var = "I only exist inside this function"
    print(global_var) # This works
    print(local_var)  # This works

my_func()
# print(local_var) # ERROR! local_var does not exist out here.

5. Lambda Functions (Anonymous Functions)

In Data Science, especially with Pandas, you often need to write a tiny function just for one specific mathematical operation. Instead of a full def block, Python offers Lambda functions—quick, one-line, anonymous functions.

python
123456789
# Standard Function
def double(x):
    return x * 2

# Lambda Function equivalent
# Syntax: lambda arguments: expression
double_lambda = lambda x: x * 2

print(double_lambda(10)) # Output: 20

6. Importing Modules

Python comes with a "Standard Library" of modules (files containing pre-written functions). To use them, you must import them.

python
123456789101112
# Import the whole module
import math
print(math.sqrt(64)) # Output: 8.0

# Import with an alias (Very common in data science)
import statistics as stats
data = [10, 20, 30]
print(stats.mean(data)) # Output: 20

# Import only a specific function
from random import randint
print(randint(1, 10)) # Random number between 1 and 10

7. Mini Project: Data Cleaner Utility

Let's build a reusable module (function) to clean messy price data imported from a CSV.

python
123456789101112131415161718192021
def clean_price(price_str):
    """
    Takes a messy string like '$1,250.99' and returns a clean float 1250.99
    """
    if type(price_str) != str:
        return price_str # Return as-is if it's already a number
        
    # Remove $ and commas
    clean_str = price_str.replace('$', '').replace(',', '')
    
    # Cast to float
    return float(clean_str)

# Test our utility
messy_data = ["$1,500.00", "$45.99", "Free"]

for item in messy_data:
    if item == "Free":
        print(0.0)
    else:
        print(clean_price(item))

8. Common Mistakes

  • Forgetting return: If your function calculates a value but doesn't have a return statement, it returns None by default. You cannot do math on None.
  • Modifying Global Variables: It is bad practice to change global variables from inside a function. Always pass data in as parameters and return the result.

9. MCQs

Question 1

What keyword is used to define a standard function in Python?

Question 2

What is the purpose of the return statement?

Question 3

If a function is defined as def calc(a, b=5):, what happens if you call calc(10)?

Question 4

What is a variable called if it is created inside a function and cannot be accessed outside it?

Question 5

What keyword creates a small, one-line anonymous function?

Question 6

Which statement imports the math module and nicknames it m?

Question 7

What does the docstring (""" text """) directly beneath a function definition do?

Question 8

If a function does NOT have a return statement, what does it evaluate to?

Question 9

How do you import *only* the sqrt function from the math module?

Question 10

Why use functions in data science?

10. Interview Questions

  • Q: What is the difference between a Local and a Global variable? Why should you avoid modifying global variables inside functions?
  • Q: Explain what a lambda function is and give a scenario where you might use one over a standard def function.

11. Summary

Functions (def) allow you to write a block of code once and use it infinitely. They accept inputs via Parameters and output data via return. lambda functions provide a shorthand for simple one-line operations. Finally, you can expand Python's capabilities by utilizing import statements to bring in external Modules containing pre-written functions.

12. Next Chapter Recommendation

In Chapter 7: Working with Strings and Lists, we will explore how to manipulate text data (splitting, replacing) and how to store and access collections of items using Lists and List Comprehensions.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·