Use the exp1 if cond else exp2 syntax.

rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)

Note you don't use return in lambda expressions.

Answer from shx2 on Stack Overflow
Top answer
1 of 8
120

Use the exp1 if cond else exp2 syntax.

rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)

Note you don't use return in lambda expressions.

2 of 8
28

The right way to do this is simple:

def rate(T):
    if (T > 200):
        return 200*exp(-T)
    else:
        return 400*exp(-T)

There is absolutely no advantage to using lambda here. The only thing lambda is good for is allowing you to create anonymous functions and use them in an expression (as opposed to a statement). If you immediately assign the lambda to a variable, it's no longer anonymous, and it's used in a statement, so you're just making your code less readable for no reason.

The rate function defined this way can be stored in an array, passed around, called, etc. in exactly the same way a lambda function could. It'll be exactly the same (except a bit easier to debug, introspect, etc.).


From a comment:

Well the function needed to fit in one line, which i didn't think you could do with a named function?

I can't imagine any good reason why the function would ever need to fit in one line. But sure, you can do that with a named function. Try this in your interpreter:

>>> def foo(x): return x + 1

Also these functions are stored as strings which are then evaluated using "eval" which i wasn't sure how to do with regular functions.

Again, while it's hard to be 100% sure without any clue as to why why you're doing this, I'm at least 99% sure that you have no reason or a bad reason for this. Almost any time you think you want to pass Python functions around as strings and call eval so you can use them, you actually just want to pass Python functions around as functions and use them as functions.

But on the off chance that this really is what you need here: Just use exec instead of eval.

You didn't mention which version of Python you're using. In 3.x, the exec function has the exact same signature as the eval function:

exec(my_function_string, my_globals, my_locals)

In 2.7, exec is a statement, not a function—but you can still write it in the same syntax as in 3.x (as long as you don't try to assign the return value to anything) and it works.

In earlier 2.x (before 2.6, I think?) you have to do it like this instead:

exec my_function_string in my_globals, my_locals
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Expression (x**2): The operation performed on the argument and returned as the result. A lambda function can use conditional expressions (if-else) to return different results based on a condition.
Published   4 days ago
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-if-else-elif-in-python-lambda-functions
How to use if, else & elif in Python Lambda Functions - GeeksforGeeks
July 23, 2025 - The lambda function will return a value for every validated input. Here, if block will be returned when the condition is true, and else block will be returned when the condition is false.
🌐
AskPython
askpython.com › home › lambda with conditional statements in python
Lambda With Conditional Statements in Python - AskPython
October 6, 2023 - Every programmer has to learn these and then proceed towards advanced topics. In almost all languages there are only three conditionals: ... The lambda function in Python is an anonymous function.
🌐
DigitalOcean
digitalocean.com › community › tutorials › lambda-expression-python
Python Lambda Expressions Explained with Examples | DigitalOcean
July 8, 2025 - The returned lambda function uses ... functions in Python. Conditional lambda functions in Python are lambda functions that use conditional statements to determine the output based on certain conditions....
🌐
Python Examples
pythonexamples.org › python-lambda-if-else
Python Lambda - If Else, Nested If Else
You can write an if else or nested if else inside Python lambda function, to conditionally choose values to be returned. In this tutorial, we write Python Lambda Functions with If Else and Nested If Else.
🌐
Dataquest
dataquest.io › blog › tutorial-lambda-functions-in-python
Tutorial: Lambda Functions in Python
March 6, 2023 - We use a lambda function to evaluate only one short expression (ideally, a single-line) and only once, meaning that we aren't going to apply this function later. Usually, we pass a lambda function as an argument to a higher-order function (the ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › lambda-with-if-but-without-else-in-python
Lambda with if but without else in Python - GeeksforGeeks
October 6, 2021 - It must have a return value. Since a lambda function must have a return value for every valid input, we cannot define it with if but without else as we are not specifying what will we return if the if-condition will be false i.e.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-use-if-else-amp-elif-in-python-lambda-functions
How to use if, else & elif in Python Lambda Functions
July 25, 2023 - In this tutorial, we will explore how to incorporate conditional statements within lambda functions, demonstrating their syntax and practical applications through working examples.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
This function returns the sum of its two arguments: lambda a, b: a+b. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python lambda using if else
Python Lambda using if else - Spark By {Examples}
May 31, 2024 - # Syntax lambda arguments,[argument,arguments] : <statement1> if <condition> else <statement2> Here, statement1 is returned when if condition is True and statement2 when if condition is False. You can use the if-else statement as part of the Python lambda expression.
🌐
Linux Hint
linuxhint.com › python-lambda-with-conditional
Python Lambda With Conditional
April 16, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... A lambda function is a small anonymous function....
🌐
Kanoki
kanoki.org › 2022 › 09 › 12 › python-lambda-if-else-elif-with-multiple-conditions-and-filter-list-with-conditions-in-lambda
python lambda if, else & elif with multiple conditions and filter list with conditions in lambda | kanoki
September 12, 2022 - We could also use lambda to loop over a list and evaluate a condition · Let’s define a function with all the conditions that we want to evaluate · def f(grade): if grade > 7: return 'A+' elif grade >= 5 and grade <= 7: return 'A' elif grade <5: return 'B' else: return False ... DataFrames are a powerful tool for working with data in Python, and Pandas provides a number of ways to count duplicate rows in a DataFrame.
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - In this step-by-step tutorial, you'll learn about Python lambda functions. You'll see how they compare with regular functions and how you can use them in accordance with best practices.
🌐
TutorialsPoint
tutorialspoint.com › lambda-with-if-but-without-else-in-python
Lambda with if but without else in Python
July 17, 2023 - In a lambda function, we can typically utilise if-else conditional statements like follows ? check_even = lambda x: True if x % 2 == 0 else False print(check_even(4)) ... The 'otherwise' clause may not always be necessary when using a 'if' statement inside of a lambda function, though.
🌐
Delft Stack
delftstack.com › home › howto › python › python lambda if
if...else in Lambda Function Python | Delft Stack
February 25, 2025 - A lambda function is a small anonymous ... lambda functions have multiple if statements? Yes, you can nest if statements within a lambda function to handle multiple conditions....
🌐
DataCamp
datacamp.com › tutorial › python-lambda-functions
Python Lambda Functions: A Beginner’s Guide | DataCamp
January 31, 2025 - In this code, a trick is used to ... function in filter includes a tuple: the first element is a print statement that logs the current number and whether it satisfies the condition (x >= 3), and the second element is the condition itself....