Challenge 4-2: Comments

Challenge 4-2: Comments

What is a Comment?

Comments are a really important concept in computer programming. We'll explain why on the next page, but first we'll give you an introduction to what comments are and how you can write them.

Comments are lines in your code that you don't want the computer to run. That might sound like a strange idea - after all, isn't the whole point of a program that it is able to be run? Well, usually, but sometimes we might want to include instructions or explanations alongside our code, which we don't want Python to interpret.

Comments are most useful in programs that you might want to save and run multiple times, so for this challenge we won't be using the Python shell. Instead, we'll be using the Python IDLE, or Integrated Development and Learning Environment. It's a handy program that lets you write, save and run Python code on your computer.

To install the IDLE, go to python.org/downloads and download the latest version of Python 3. For detailed information about running the installer and launching Python on Windows, look at the documentation on this page: http://docs.python.org/3/using/windows.html.

Now, back to comments.

In order to write a comment in Python, simply type a "#", the hashtag symbol, in front of any line you want to be a comment, like so:

>>> def func(var):
>>> # This whole line is a comment
>>> print(var)

You can also start a comment halfway through a line. In this case, everything to the left of the hash will be interpreted and run, and everything to the right will be ignored.

>>> def func(var):
>>> # This whole line is a comment
>>> print(var) # This is a comment too

In this example, the print statement will still be interpreted and run by python, but everything after the hashtag will be ignored.

Notice how Python highlights comments in a different colour, to make sure you know that they're not going to be run by the computer. If you're using the online Python shell you might not see these colours, but don't worry, the program will work just the same.