CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 02 / BEGINNER PYTHON

Python syntax: quotes, colons and indentation

Syntax means the rules for writing valid code. Python uses punctuation, keywords and indentation to make the structure of a program precise.

The idea

You can read a sentence with missing punctuation, but a programming language needs a dependable way to separate its parts. Treat each symbol as a signpost. Quotes mark text; parentheses supply information to a function call; a colon introduces a block after statements such as if and for. Indentation tells Python which instructions belong to that block. Use four spaces consistently in these examples.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
has_key = True
if has_key:
    print("The door opens.")
print("Keep exploring.")
Run and change this example ↗
Reveal the expected output
The door opens.
Keep exploring.

Read it one step at a time

  1. True is a Boolean value, so it is not in quotes. # can introduce a comment for a human reader.
  2. The colon ends the if header. The indented print belongs to that decision.
  3. The final print is back at the left edge. It runs whether or not the key is present.

Make one change

Change True to False. Predict both output lines before running.

Check your thinking

The door message disappears; Keep exploring. remains.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.