Comments¶
Definition¶
Comments are part of your code that is for humans not computers. They allow you to make notes about what a line or section of computer code does.
Hint
Comments should be helpful and describe algorithms or difficult to understand parts of a program. It is often more useful to comment a section of code than each individual line.
Easy example¶
# This is a comment
There is no output as comments are ignored when the program is run.
Syntax¶
# Put the comment after a hash symbol
Examples¶
Example 1 - A comment on its own¶
# This is a comment
Nothing will happen when this is run. The line of code will be ignored.
Example 2 - A comment at the end of a line of code¶
radius = 10
area = 3.14 * radius #We don't need an accurate value for pi here
The value 31.4
will be stored in the area
variable. The comment after this will be ignored.
Example 3 - A comment on multiple lines of code¶
# The following algorithm will
# calculate the distance between
# the two points on the map
These three lines would be ignored when the program is run.
Example 4 - A comment on multiple lines of code using quotes¶
'''The following algorithm will
calculate the distance between
the two points on the map'''
Key points¶
Note
Comments are important both for other programmers to understand your code and also for you to understand it. They make it easier for you or someone else to maintain your code. The computer will ignore these comments.
Hint
Python multi-line comments should have a #
at the start of each line. However, you can start and end a multi-line comment with triple-quotes, like this: '''
. These only need to be put at the beginning and end of the section.