Python

Python Lambda With Conditional

Lambda functions are anonymous functions in Python, which implies they don’t have a name. It accepts an unlimited number of arguments but only evaluates and returns one expression. A return value is required. We can’t build a lambda function using if but not else, since we’re not declaring what we’ll return if the if-condition is false, i.e., it’s the else part. When you need more powerful anonymous functions, combining both might be quite handy.

What Are Conditional Statements?

Conditional statements are one of the most important topics to master in any programming language. Their principal responsibility is to act and follow the conditions. Every programmer must first master these concepts before moving on to more sophisticated topics. There are only three conditionals in practically all languages: if, else, and else if statements.

Conditional statements help you make decisions that are based on a set of circumstances. These conditions are specified with the help of conditional statements that contain Boolean expressions (true or false).

We can do a variety of functions with lambdas. These functions include addition, subtraction, multiplication, and division.

Now, you can explore a few examples of lambda functions. First, we’ll go over some programming examples of simple mathematical operations. Then, we’ll go over some conditional examples:

Example 1:

In our first example, the code is straightforward. We start by creating a lambda object called “add_res”. The lambda expression is used to store two parameters. These are two numbers that will be added together in this parameter. After that, we put the additional phrase in front of the colon in the following code.

add_res = lambda one, two: one + two

print(add_res(3, 4))

After running the aforementioned program, we obtain 7 as a result, as seen below:

Example 2:

You can see that we used a subtraction mark instead of an addition symbol in this example:

sub_res = lambda one, two: one - two

print(sub_res(70, 40))

As you can see in the following output, we obtained 30:

Example 3:

Now, we conduct a multiplication operation on two numbers. We performed the multiplication operation on two variables in the first line of code, wherein one and two are the variable names. After that, we used the print command to print the output after passing the values for both variables.

multiply_res = lambda one, two: one * two

print(multiply_res(10, 4))

Because 10 is multiplied by 4 in the preceding code, the outcome is 40, as seen below:

Example 4:

We will employ the lambda with if-else conditions in this and the following examples. It’s a good idea to use the lambda with if-else situations. Note that in lambdas, the expression section can only carry one expression at a time. When using lambda with if-else conditions in Python, here is the following general syntax:

name_of_variable = lambda parameters : if_code if (condition) else else_code

The syntax is slightly different from lambda’s core implementation. Simply declare the code before the if statement, then complete the part of the if statement by mentioning the condition. If the necessity arises, the else block is immediately followed by the if statement.

As lambda_example_conditional, we build a lambda object in this example. Then, we save a variable “a” and an expression as a/10 from which our conditional statement is derived. If variable “a” is less than 20, the written code will divide it by 10. Otherwise, it will print the variable value.

The lambda_example_conditional function is then called, and the parameter is set to 2. Because 2 is less than 20, it will be divided by 100, resulting in a screen output of 0.2.

lambda_example_conditional = lambda a: a/10 if a < 20 else a

print(lambda_example_conditional(2))

For your convenience, we have provided the following output for your consideration.

Example 5:

Now, we’ll look at how to use the other advanced operations using the previous example code. In this instance, we’ll use lambdas to define whether a specified integer is odd or even:

check_num = lambda number : print(number, 'is Even') if number%2 == 0 else print(number, ' is Odd')

number = int(input('Please provide any number: '))

res = check_num(number)

print(res)

The following text will appear on the screen after running the given code. You will be encouraged to enter any number. Once you do so, the output will be revealed.

The following output demonstrates that the specified number is odd:

Example 6:

Here’s another example of using lambdas to determine whether a given number is even or odd. We begin by defining the lambda statement. Next, “one” and “two” are the parameters for the function, while check_num is the name of the function.

Then, there’s the if block. The essential requirement is that “one” must be divisible by “two”. If this is correct, the block will print “one is divided by two”. The else part of the code will display “one is indivisible by two” if the remainder is not equal to zero. Then, we build a “res” function object in which we have passed the function that we have created in the first line of code along with the variables “one” and “two”.

check_num = lambda one, two : print(one,'is divisible by', two) if (one%two == 0) else print(one ,' is indivisible by ', two)

one = int(input('Enter first value: '))

two = int(input('Enter second value: '))

res = check_num(one, two)

Here’s the result: the number 4 is given, and the function returns that it’s indivisible by 5.

Example 7:

In the last example, we’ll employ a lambda function with nested if-else conditions. We’ll construct a lambda function that returns the number if it’s divisible by 10, the number’s square if it’s even, and the number’s cube if it’s odd.

res = lambda num: num if num%2 == 0 else ( num**2 if num%2 == 0 else num**3 )

print(res(8))

print(res(3))

print(res(50))

The previous code gave the following results: 8, 27, and 50.

Conclusion:

In this post, we looked at how to employ if, else, and else if in a lambda function in Python. Furthermore, we have explained all of the operations with several examples for your convenience. You can execute these programs and see the output yourself to better understand the overall concept. We hope you found this article helpful. Check the other Linux Hint articles for more tips and articles.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content