Less Than or Equal to Operator (<=)
Either True or False is returned by the <= operator. When the left operand is less than or equal to the right operand, it returns “True”. It also returns true if the left and the right operand equals each other. And “False” in cases when the left side value is not less than the right-side value; in simple words, the program returns False. For example, 5=3 and evaluates to False but 3<=4, and 3=3 is True.
Syntax of Python Less Than or Equal
Here is the syntax:
A Boolean value is returned by the <= operator. “True” if the value of operand 1 is lower than or equal to that of operand 2. Otherwise, False is returned. The result is computed by comparing the relevant elements of the objects if the operands are sequences such as strings, lists, tuples, etc.
Sequences are compared for each of their elements until one of them receives a False result from the comparison, or the sequence reaches its conclusion with all True results.
As demonstrated below, the compound expression less than or equal to is created using the operators less than and equal to.
To further grasp how this comparison operator functions, let’s concentrate on a few examples.
Example 1
You’ll notice in this example that the operator only returns True if the value on the left is either lower than or equal to the value on the operator’s right. The code below explains what “=” in Python means. When we print, “True” will be displayed in the result. Please notice that the number 22 is less than the number 35 in this scenario. The output is thus returned as True. For further information on what = in Python means, see the screenshot below.
two = 35
print(one <= two)
Since 22 is less than 35, you can see that the program returns “True” in this case.
Example 2
Here is another example in which we are going to make multiple comparisons. Firstly, we have created four variables which are “NumOne”, “NumTwo”, “NumThree”, and “NumFour”, and these contain 22, 22, 20, and 6 values.
After that, we compared the first number with the third number (NumOne <= NumTwo) then the third number was compared with the first number (NumThree <= Num
One). Finally, the first number is compared with the fourth number (NumOne <= NumFour).
In the last section of the code, you can see that the original numbers and the comparison result are displayed in an easy-to-understand format.
NumTwo = 22
NumThree = 20
NumFour = 6
first_comparison = NumOne <= NumTwo
second_comparison = NumThree <= NumOne
third_comparison = NumOne <= NumFour
print("{} is less than or equal to {}?: {}".format(NumOne, NumTwo, first_comparison))
print("{} is less than or equal to {}?: {}".format(NumThree, NumOne, second_comparison))
print("{} is less than or equal to {}?: {}".format(NumOne, NumFour, third_comparison))
Here is the result in the True and False format.
Example 3
The less than or equal to the operator with sequences is illustrated in this example.
The operator compares the corresponding items from the two sequences iteratively when dealing with sequences. Up until they receive a False result from comparison, or the conclusion of the sequence is reached with all True results from comparisons, all of the corresponding elements from the two sequences are subject to comparison.
The following program will compare four lists—a, b, c, and d—and determine whether and is less than or equal to each of the other three.
Checking if [22, 34, 21] = [77, 9] implies determining whether [a,=b]. Less than or Equal to returns True when you are comparing the first entry of the lists.
For a = c, this entails determining whether [22, 34, 21] = [21, 63, 2, 1]. The less than or equal to the operator in a Python program returns True when the first two items are compared. As a result, the operator keeps looking until it finds a list’s end, where all of the elements are True, or until it finds a False in the middle. The operator gives False as a result for the third element. Now that the comparison has been stopped, the operator returns False. And it is obvious from the data that the operator returns False for the condition a = d.
b = [77, 9]
c = [21, 63, 2, 1]
d = [12, 24, 88]
print(a <= b)
print(a <= c)
print(a <= d)
The aforementioned code produced the following results:
Example 4
The Python less than or equal to the if statement is used in this example. In an, if statement, the less than or equal to the operator can be used as an expression. It is done to decide whether to execute the if a section of the code. For instance, the if section is entered if the condition age=15 determines whether the value of the variable “age” is less than or equal to 15.
The user is prompted to enter their age using the input() function in the following code. It then determines if the user input is less than or equal to 15 after converting it to an integer using the int() function. If so, the if branch is reached. Otherwise, it moves to the else branch.
if age <= 15:
print('Not Eligible')
else:
print('Eligible')
Here is an example of how to use this code, where the number 22 is input:
Here is an example of how the condition is not met during execution.
Conclusion
In Python, there are many kinds of operators, including arithmetic, comparison, and bitwise operators. The less than or equal to ( number <= number ) comparison operator was the subject of this article. Binary operators, known as comparison operators, are employed in programs to compare two items. As they establish the relationship between two objects in Python, these are also known as relational operators. We have utilized the less than or equal operator to alter the data and control the order of execution in our example programs.