Statements, indentation, assignment, print

Python groups with indent, not braces. Assignment stores; print shows. Unpacking two names from a pair is the same idea as two lines of assignment, written shorter on a laptop.

Lemonade recipe

Steps in order — just like a program.

You will be able to

  • Read a program as statements in order
  • Use indentation as grouping
  • Assign before print; treat comments as why

Indent is the block

Python groups statements by indentation — not braces. Mix tabs and spaces and the file lies. Four spaces is the campus house style. A line continuation (backslash or parentheses) exists on a laptop for long calls.

Assignment stores. Sequence assignment a, b = 0, 1 evaluates the right first (laptop). Augmented n += 1 is n = n + 1 here. Expression statements that call a method for its side effect (append) are still statements.

print shows; it does not keep

print(net) is for humans. Store first if the next function needs the number. Comments start with # and are not executed. A # inside a string is just a character.

Words that matter

Statement
One instruction — assign, print, def, if, for.
Block
Indented body under if/for/def.
Comment
A # line for humans.

Common mistakes

Avoid: Printing a fee and never assigning it.

Do this: fee = late_fee(days) then print(fee) if a person must see it.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — assign, then use

Keep this order.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — store, then two prints

free is still available for a second line.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — A three-statement bill

Config, compute, show.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Store then show

hours = 3. Pay is hours * 120. Assign pay, then print so 360 appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. Python groups a block with…
2. print is…

Progress is stored in a browser cookie on this device.