Double Comparisons in Python
In Python, you can use double comparisons, where you write comparison operators on the left and right sides of a variable.
Let us have a variable tst
:
tst = 3
Let's do a double comparison for her:
if 2 < tst < 10:
print('+++')
else:
print('---')
Given a variable:
tst = 15
Check that it is greater than 10
and less than 20
.
Given a variable:
tst = -5
Check that it is less than 0
and greater than -10
or greater than -8
and less than 30
.
Given a list:
tst = ['a', 'b', 'c', 'd', 'e']
Check that its length is less than 6
and greater than 0
.