...

/

Different Operators in Conditions

Different Operators in Conditions

Learn about the usage of different operators to build conditions in Python.

Nuances of conditions

A condition is built using relation operators <, >, <=, >=, ==, and !=.

10 < 20 # yields True
'Santosh' < 'Adi' # yields False, alphabetical order is checked
'gang' < 'Good' # yields False, lowercase is > uppercase
Build conditions using relation operators

Note: In Python, a = b is assignment, and a == b is comparison.

Ranges or multiple equalities can be checked more naturally, as shown below:

if a < b < c : # checks whether b falls between a and c
if a == b == c : # checks whether all three are equal
if 10 != 20 != 10 : # evaluates to True, even though 10 != 10 is False
Ranges or multiple equalities

Any nonzero number (positive, negative, integer, float) is treated as True, and 0 is treated as ...