Comments, blank lines and long statements
Python skips comments and blank lines. Inside brackets, one statement can cover several lines.
A Python program is a sequence of statements that run from top to bottom. Indentation (four spaces) is part of the grammar and shows which lines belong together, # starts a comment, and capital and small letters are different.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a program that sets marks = 82. Only if marks >= 50, it prints Well done! and You passed. (two indented lines under an if). Then it always prints Report finished.
This program has two indentation mistakes. Line 1 is for n in [1, 2, 3]:, which repeats its block once for each number. Line 2 is print(n) with no spaces in front. Line 3 is print("Done") with four spaces in front. Fix the spaces so the program prints 1, 2, 3 and then Done once.
Write a list of five fruit names over five lines inside square brackets, then print the list.
Explain it without notes
How does Python know which lines belong to an if?
What is the difference between a syntax mistake in indentation and a logic mistake in indentation?
What is a comment for, and what makes a good one?
How can one statement be spread over several lines?
Expected output
Item #1: 40
60
['Asha', 'Ravi', 'Meera']