Comments and error messages

Getting Started, lesson 3 of 5

👋 Getting Started, lesson 3 of 5

Comments and error messages

Leave notes in your code and learn to read Python's error messages.

8 min

1 exercise

2 quizzes

0/3 solved

Getting Python ready… examples can run in a moment.

A comment starts with #. Python ignores everything from the # to the end of the line, so comments are notes for humans: what the code does, or why.

Putting # in front of a line of code is also a handy way to switch it off without deleting it (called commenting out).

Example

Comments are ignored

Remove the # in front of the switched-off line and run it again.

Quiz

What does this print?

Everyone makes mistakes, so Python tells you exactly what went wrong with an error message (a traceback). Read it from the bottom up:

  1. The last line names the error type and explains it.
  2. Just above, you see the line number and the code that failed.
Example

A NameError

Start is printed, then line 2 fails: Python doesn't know any name called mesage. Text needs quotes! Change it to print("message") to fix it.

Example

A SyntaxError

A SyntaxError means the code isn't valid Python, so nothing runs at all. Here a closing ) is missing. Add it and run again.

Quiz

Which error does this code cause?

Exercise

Squash the bugs

This program has two mistakes. Run it, read the error message, fix it, and repeat until it prints:

Python is fun!
Let's keep going