Functions, Lambda, and Comprehensions
Functions are the building blocks of clean, reusable code. In data science, you will write functions to transform data, create features, and build pipelines. This lesson covers everything from basic definitions to advanced patterns.
Defining Functions
A function maps inputs to outputs: . In Python, define with def:
Parameters and Arguments
Positional and Keyword Arguments
*args and **kwargs
*args collects extra positional arguments as a tuple. **kwargs collects extra keyword arguments as a dictionary.
Unpacking Arguments
Return Values
Scope and Closures
Lambda Functions
Lambdas are anonymous functions: . Equivalent to .
When to Use Lambdas
map, filter, and reduce
map
map() applies a function to every element of an iterable.
filter
filter() keeps elements where the function returns True.
reduce
reduce() accumulates elements into a single value. It is in the functools module.
Comprehension Transformation Flow
List Comprehensions
List comprehensions create new lists: . Faster and more readable than loops.
Basic Syntax
Filtering
Nested Comprehensions
Practical Data Science Examples
Dictionary Comprehensions
Set Comprehensions
Generator Expressions
Generator expressions look like list comprehensions but use parentheses. They produce values lazily, which saves memory.
Function Best Practices
Key Takeaways
- Functions encapsulate logic and make code reusable.
- Use
*argsand**kwargsfor flexible function signatures. - Lambdas are best for short, one-off operations (sorting, mapping).
- List comprehensions are preferred over
map/filterfor readability. - Generator expressions save memory for large datasets.
- Always write descriptive function names and document with docstrings.