The Python if Statement Explained for Beginners

Doing something only when a question is True — and nothing when it isn't.

  • Lesson 2 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

if, by itself

  1. The dragon: Now you can ask a question. Let's actually DO something based on the answer.

  2. The dragon: if CONDITION: means 'the next line only runs when this is True'.

    example.pyPython 3
    health = 20if health < 30:    print("Watch out!")
    OutputWatch out!
  3. The dragon: See the indent? Everything indented under the if belongs to it — that's how Python knows what's conditional.

    example.pyPython 3
    if health < 30:    print("Watch out!")
  4. The dragon: And here's the important part: if the condition is False, absolutely nothing happens. That's normal.

    example.pyPython 3
    health = 90if health < 30:    print("Watch out!")
  5. The dragon: No else needed. An if all by itself, doing nothing when it's False, is completely correct.

  6. The dragon: Now it gets real: your code will run in several caves at once. A knight in one, a sheep in another. Only an if can handle both.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 1

    health = 90. What happens here?

    question.pyPython 3
    health = 90if health < 30:    print("Watch out!")print("Done checking.")
    • Just: Done checking.Correct
    • This causes an error — an if needs an else
    • Watch out! then Done checking.

    Why: health is 90, so health < 30 is False. The indented print never runs, and Python moves straight to the next line: Done checking. No else, no error — this is exactly how if is meant to work alone.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    Your code runs in TWO caves. In one, a knight blocks the path — breathe fire and he runs. In the other, a sheep is just grazing, and scaring it would be mean. This code always breathes fire. Put it inside an if, so the dragon only does it when sees_knight() is True.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.