Python Syntax · Exercises
Beginner

Python Syntax Exercises

4 exercises that build on the Python Syntax lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: Python syntax is the set of rules for writing code Python can understand: one statement per line, run from top to bottom, with indentation showing which lines belong together. By the end you can write and run a small program with print(), add comments, indent a block correctly, and use a SyntaxError message to find and fix a typo. You will practise: Your first program; Fix the shopping list; Predict the output; Battery and signal warnings.

0 / 4 completed

Python Syntax practice exercises

1. Your first program

Write code Easy

Write a program that prints exactly these two lines:

Hello, Python!
I am learning to code.

Use one print() call for each line. Capital letters and punctuation must match.

Your code
# Write your two print() calls below

2. Fix the shopping list

Fix the bug Easy

This program should print the three lines below, but it has two bugs. Run it, read the error, fix that line, then run again until it works.

Shopping list:
- apples
- bread
Your code
print("Shopping list:")
print("- apples)
Print("- bread")

3. Predict the output

Predict the output Medium

Read the code and type exactly what it prints. Watch out for comments and for the indented block. Then press Check to run it and compare.

Read this code
print("A", "B")
# print("C")
if 2 > 5:
    print("D")
    print("E")
print("F")  # print("G")

4. Battery and signal warnings

Write code Medium

The variables battery and signal are already set. Add code that:

  1. prints Low battery and then Plug in your charger only if battery is below 20,
  2. prints Weak signal only if signal is below 2,
  3. always prints Done at the very end.

Use if blocks with 4-space indentation, like the weather example above. With the given values the output should be:

Low battery
Plug in your charger
Done
Your code
battery = 15
signal = 4

# write your if blocks here

Next lessonPython Comments

More Python Syntax examples · All Python exercises