Lesson 4
Repeated Practice Makes Perfect
Chapter 9: The PEP 8 style guide
True or False equality in conditions
One more PEP 8 recommendation: avoid x == True
or x == False
in if
or while
conditions. Keep this implicit (because it is much easier to read).
# Recommended:
if x > 2:
print("yes")
# Not recommended:
if (x > 2) == True:
print("yes")
This is more apparent when using variables as conditions. The first one reads more like English.
# Recommended:
if found:
print("Great!")
# Not recommended:
if found == True:
print("Great!")
Tip: It is important that you name your boolean variables meaningfully because of this. You can name your boolean variables so that they sound like they will return True or False, for example is_prime
or has_number
.