Python Conditionals Like Greater, Lower, Equal Operators Examples – POFTUT

Python Conditionals Like Greater, Lower, Equal Operators Examples


Python provides some conditionals to compare two python data or variables. We can compare same or similar variables and data then we will get result which present the comparison result. The comparison results will be true or false . In this tutorial we will look most popular comparison operations in python.

Less Than

Less than or < is a mathematical operator used in python. There is other uses than mathematic. For example we can compare two dates with less than operator. This operator is generally used to compare two integers or float numbers and returns result as boolean True or False . We will use < operator.

4<5
#This will return false
5<4
#This will return true because 'a' is a string ASCII char and converted to the number
5<'a'
#This will raise exception because a is not a comparable type
5<a
Less Than
Less Than

Greater Than

Greater than is the reverse of the less than operator. This operator will generally used to compare numbers but also can be used to compare dates. We will use > as greater operator.

5 > 4
#This will return true
5 > 4
#This will return false because 'a' is a string ASCII char and converted to the number
5 > 'a'
#This will raise exception because a is not a comparable type
5 > a
Greater Than
Greater Than

Less Than or Equal

We may need to compare two numbers or dates but also check whether they are equal. In this situations we will use Less than or equal operator which is a combination of less than and equal operators. We will use <= as less than or equal operator.

5 <= 4

4 <= 4
Less Than or Equal
Less Than or Equal

Greater Than or Equal

We can combine equal operator with greater than operator too. This will return boolean value True or False according to the situation. If the first value is greater or equal to second value this will return true if not false.

4 >=5

5 >=5
Greater Than or Equal
Greater Than or Equal

Equals

Equal is very popular comparison operator. This will check whether given values or variables are equal. Also Equal can be used for date values. We will provide both variables or values around == operator.

5 == 5

5 == 4
Equals
Equals

Not Equal

Not equal operator is the reverse of the equal operator. With this operator we will check whether two values or variables are not the same. If they are not same True boolean result is returned it not False is returned. We will use != as not equal operator.

5 != 4

5 != 5
Not Equal
Not Equal

LEARN MORE  Powershell Comparison Operators Like Equal, Greater, Lesser, Contains, Regex

Leave a Comment