All lessons

Making Decisions

8 min · Python

Run different code depending on whether something is true.

Programs make choices by checking whether something is true. You compare values with operators like > (greater than), < (less than), and == (equal to — that's two equals signs for a comparison).

An if statement runs the indented code beneath it only when the condition is true. Run this:

Try it yourself

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to run itExpected output: You are an adult

else: a fallback

Add an else to say what happens when the condition is false. Exactly one of the two blocks runs.

The indentation — the spaces before print — is how Python knows which lines belong to the if and which belong to the else. It matters.

Try it yourself

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to run itExpected output: Cold

Your turn

The variable number is 7. Print Positive if it is greater than 0, otherwise print Not positive.

Your turn

Loading editor...

Ln 1, Col 1Spaces: 4