Functions and Modules in Python
# 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
defkeyword.
-
Add parentheses
()and a colon:.
- Indent the code block.
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.
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).
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.
6. Importing Modules
Python comes with a "Standard Library" of modules (files containing pre-written functions). To use them, you must import them.
7. Mini Project: Data Cleaner Utility
Let's build a reusable module (function) to clean messy price data imported from a CSV.
8. Common Mistakes
-
Forgetting
return: If your function calculates a value but doesn't have areturnstatement, it returnsNoneby default. You cannot do math onNone.
-
Modifying Global Variables: It is bad practice to change global variables from inside a function. Always pass data in as parameters and
returnthe result.
9. MCQs
What keyword is used to define a standard function in Python?
What is the purpose of the return statement?
If a function is defined as def calc(a, b=5):, what happens if you call calc(10)?
What is a variable called if it is created inside a function and cannot be accessed outside it?
What keyword creates a small, one-line anonymous function?
Which statement imports the math module and nicknames it m?
What does the docstring (""" text """) directly beneath a function definition do?
If a function does NOT have a return statement, what does it evaluate to?
How do you import *only* the sqrt function from the math module?
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
lambdafunction is and give a scenario where you might use one over a standarddeffunction.
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.