The following are part of the PEP 8 Python Style Guide, which gives some guidelines on the preferred way to write conditions for if statements.
something = True
if something == True:
pass
if something is True:
passif something:
print("This is good!")
## for False:
something = False
if not something:
print("Yay!")something = None
if something == None:
pass
# Also bad
if not something is None:
passif something is None:
print("This is good!")
if something is not None:
print("Yay!")some_list = []
if len(some_list) == 0:
pass
if not len(some_list):
passif some_list:
print("This is good!")
if not some_list:
print("Yay!")something = "Hello"
if something == "Hello" or something == "Hi" or something = "Hey":
print("This is pretty noisy!")greetings = ["Hello", "Hi", "Hey"]
if something in greetings:
print("Better!")
value = 3
if value in [1, 2, 3, 4, 5]:
print("Good too!")[1] PEP 8 -- Style Guide for Python Code
[2] Python Tricks: A Buffet of Awesome Python Features by Dan Bader
[3] Effective Python: 90 Specific Ways to Write Better Python by Brett Slatkin
[4] Python Cookbook, Third Edition by David Beazley and Brian K. Jones
[5] Writing Idiomatic Python 3 by Jeff Knupp
[6] The Little Book of Python Anti-Patterns by QuantifiedCode