0%

Making Decisions · practice

Run Code When a Condition Is True

A comparison gives Python a answer. An statement uses that answer to decide whether a block of instructions should run.

Here is a first decision:

Try it

The program prints both lines because temperature < 15 is True.

Change temperature to 20 and run the example again. The jacket message disappears, but the final line still prints.

Read an if statement in three parts

The first two lines contain three important pieces:

if temperature < 15:
    print("Bring a jacket.")
  1. The keyword if begins the decision.

  2. temperature < 15 is the . Python evaluates it as True or False.

  3. The colon : announces that an indented block follows.

When the condition is True, Python runs the indented block. When it is False, Python skips that block.

You do not need parentheses around an ordinary condition in Python. The colon and indentation show the structure.

Indentation groups instructions

Python uses indentation as part of its syntax. The usual style is four spaces for every line inside an if block.

A block can contain more than one instruction:

Try it

All three indented lines belong to the if block. The final print call is no longer indented, so it runs after the decision whether the condition was true or false.

Trace the example from top to bottom. Python evaluates the condition, enters the block, changes balance, and then continues with the unindented line. If cost were 30, Python would skip the entire indented block.

A Boolean variable can be the condition

A condition does not have to contain a comparison at the if line. If a already contains True or False, use it directly:

Try it

Read if is_member: as “if is_member is true.” Writing if is_member == True: is unnecessary. The variable already contains the Boolean answer that if needs.

Separate if statements ask separate questions

A program can contain several independent if statements. Python evaluates each one:

Try it

The first message prints. The second does not. These are two separate decisions, so both conditions are checked.

The next lesson introduces else for situations where exactly one of two paths should run. For now, each if block either runs or is skipped on its own.

Two syntax details to check

A missing colon is a :

if temperature < 15
    print("Bring a jacket.")

An unindented line cannot form the intended block:

if temperature < 15:
print("Bring a jacket.")

When Python points to an if line or the line immediately below it, check the colon and indentation first.

Which line belongs to the if block?

points = 12
if points >= 10:
    print("Target reached")
print("Score checked")

What does this code print?

temperature = 22
if temperature < 20:
    print("Wear a jacket")
print("Ready")

Write three independent decisions

The exercise gives you fixed information about a day out. Add three if statements:

  • print a water reminder when the temperature is at least 25;

  • print an umbrella reminder when rain is expected;

  • print an opening message when the venue is open.

One condition is false. Its message must be skipped. Keep the final unindented line so the program always finishes with Check complete.

The final unindented line runs after all three decisions, including when a block is skipped.

Task

Add three independent statements to the day plan.

  • If temperature is at least 25, print Take extra water.

  • If rain_expected is true, print Pack an umbrella.

  • If venue_open is true, print The venue is open.

Use a colon and four spaces of indentation for each block. Keep the fixed values, heading, and final Check complete. line unchanged.

After the program works, click Step in the editor toolbar. Watch Python enter the temperature and venue blocks, skip the rain block, and then continue with the final unindented line.