Comparison And Logical Operators In Python

Hello Pythonistas, here’s a quick reference to Comparison and Logical operators in Python.

Comparison

Tell me when was the last time you shopped online🌐?

Did you have options to choose from?

How did you choose the best from the available options?

Maybe you did it by comparing the prices💵, quality✅, or brand🏆 of the product.

Comparison operators are used for the same purpose.

No, they won’t help you compare products you shop online, they help you out with comparing numbers or strings or other data types.

Like, say you want to know if this is 41919.21524 greater than this 119749.14877 or not.

You can simply know it by typing in idle 41919.21524>119749.14877 and pressing enter and getting the answer as False which means it’s not greater.

Let’s look at these operators in detail.

OperatorExplanationExample
==This checks if LHS equals RHS.print(10==10) #True
!=This checks if LHS is not equal to RHS.print(10!=11) #True
<=This checks if LHS is less than or equal to RHS.print(12<=10) #False
>=This checks if LHS is greater than or equal to RHS.print(10>=10) #True
<This checks if LHS is less than RHS.print(100<10) #False
>This checks if LHS is greater than RHS.print(100>10) #True

Logical

Well, have you ever been told by anyone to think logically🤔 using your brain🧠?

I had to listen to it a hell lot of times.

Now, I am not sure whether I think logically🤔 or stupidly😅 using my brain🧠. But, python has got tools to make your program think🧠 logically. Here are those three tools:

OperatorFunctionExample
andThis returns True if both the statements are trueprint(10==10 and 10!=11) #True
orThis returns True if at least one of the statements is trueprint(10!=10 or 10!=11) #True
notIf the result is True it will give Falseprint(not(10==10)) #False

Note: If you are unable to understand what returns mean it is the result we get on using a function or operators that can be stored inside a variable or be printed using the print function. Don’t worry if you still feel confused it would be discussed in detail when I will explain the functions.

Leave a Reply