Back to Python Fundamentals

3: Comments and Readable Layout

Readable code is easier to debug and maintain. Comments explain why code exists, not obvious steps.

Contributed by Bijou Raj

In [1]Text cell

Quick lesson. Comments are helpful for communicating ideas in our code. Sometimes we make a decision or write some code that may be unclear to others, or even to ourselves in the future. Comments are helpful reminders of what we need to know. We use the # symbol for single line comments.

In [2]Code cellpyodide
In [3]Text cell

We can also block out whole lines of text with three ". This is used for other purposes as well, but for now, it is okay to use these to comment out massive chunks of text.

In [4]Code cellpyodide
In [5]Text cell

In addition to comments being helpful to understanding our written code, they can help us in the writing process as well. Commenting things out when we don't want them to run can be very helpful for debugging. Take this, for example.

In [6]Code cellpyodide
In [7]Text cell

There's a lot going on here, but what if something was broken? It could be difficult to find out where it was if we executed the entire block in each iteration, so let's get rid of some of the noise.

In [8]Code cellpyodide
In [9]Text cell

This can help us isolate smaller sections of our code. Try it out on the next exercises.

In [10]Text cell

We only want to print out Good.

In [11]Challenge cellpyodide
In [12]Text cell

Print out only the value of hm.

In [13]Challenge cellpyodide
In [14]Text cell

Uncomment the lines that will get us to print: John is five years old.

In [15]Challenge cellpyodide