Loading...

Python Indentation

The last page kept every line at the left edge. This page moves some lines in, by four spaces, so Python can see a block. A block is the lines that belong to the line above them. That upper line ends with a colon.

Four spaces under the colon

if True: is the header. True means the block runs. The two prints sit four spaces to the right, so both belong to the header. The output is two lines: lida, then tea. What a condition means in full comes later. Here the only question is which lines are inside.

Both lines inside

python
if True:
print("lida")
print("tea")

Count the spaces before print. There are four. Not one, not a tab. This course uses four spaces for every line inside a block.

The line that steps back out

if False: still opens a block, but False means the indented lines are skipped. print("lida") is inside, so it does not run. print("tea") has gone back to the left edge, so it is outside the block and it does run. The output is one line: tea.

Only tea stays outside

python
if False:
print("lida")
print("tea")

Same two calls as the first program. The indent is what changed. lida moved inside a block that does not run. tea stayed outside, so tea is the only line you see.

Three positions

Read the second program as three different jobs. The header decides. The indented line belongs to it. The left-edge line does not.

📊 Where each line sits

Line Position Runs when the header is False
if False: header, ends with a colon the test itself, not a print
print("lida") four spaces in no
print("tea") left edge yes

Spaces, not a tab

Use the space key four times. A tab is a different character, even when it looks like a gap. Mixing a tab with spaces makes the block uneven, and Python stops. Do not guess the width by eye.

The colon is part of the header

The header is unfinished without the colon. if False with no colon is not a header, so the indented line under it has nowhere to belong. The program stops before it prints.

Left for the next pages

This page does not teach the rest of decisions, and it does not start a line with #. A note that Python skips is the next page. Names and input stay later too.

Check yourself

  1. A line inside the block is four spaces in.
  2. Under if True:, both prints run: lida, then tea.
  3. Under if False:, the indented print("lida") does not run.
  4. print("tea") at the left edge still runs.
  5. A tab is not a substitute for the four spaces.

Misses

  • Indenting by one or two spaces
  • Using a tab, or mixing a tab with spaces
  • Leaving the colon off the header
  • Indenting the line that should stay outside
  • Expecting lida to print under if False:

Keep

  • A header ends with a colon. The block under it is indented four spaces.
  • if True: runs the block. Both prints show lida, then tea.
  • if False: skips the indented line. print("tea") at the left still runs.
  • A tab is not four spaces.
  • The next page is a note Python does not run.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top