This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: The PEP 8 style guide

True or False equality in conditions

face Josiah Wang

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.