Functions and Lambdas in Python
Introduction to Functions
Title | Concept | Codes |
---|---|---|
What are Functions? | Blocks of reusable code that perform a specific task. | Encapsulate logic, promote code reuse, and enhance code modularity. |
Function Syntax in Python | Define functions, arguments, and return values in Python. | Functions are defined using the def keyword with optional parameters and return statements. |
Purpose of Functions | Modularity, Reusability, and Improved Code Organization. | Functions break down complex systems into manageable components and streamline code maintenance. |
1. Defining Functions
-
Function Syntax:
-
Function Arguments:
- Positional Arguments
- Keyword Arguments
- Default Arguments
-
Variable-Length Arguments
-
Function Returns:
- Optional return statement
- Can return single or multiple values
Working with Functions
Function Parameters
Title | Concept | Codes |
---|---|---|
Positional Parameters | Arguments passed based on order. | def greet(name, message): print(f"Hello, {name}! {message}") |
Keyword Parameters | Arguments passed based on names provided. | greet(message="How are you?", name="Alice") |
Default Parameters | Predefined values for function arguments. | def greet(name, message="Good day!"): print(f"Hello, {name}! {message}") |
Variable-Length Arguments | Handle arbitrary number of arguments. | def sum_values(*args): return sum(args) |
Function Scope
Title | Concept | Codes |
---|---|---|
Global vs. Local Scope | Differentiating variable accessibility within functions. | Global variables accessible throughout, local variables limited to function scope. |
Accessing Variables in Different Scopes | Utilizing variables with specific scopes. | global_var = 10 def my_func(): local_var = 5 |
The 'global' Keyword | Modifying global variables within function scope. | def change_global_value(): global global_var global_var = 20 |
Lambda Functions
Title | Concept | Codes |
---|---|---|
Definition and Syntax | Anonymous functions defined in a single line. | lambda x, y: x + y |
Advantages of Lambda Functions | Concise, no need for def keyword, used for short functions. |
lambda x: x**2 if x > 0 else 0 |
Use Cases for Lambda Functions | Functional programming, data transformations, quick functions. | list(map(lambda x: x+2, [1, 2, 3])) |
Built-in Functions in Python
Common Built-in Functions
- print() Function
- len() Function
- range() Function
map() Function
-
Using map() with Functions
-
Lambda Functions with map()
filter() Function
-
Using filter() with Functions
-
Lambda Functions with filter()
Recursion in Python
Understanding Recursion
- Definition and Concepts
- A function calling itself
-
Breaks a problem into smaller, similar subproblems
-
Recursive Functions
- Must have a base case to terminate
- Examples: Factorial, Fibonacci
Advantages and Limitations
- Pros of Recursion
- Elegant solutions for some problems
-
Improves readability for certain algorithms
-
Common Pitfalls and Limitations
- Stack overflow with excessive recursion
- Harder to debug compared to iterative solutions
Recursive Examples
-
Writing Recursive Functions
-
Recursion vs. Iteration
- Recursion: Compact and elegant
- Iteration: Better for performance in some cases
Functional Programming Concepts
Pure Functions
- Definition and Characteristics
- Output solely determined by input
-
No side effects
-
Benefits of Pure Functions
- Easier to test and debug
- Enables parallel and concurrent execution
Higher-Order Functions
- Definition and Examples
- Functions that take other functions as arguments
-
Examples:
map()
,filter()
-
Using Functions as Parameters
- Enhances code reusability and flexibility
- Example:
sorted()
Immutability and Higher-Order Functions
- Understanding Immutability
- Data that cannot be changed after creation
-
Promotes functional programming principles
-
Applying Higher-Order Functions with Immutable Data
- Avoids unintended data changes
- Supports functional programming paradigms