Comparision Operator in Python part 20

Comparision Operator in Python part   20







Python comparative operator with syntax and examples
BY DATAFLAIR TEAM · Published Jnanpith 15, 2018 · Updated NOVEMBER 21, 2018

1. Python Comparison Operator
In our previous article, we talked about Python bitcoin operators. Today, we focus our words on Python comparative operators. They are also called relational operators in Python. In addition, we will learn different types of comparative handlers in Python: less, greater, greater than, equal to, and no compared with their syntax and examples.

So, let's start the Python comparison operators tutorial.

Python comparative operator with syntax and examples
Python comparative operator with syntax and examples

2. Python Comparative Operator
A comparison operator in Python, also called the Python relational operator, compares the values ​​of the two operands and determines whether the condition is met or not. We have six of these, including and limited to- less than, greater than, less than or equal to, greater than or equal to, equal to and not equal to. So, let's start with the Python comparison operators.

3. Python Les Than (<) operator
The first comparison operator we see here in Python is less than the operator. Rejected by <, it checks if the left value is less than the right.

>>> 3 <6
Output: True

Since 3 is less than 6, it is True.

>>> 3 <3
Output: False

Because 3 is equal to 3, and is not less than this, it returns false.

Recommended Reading - Python Operator Priority

But let's see if we can apply it to values ​​other than forts.

>>> 3 <3.0
Output: False

Here, 3 is an int, and 3.0 is a float, but 3 is not less than 3.0, or vice versa.

>>> 3.0 <3
Output: False

Now, try it on the string.

>>> 'Ayushi' <'Ayushi'
Output: True

This is true because when comparing strings, their ASCII values ​​are compared. The ASCII value of 'A' is 65, but 97 for 'a'. Therefore, er A is less than "a". Similarly, 'Ayushi' is less than 'Ayushi'.

But does it work with Python Booleans?

>>> 0.9999999 <true
Output: True

Yes it does. But what's fascinating is that it also works on containers like tuples. Let's look at some of these.

>>> (1,2,3) <(1,2,3,4)
Output: True

>>> (1,3,2) <(1,2,3)
Output: False

>>> (1,2,3) <(1,3,2)
Output: True

>>> () <(0,)
Output: True

But you cannot compare tulle with different types of values.

>>> (1,2) <('one', 'two')
Traceback (most recent call last):

In file "<pyshell # 84>", line 1, <module>

(1,2) <('one', 'two')

TypeError: and <of 'not supported between instances of int' and 'str'

However, if you find comparable elements in the same indices, it is possible to compare the two tuples.

Must Check - Python Tuple Vs List

>>> (1, 'one') <(2, 'two')
Output: True

And when we say the same index, we mean it.

>>> (1, 'one') <('two', 2)
Traceback (most recent call last):

In file "<pyshell # 86>", line 1, <module>

(1, 'one') <('two', 2)

TypeError: and <of 'not supported between instances of int' and 'str'

Finally, these work on lists, and sets, but not dictionaries.

>>> [0] <[wrong]
Output: False

>>> {1,2,3} <{1,3,2}
Output: False

Here, because the second set rearranges itself to {1,2,3}, the two sets are the same. As a result, it returns false.

>>> {1: 'one', 2: 'two'} <{1: 'three', 2: 'four'}
Traceback (most recent call last):

File "<pyshell # 91>", line 1, in <module>

{1: 'one', 2: 'two'} <{1: 'three', 2: 'four'}

TypeError: Not Supported between "<" of Dictatorship of and "Dictatorship of

If you encounter any doubts in Python comparison operators? Please Comment.

4. Python Greater Than (>) Operator
Let's look more at the comparison operator than Python

Now that we have seen what we can apply these operators to, we will now focus on the operators. Larger than an operator, denoted by>, checks if the left side is greater than the right one.

We recommend you to read Python Decision Making

>>> 0.5> false
Output: True

>>> 3,4,5> 3,4,5.0
Output: (3, 4, true, 4, 5.0)

Hey, it made a rhyme when comparing what we wanted to do. The reason for this is that it took 5 (3) as the value (True). This placed it as the value in the tuple. So let's try to find our way around it.

>>> 3,4,5> 3,4,5.0
Output: (3, 4, true, 4, 5.0)

Therefore we see that spaces do not do this. Let's try something else

>>> 3,4,5> (3,4,5.0)
Traceback (most recent call last):

File "<pyshell # 96>", line 1, in <module>

3,4,5> (3,4,5.0)

TypeError: and> is not supported between 'and int' and 'tuple' instances

Hmm, we think we need to put brackets around both tuples.

>>> (3,4,5)> (3,4,5.0)
Output: False

Yes, it works now. We told you earlier that it is okay to leave parentheses when declaring trivial. But in this situation, it numbered 3, 4 and 5, and it was assumed that we were announcing a tuple, and not comparing two. You should take care of such situations by coding carefully.

Check - How much do you know about numbers in Python

5. Least or equal (<=) operator
We think the next two operations

Post a Comment

0 Comments

Search This Blog