Python tip 4: comparison chaining
You can chain comparison operators arbitrarily. Apart from terser code, this also has the advantage of having to evaluate the middle expression only once.
>>> from math import factorial
# factorial function gets called twice for this example
>>> factorial(3) > 2 and factorial(3) < 10
True
# function needs to be called only once here
>>> 2 < factorial(3) < 10
True
# another example
>>> 'bat' < 'cat' < 'cater'
True
Video demo:
See my 100 Page Python Intro ebook for a short, introductory guide for the Python programming language.