OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Decisions with if, elif and else

Express a rule as readable branches and understand indentation.

Lesson 6 of 22 · About 20 minutes · Beginner · English

By the end of this lesson

Choose one branch from a small decision tree.

Before you start

  • Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
  • Complete lesson 5 in this route, or be comfortable with its examples.

Three words to know

Boolean
True or False.
Comparison
An expression such as count == 0 that produces a boolean.
Branch
A block selected by a condition.

1. Indentation is structure

A colon begins the indented body of an if statement. Use four spaces consistently. Python chooses the first true branch, so the order of conditions matters. == compares values; = assigns a name.

Choose a statusPYTHON · save as lesson.py, then run with your interpreter
count = 3
if count == 0:
    print("empty")
elif count < 5:
    print("small")
else:
    print("large")
What you should see · example output
small

2. Combine rules clearly

and requires both conditions; or allows either; not reverses a truth value. Parentheses can clarify a compound rule. Prefer explicit comparisons while learning. A non-empty string such as "False" is truthy even though its letters spell False.

Two requirementsPYTHON · save as lesson.py, then run with your interpreter
is_json = True
size = 120
if is_json and size <= 65536:
    print("Ready for the next check")
print(bool("False"))
What you should see · example output
Ready for the next check
True

3. Predict before executing

Change count to 0 and then to 8. Write the expected branch before running. If every case prints the same line, check whether print is outside the branch and whether you accidentally compared the wrong name. A syntax error near indentation is a structural problem, not evidence that the condition was false.

YOUR TURN

A small check before you continue

Write a condition accepting a number from 1 to 10 inclusive. What changes if you use < instead of <= at the upper bound?

Show a suggested answer

1 <= number <= 10 accepts both endpoints. Using number < 10 rejects 10. Test 0, 1, 10 and 11 to see the boundary.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.