Logical and Membership Operators
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Real rules need more than one check. Python combines checks with plain English words, and and / or have a trick of their own: they hand back one of the values.
and, or and not
Logical operator is one of the words and, or and not. They combine or flip conditions, and they decide by whether a value is true or false, not by its bits.
Python writes the three logic operators as words, not as &&, || and !.
is_admin = Truehas_token = Falseprint(is_admin and has_token)print(is_admin or has_token)print(not has_token)and is true only if both sides are. or is true if at least one side is. not flips the value.
Truthiness
Truthiness is whether Python treats a value as true or false when a condition asks. Zero, empty text and None are false. Almost everything else is true.
Python can use any value as a yes or no. A few values count as no; everything else counts as yes.
print(bool(0))print(bool(""))print(bool(None))print(bool("0"))print(bool(-3))The false values are 0, 0.0, the empty string "", None and False. "0" is text with one character, so it is true.
Short-circuiting and the value that comes back
Short-circuit evaluation is Python stopping early in and and or. In a and b, if a is false then b is never evaluated. In a or b, if a is true, b is skipped.
Python stops as soon as the answer is known. And it returns the value it stopped at, not just True or False.
name = ""print(name or "guest")print("Asha" or "guest")print("Asha" and 42)print(0 and 42)Example 1 · Guarding a division
divisor = 0result = divisor != 0 and 100 / divisor > 2print(result)False
What this code does
divisor != 0isFalse, so Python stops there.- The division is never run, so there is no ZeroDivisionError.
Example 2 · A default display name
user_name = ""display_name = user_name or "Anonymous"print(display_name)Anonymous
What this code does
- An empty string is false, so
ormoves on and returns the second value. - A non-empty name would have been returned unchanged.
or returns the first true value, or the last one. and returns the first false value, or the last one. Once the answer is known, the right side is never read.
Membership: in and not in
Membership operator is in or not in. It tests whether a value is found inside something that holds values, such as a piece of text, and gives back True or False.
To ask whether some text appears inside other text, use in. It reads like English.
email = "developer@mentornxt.com"print("@" in email)print("admin" not in email)print("mentor" in email)Example 3 · Valid file name
file_name = "report.pdf"is_pdf = ".pdf" in file_nameis_temp = "temp" in file_nameprint(is_pdf and not is_temp)True
What this code does
".pdf" in file_nameisTrue.not is_tempisTrue, because the name has no "temp".notis worked out first, thenand, thenor.
in is true if the left text is found anywhere inside the right text. not in is the opposite.
Next step
Check your understanding
9 quick questions