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:
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.
After running the aforementioned program, we obtain 7 as a result, as seen below:
You can see that we used a subtraction mark instead of an addition symbol in this example:
As you can see in the following output, we obtained 30:
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.
Because 10 is multiplied by 4 in the preceding code, the outcome is 40, as seen below:
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:
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.
For your convenience, we have provided the following output for your consideration.
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:
number = int(input('Please provide any number: '))
res = check_num(number)
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:
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”.
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.
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.
The previous code gave the following results: 8, 27, and 50.
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.