Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use if, else & elif in Python Lambda Functions
Python lambda functions are anonymous, single-line functions that provide a concise way to define small operations. When combined with conditional statements like if, else, and elif, they become powerful tools for inline decision-making logic.
In this tutorial, we will explore how to incorporate conditional statements within lambda functions, demonstrating their syntax and practical applications through working examples.
Basic Syntax
A lambda function with conditional statements follows this general structure ?
lambda arguments: expression_if_true if condition else expression_if_false
The key components are ?
lambda ? keyword to define the anonymous function
arguments ? input parameters for the function
condition ? logical expression that evaluates to True or False
expressions ? values returned based on the condition result
Simple If-Else Example
Let's create a lambda function that determines if a number is even or odd ?
check_even = lambda x: "Even" if x % 2 == 0 else "Odd" print(check_even(4)) print(check_even(7)) print(check_even(0))
Even Odd Even
Multiple Conditions Using Chained If-Else
For multiple conditions, we can chain if-else statements to simulate elif behavior ?
get_grade = lambda score: "A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "D" if score >= 60 else "F" print(get_grade(95)) print(get_grade(85)) print(get_grade(75)) print(get_grade(65)) print(get_grade(45))
A B C D F
Nested Conditional Logic
Lambda functions can handle nested conditions for more complex decision-making ?
categorize_number = lambda num: "Positive" if num > 0 else "Negative" if num < 0 else "Zero" print(categorize_number(5)) print(categorize_number(-2)) print(categorize_number(0))
Positive Negative Zero
Practical Applications
Lambda functions with conditionals are commonly used with built-in functions like map(), filter(), and sorted() ?
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Using with map() to categorize numbers categories = list(map(lambda x: "Small" if x <= 3 else "Medium" if x <= 7 else "Large", numbers)) print(categories) # Using with filter() to get specific values filtered_nums = list(filter(lambda x: "Even" if x % 2 == 0 else None, numbers)) print([x for x in numbers if (lambda y: True if y % 2 == 0 else False)(x)])
['Small', 'Small', 'Small', 'Medium', 'Medium', 'Medium', 'Medium', 'Large', 'Large', 'Large'] [2, 4, 6, 8, 10]
Comparison of Approaches
| Approach | Readability | Best For |
|---|---|---|
| Simple if-else | High | Binary conditions |
| Chained if-else | Medium | Multiple exclusive conditions |
| Nested conditions | Lower | Complex hierarchical logic |
Conclusion
Lambda functions with conditional statements provide a powerful way to create concise, inline decision-making logic in Python. While they excel at simple conditions, complex logic may be better suited to regular functions for improved readability.
