Python if not is an easy way to check whether a value is False or True

A Quick Example

data1 = 10

if not data:
    print("Not a zero value")

/* Result: Not a zero value */
Python if not

Python Membership Operators

The not operator is not only used to check value True or False but also used in the membership operators. Python membership operator offer a quick way to validate if a value in a sequence (list, tuple, string) or not.

  • in: return True if value is in sequence. Otherwise, return False.
  • not in: return True if value is not in sequence. Otherwise, return False.

Example 1: Using not in operator

list_data = ["Apple", "Orange", "Mango", "Banana"]

if "Apple" in list_data:
    print("Got Apple in the list data")

if "Watermelon" not in list_data:
    print("Watermelon is not in the list data")

# Result:
#  Got Apple in the list data
#  Watermelon is not in the list data

In the first if, because Apple value is in the list, so the if expression return True. The sentence will be printed.

In the second if, because Watermelon value is not in the list, so the if expression return True. The sentence will be printed as well.

Example 2: Using not with string

text_data = "Hello World"

if "E" not in text_data:
    print(text_data)

# Result:
#   Hello World

In the above example, you can use not check if a character or phrase is in a string.

Newsletter

Send us your email, we will make sure you never miss a thing!

References

You want to get more detail of membership operators and other stuff. Reach below resource for more knowledge:

Sharing is caring!

1 thought on “Python if not

  1. Pingback: Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *