Python while and if: True or False Conditions — Absolute Beginners Tutorial

Every while loop and every if statement in Python lives or dies by a single question: is this condition True right now, or is it False? This tutorial teaches you to read and write both, including the patterns while True, while not, if True, and if not.

Before writing any loop or branch, Python evaluates your condition and reduces it to one of two values: True or False. These are not strings, not numbers — they are Python's actual boolean literals, belonging to the built-in bool type. Understanding how while and if use those values is the foundation of all control flow in the language.

True and False: Python's two boolean values

Python has exactly two boolean values: True and False. Note the capitalization — Python is case-sensitive, so true and false (lowercase) are not boolean values. They would cause a NameError.

python
print(type(True))   # <class 'bool'>
print(type(False))  # <class 'bool'>

# Every comparison produces a boolean
print(5 > 3)   # True
print(5 < 3)   # False
print(5 == 5)  # True

When Python evaluates a condition in a while or if statement, it is asking: does this expression produce True or False? You can write the literals directly, or you can write an expression — a comparison, a function call, a variable — and Python resolves it down to one of those two values.

Truthiness

Python also evaluates non-boolean values in a boolean context. An empty list [], the number 0, an empty string '', and None all act as False. A non-empty list, any non-zero number, or a non-empty string acts as True. This is called truthiness, and it means you can write if my_list: instead of if len(my_list) > 0:.

The if statement: run a block when something is True

An if statement evaluates its condition exactly once. If the condition is True, the indented block runs. If the condition is False, Python skips that block entirely and moves on.

python
door_locked = True

if door_locked:
    print("The door is locked.")

# Output: The door is locked.

The condition here is a variable whose value is True. Python reads door_locked, sees that it holds True, and runs the block. Now change the value:

python
door_locked = False

if door_locked:
    print("The door is locked.")

# Nothing prints — the block is skipped

You can also use a comparison expression directly as the condition:

python
temperature = 98

if temperature > 100:
    print("Overheating warning.")
else:
    print("Temperature is normal.")

Python evaluates temperature > 100, which produces False (since 98 is not greater than 100), so the else branch runs instead.

code builder click a token to place it

Build an if statement that prints "Access granted" when is_admin is True:

your code will appear here...
if while is_admin : print ("Access granted") pass
Why: An if statement starts with the keyword if, followed by a condition — here is_admin — then a colon :. The indented block that follows is the body that runs when the condition is True. print("Access granted") is the body. while would open a loop, not a one-time check, and pass would make the block do nothing.

The while loop: keep running while something is True

A while loop works like an if statement that repeats. Python evaluates the condition, runs the block if the condition is True, then evaluates the condition again. This continues until the condition becomes False — at which point the loop ends and execution continues after it.

python
count = 0

while count < 3:
    print("count is", count)
    count = count + 1

# Output:
# count is 0
# count is 1
# count is 2

Each time through the loop, Python checks count < 3. The first three times it is True, so the block runs. After the third pass, count equals 3, the condition becomes False, and the loop stops.

Key mental model

Think of while as asking the same yes/no question over and over. Every time the answer is yes (True), the body runs. The moment the answer is no (False), the loop exits. If the answer never becomes no, the loop never stops.

The difference between if and while in one sentence

if checks the condition once and runs the block at most one time. while checks the condition repeatedly and runs the block zero or more times — as long as the condition stays True.

Keyword Checks condition Runs block Typical use
if Once 0 or 1 times Branch on a single state
while Repeatedly 0 or more times Repeat until a state changes

while True and while not: the two infinite-loop patterns

while True

while True creates a loop whose condition is the literal value True. Because True never becomes False on its own, the loop runs forever — unless something inside the body explicitly stops it. That stopping mechanism is the break statement.

python
attempts = 0

while True:
    password = input("Enter password: ")
    attempts = attempts + 1

    if password == "correct_password":
        print("Access granted after", attempts, "attempt(s).")
        break  # exit the loop

    print("Wrong password. Try again.")

The loop keeps asking for a password indefinitely. Only when the user enters the right value does the if block run, print a message, and call break to exit. Without that break, the loop would never end.

Always include a break path

Every while True loop must have at least one break statement that can actually be reached. A loop with no reachable break is an infinite loop that will hang your program. When writing while True, write the break first, then add the surrounding logic.

while not

while not inverts the condition. while not condition means: keep looping as long as condition is False. When condition finally becomes True, the not turns it to False, and the loop exits.

python
download_complete = False

while not download_complete:
    print("Downloading...")
    # simulate work
    download_complete = True  # in real code this would check progress

print("Download finished.")

The loop reads: "while the download is not complete, keep running." As soon as download_complete is set to True, the condition not download_complete becomes not True, which is False, and the loop exits.

python
# How not flips values
print(not True)   # False
print(not False)  # True

# Applied to a variable
found = False
print(not found)  # True — the condition fires

if not: run a block when something is False

if not runs the block only when its condition is False. It is the natural way to check for the absence of something.

python
is_logged_in = False

if not is_logged_in:
    print("Please log in to continue.")

# Output: Please log in to continue.

The pattern if not is also the idiomatic way to check whether a collection is empty:

python
items = []

if not items:
    print("The list is empty.")

items.append("apple")

if not items:
    print("Still empty.")
else:
    print("The list has items.")

# Output:
# The list is empty.
# The list has items.

An empty list is falsy in Python — it evaluates to False in a boolean context — so not items is True when the list is empty and False when it contains anything.

spot the bug click the line that contains the error

This loop is supposed to keep running until ready becomes True. One line is wrong. Click it.

1 ready = False
2 while ready:
3 print("Waiting...")
4 ready = True
5 print("Ready!")
Fix: Line 2 should be while not ready:. The intent is to loop until ready becomes True. Since ready starts as False, while ready: exits immediately — the body never runs at all. Changing to while not ready: means: keep looping while ready is still False, which is the correct behavior.

Combining while and if together

In practice, while loops and if statements almost always appear together. The while loop controls how long something keeps running, and the if statements inside the loop control what happens on each pass.

python
number = 1

while number <= 10:
    if number % 2 == 0:
        print(number, "is even")
    else:
        print(number, "is odd")
    number = number + 1

# Output:
# 1 is odd
# 2 is even
# 3 is odd
# ... and so on up to 10

The while condition number <= 10 controls how many times the loop runs. On each pass, the if / else inside determines what message to print. The two constructs work at different levels: the loop decides duration, the if decides what happens within each iteration.

Using break inside while with an if

The break statement is how you exit a while loop from inside. It is typically placed inside an if block that checks for an exit condition:

python
numbers = [4, 7, 2, 9, 1, 5]
index = 0

while True:
    if index >= len(numbers):
        break  # no more items — exit

    if numbers[index] == 9:
        print("Found 9 at index", index)
        break  # found it — exit

    index = index + 1

There are two break paths here: one for running out of items, and one for finding the target. The while True keeps the loop running until one of those two conditions is met.

How to Write while and if with True or False

  1. Know your two boolean values

    Python has exactly two boolean literals: True and False, both capitalized. Every condition in a while loop or if statement reduces to one of these. You can write them directly or produce them through comparisons like x > 0 or name == "admin".

  2. Use if when you want to act on something being True once

    Write if condition: followed by an indented block. Python evaluates the condition once. If it is True, the block runs. If it is False, the block is skipped. Use else: after the block to handle the False case.

  3. Use while when you want to keep acting as long as something is True

    Write while condition:. Python re-evaluates the condition before every iteration. Make sure the body can change the condition — or include a break — otherwise the loop runs forever.

  4. Write while True when the exit point is inside the loop

    Use while True: when it is easier to check the exit condition inside the body than at the top. Always pair it with a break inside an if block that fires when you want the loop to stop.

  5. Use while not and if not for absence-based conditions

    When your logic is "do this while something has NOT happened" or "do this if something is NOT present," use while not condition: or if not condition:. The not operator inverts the boolean value, making the code read the way you would say it out loud.

Python Learning Summary Points

  • True and False are Python's only boolean literals. They are case-sensitive — true with a lowercase t is a NameError.
  • An if statement evaluates its condition once and runs its block zero or one times depending on the result.
  • A while loop evaluates its condition before every iteration and keeps running as long as the condition remains True.
  • while True creates an infinite loop. It requires a break statement inside to ever stop.
  • while not condition loops as long as the condition is False. When the condition becomes True, the loop exits.
  • if not condition runs its block only when the condition is False. It is the standard pattern for checking whether something is absent or empty.
  • Falsy values in Python — 0, "", [], {}, None, and False itself — all evaluate to False in a boolean context. Everything else is truthy.
  • Inside a while loop, if statements handle per-iteration branching. The while decides how long the loop runs; the if decides what happens on each pass.
check your understanding question 1 of 5

Frequently Asked Questions

while True creates an infinite loop — a loop that runs forever because its condition is permanently True. It only stops when a break statement is reached inside the loop body, or when the program is terminated.

while True runs until an explicit break inside the loop stops it. while some_condition re-evaluates the condition before each iteration and exits automatically when that condition becomes False. Use while True when the exit point is determined by logic inside the loop rather than a simple variable check at the top.

while not condition means: keep looping while the condition is False. The not operator inverts the boolean value. So while not done runs as long as done is False, and stops as soon as done becomes True.

if True means the condition is always met, so the block inside always executes. In practice, if True is rarely written literally — it appears during development as a placeholder when you plan to replace True with a real expression later.

if not condition means: run this block only when the condition is False. It is equivalent to if condition == False, but more readable. Common uses include checking whether a list is empty (if not my_list) or whether a flag has not been set yet.

Yes. A while loop and an if statement both accept any boolean expression as their condition. The difference is behavior: if evaluates the condition once and either runs the block or skips it. while re-evaluates the condition repeatedly and keeps running the block until it becomes False.

Python treats these as False in a boolean context: the literal False, None, the integer 0, 0.0, an empty string '', an empty list [], an empty tuple (), an empty dict {}, and an empty set set(). Everything else evaluates to True.

Use a break statement inside the loop body. When Python executes break, it exits the loop immediately and continues with the first statement after the loop. You can also raise an exception or return from a function to stop the loop, but break is the standard mechanism.