we help you to understand Python better

x not in v.s. not x in

When you have used the not operator in Python, you probably have noticed that this code:

if "@" not in email_address:
    print("Error!")

outputs the same result as this code:

if not "@" in email_address:
    print("Error!")

Both expressions are functionally identical and produce the same output. In the Python documentation, you find an example how to compare a variable to a singleton. PEP8 says: “Use is not operator rather than not ... is”.

Correct way

When the same rule applies to in, this is the correct way to check if a character is not in a string:

email_address = "pythonforeveryone.com"  # not valid, no @

if "@" not in email_address:
    print("Error!")

Output:

Error!
Dive deeper into this topic with training?
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.