Python 101 – Using Comments in Python

When writing your code, the flow, what each variable does, how the overall program will operate and so on, is all inside your head. Another programmer could follow the code line by line, but when the code starts to hit thousands of lines, things get a little difficult to read. A method used by most programmers for keeping their code readable, is by commenting on certain sections.

Step 1 – We’ll start by creating a new instance of the IDLE Editor (File > New File), and then create a simple variable and print command:

a=10
print(“The value of A is,”, a)

Save the file, and execute the code.

Step 2 – Running the code will return the line: The value of A is, 10 into the IDLE Shell window – which is what we expected. Now let’s add some of the types of comments you’d normally see within code:

# Set the start value of A to 10
a=10
# Print the current value of A
print(“The value of A is,”, a)

Step 3 – Re-save the code and execute it. You’ll see that the output in the IDLE Shell is still the same as before, despite the extra lines being added. Simply put, the hash symbol (#) denotes a line of text the programmer can insert, to inform them and others of what’s going on, without the user being aware.

Step 4 – Let’s assume that the variable A we’ve created is the number of lives in a game. Every time the player dies, the value decreases by 1. The programmer could insert a routine along the lines of:

a=a-1
STEP 4
print(“You’ve just lost a life!”)
print(“You now have”, a, “lives left!”)

Step 5 – While we know that the variable A denotes number of lives and the player has just lost one, a casual viewer, or someone checking the code, may not know. Imagine for a moment that the code is twenty thousand lines long, instead of just our seven. You can see how handy comments are.

Step 6 – Essentially, the new code together with comments could look like:

# Set the start value of A to 10
a=10
# Print the current value of A
print(“The value of A is,”, a)
# Player lost a life!
a=a-1
# Inform player, and display current value of A (lives)
print(“You’ve just lost a life!”)
print(“You now have”, a, “lives left!”)

Step 7 – You can use comments in different ways. For example, Block Comments are a large section of text that details what’s going on in the code, such as telling the code reader which variables you’re planning on using:

# This is the best game ever, and has been developed by a crack squad of Python experts
# who haven’t slept or washed in weeks. Despite being very smelly, the code at least
# works really well.

Step 8 – Inline Comments are comments that follow a section of code. Take our examples from above, instead of inserting the code on a separate line, we could use:

a=10 # Set the start value of A to 10
print(“The value of A is,”, a) # Print the current value of A
a=a-1 # Player lost a life!
print(“You’ve just lost a life!”)
print(“You now have”, a, “lives left!”) # Inform player, and display current value of A (lives)

Step 9 – The comment, the hash symbol, can also be used to comment out sections of code you don’t want to be executed in your program. For instance, if you wanted to remove the first print statement, you would use:

# print(“The value of A is,”, a)

Step 10 – You also use three single quotes to comment out a Block Comment, or multi-line section of comments. For them to work, place them before and after the areas you want to comment:

‘’’
This is the best game ever, and has been developed by a crack squad of Python experts who haven’t slept or washed in weeks. Despite being very smelly, the code at least works really well. ’’’

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Back to top button