Different Operators in Conditions
Learn about the usage of different operators to build conditions in Python.
We'll cover the following...
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, anda == 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 cif a == b == c : # checks whether all three are equalif 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 ...