Functions & Lambdas
Functions & Lambdas in Python
Section titled “Functions & Lambdas in Python”1. Functions in Python (def)
Section titled “1. Functions in Python (def)”A function in Python is a reusable block of code that performs a specific task.
It is defined using the def keyword and can accept parameters and return a value.
General Syntax
Section titled “General Syntax”def function_name(parameters): # logic return resultFunctions help in:
- Reusability
- Better readability
- Avoiding duplicate code
- Encapsulating logic
2. Example Function Explained
Section titled “2. Example Function Explained”def greet(name, exclaim=False): ending = "!" if exclaim else "." return f"Hello {name}{ending}"Breakdown
Section titled “Breakdown”Function Definition
Section titled “Function Definition”def greet(name, exclaim=False):greet→ function namename→ required parameterexclaim=False→ optional parameter with a default value
This is equivalent to a default parameter in C#:
void Greet(string name, bool exclaim = false)3. What is exclaim?
Section titled “3. What is exclaim?”exclaim is a boolean flag that:
- Controls the behavior of the function
- Acts like an ON/OFF switch
| Value | Meaning |
|---|---|
False (default) | Normal greeting |
True | Excited greeting |
4. Conditional (Ternary) Expression
Section titled “4. Conditional (Ternary) Expression”ending = "!" if exclaim else "."Equivalent long form:
if exclaim: ending = "!"else: ending = "."5. Return Statement & f-Strings
Section titled “5. Return Statement & f-Strings”return f"Hello {name}{ending}"returnsends the result back to the callerf"..."is an f-string (string interpolation){name}and{ending}insert variable values
6. Function Usage Examples
Section titled “6. Function Usage Examples”greet("Banky")Output:
Hello Banky.greet("Banky", True)Output:
Hello Banky!7. Lambda Functions (Anonymous Functions)
Section titled “7. Lambda Functions (Anonymous Functions)”A lambda function is:
- A small, one-line function
- Anonymous (no name)
- Used for simple logic
- Automatically returns a value
Syntax
Section titled “Syntax”lambda arguments: expression8. Lambda Example Explained
Section titled “8. Lambda Example Explained”adder = lambda a, b: a + bEquivalent regular function:
def adder(a, b): return a + bEquivalent C# lambda:
Func<int, int, int> adder = (a, b) => a + b;Equivalent JavaScript arrow function:
const add = (a, b) => a + b;9. Lambda Usage
Section titled “9. Lambda Usage”adder(3, 5)Output:
810. def vs lambda
Section titled “10. def vs lambda”| Scenario | def | lambda |
|---|---|---|
| Multi-line logic | ✅ | ❌ |
| Complex logic | ✅ | ❌ |
| Readability | ✅ | ⚠️ |
| One-line logic | ⚠️ | ✅ |
| map / filter usage | ⚠️ | ✅ |
11. Python vs C# Default Parameters
Section titled “11. Python vs C# Default Parameters”| Aspect | Python | C# |
|---|---|---|
| Default parameters | Yes | Yes |
| Evaluation time | Function definition | Compile time |
| Mutable defaults | ⚠️ Avoid | Safe |
| Boolean flags | Common | Common |
Note: Default parameters are safe for immutable types like
bool,int, andstring.
12. Practice Questions
Section titled “12. Practice Questions”-
How do you give a parameter a default value in Python?
Answer
Assign it in the function definition: `def f(x=0):` -
Name the JavaScript feature similar to Python’s lambda.
Answer
Arrow functions. -
What is the difference between
defandlambda?Answer
`def` is used for multi-line, named functions with complex logic, while `lambda` is for short, anonymous, one-line expressions.
13. Final Summary
Section titled “13. Final Summary”defdefines reusable functions in Python- Default parameters work the same as C# optional parameters
exclaimis a boolean flag controlling behaviorlambdacreates small, one-line anonymous functions- Use
deffor clarity and complex logic - Use
lambdafor short, simple expressions