DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk5 min

Python Logic for Absolute Beginners: Clearing the Confusion Around True, False, and, or, and not

Python conditions test truthiness, not just True or False. Learn why and, or, and not behave differently from their English names, with examples.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Python, a condition does not have to be literally True. Python asks whether a value is truthy or falsy, and and, or, and not all work from that rule. The part that surprises most beginners is that and and or return one of the values you gave them, which may be a string, a number, or a list rather than True or False.

What a Boolean value is

Python has two Boolean values, True and False. A comparison such as age >= 18 produces one of them, so a Boolean is often the result of asking a question about your data.

As an Amazon Associate I earn from qualifying purchases.

age = 20
is_adult = age >= 18
print(is_adult)          # True
print(type(is_adult))    # <class 'bool'>

The official Python language reference, in its Boolean operations and comparisons sections, defines these rules. The examples here use Python 3 and follow the documentation for the 3.14 series, which the official docs currently label 3.14.7.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How Python decides whether a value is true in a condition

An if statement does not require the condition to be exactly True. Python calls the value’s truth value, which is what bool() returns for it. The language reference lists these values as false: False, None, numeric zero (such as 0 and 0.0), empty strings, and empty containers such as lists, tuples, dictionaries, and sets. Every other value is true, unless the object’s class customizes its truth value with a __bool__ or __len__ method.

name = ""
if name:
    print("Hello,", name)
else:
    print("No name given")    # this line runs, because "" is falsy
Value bool(value) Why
True True Boolean value itself
None False Listed as false in the language reference
0 False Numeric zero is false
0.0 False Floating-point zero is also numeric zero
"" False Empty string
" " True A space is a non-empty string
[] False Empty container
[0] True Non-empty list; the value inside does not matter

The last two rows are the ones beginners most often misread. A list containing a zero is not empty, so it is truthy.

not: the negation operator, which always returns a Boolean

In Python, not is an operator keyword, not a function you call with a name in parentheses. It is written directly before its operand. The Python language reference, in the Boolean operations section, states:

“The operator not yields True if its argument is false, False otherwise.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
print(not "")         # True
print(not [1, 2])     # False
print(not 0)          # True

When to use not instead of checking for False

Most of the time your data is not a literal False. It is a list, a string, or a value that might be None, and you want to ask whether it is missing or empty. not applies Python’s truth rule to any object, so one test covers all of those cases:

items = []
if not items:
    print("Nothing to show")  # runs, because [] is falsy

Comparing with False does not do the same thing. [] == False is False in Python, so if items == False: would miss the empty list entirely. The same is true of None and empty strings. Use not when you mean “this value is empty, missing, or zero” and use == False only when the value is a real Boolean that you want to compare directly.

and and or: they return operands, not new Booleans

Both operators use short-circuit evaluation. Python evaluates the left operand first, and it may skip the right operand entirely:

  • a and b returns a if a is falsy. Otherwise it evaluates and returns b.
  • a or b returns a if a is truthy. Otherwise it evaluates and returns b.

The result is therefore the last value Python evaluated. That value is not converted to True or False.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Expression Result Why
"" or "Guest" "Guest" Left side is falsy, so or returns the right side
"Ana" or "Guest" "Ana" Left side is truthy, so the right side is never evaluated
0 and 5 0 Left side is falsy, so and stops and returns it
3 and 5 5 Left side is truthy, so the right side is returned
[] and [1] [] The empty list is falsy, so it is returned unchanged
False and 1/0 False Short-circuit: 1/0 is never evaluated, so no error occurs

The name = name or “Guest” pattern

name = name or "Guest"

This line is common and useful, but it replaces any falsy value, not only missing ones. An empty string and 0 both trigger the default. If 0 is a legitimate value in your program, write an explicit check such as name = "Guest" if name is None else name instead.

Where the Boolean-only table applies

When both operands are actual Booleans, the results match the familiar truth table. This is the only case where the answer is always True or False:

A B A and B A or B
False False False False
False True False True
True False False True
True True True True

Operator precedence: not, and, or

When an expression has several operators, Python groups them by precedence. From tightest to loosest binding, the relevant levels are:

  1. Comparisons such as ==, <, and >=
  2. not
  3. and
  4. or

Because and binds tighter than or, the expression on the first line below is read as the second:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
print(True or False and False)     # True
# parsed as: True or (False and False)

print((True or False) and False)   # False
# parentheses change the grouping

Likewise, not 1 == 2 is read as not (1 == 2), which is True. Parentheses make the intended grouping visible to the next person who reads the code, including you in a month.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Chained comparisons

Python allows a comparison to be chained, which is how range checks are normally written:

score = 85
print(70 <= score <= 89)   # True

The expression means 70 <= score and score <= 89, and score is evaluated only once. Beginners often write the same check with two separate conditions joined by and, which is also correct, but the chained form is shorter and matches how the value is described in the language reference.

Common confusions to clear up

  • Assignment versus equality. = stores a value in a name, and == asks whether two values are equal. if score = 90: is a syntax error in a condition, which is Python’s way of catching the mistake.
  • Testing against True. if ready == True: works, but if ready: is the idiomatic form, because it accepts any truthy value.
  • Identity versus equality. is checks whether two names refer to the same object, while == compares values. For None, the convention is if value is None:.
  • Expected Boolean results. If you assume and and or always return True or False, the first example that prints a string or a number will look like a bug. It is documented behavior.

Check the behavior yourself

You can verify every example above in an interactive session without creating a file:

  1. Open a terminal and start Python 3 by typing python3. On Windows, the launcher is usually py.
  2. At the >>> prompt, type an expression such as "" or "Guest" and press Enter. Python prints the value it returned.
  3. Type bool("") next and compare the output. Repeat with bool([0]) to see the case that surprises most beginners.
  4. Type exit() or press Ctrl+D (Ctrl+Z then Enter on Windows) to leave the session.

For the authoritative wording, read the Boolean operations, comparisons, and operator precedence sections of the official Python language reference. Those sections describe the rules used throughout this article.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.