• Save
  • Download
  • Clear Output
  • Runtime
  • Run All Cells

Loading Runtime

A small but essential concept that we need to cover early-on is something called "comments". Comments are basically textual notes that you can leave for yourself (and others) in your code.

Comments can give extra explanation about what your code is and does –to make it easier for you and others to keep up with what's going on. When used wisely, they can make your code a lot more readable. And we love readability.

Comments are also really useful for temporarily causing certain lines of code to be ignored by the Python interpreter, and at the end of the day that's all that a comment really is. We put a certain symbol at the beginning of a line and that causes Python to skip over those indicated lines and not treat them as code. This can be really useful for temporarily hiding certain lines of code from the interpreter until we're ready to make them visible again. Let me show you what I mean.

Creating a single-line comment.

To create a comment we put a pound sign # (also called a "hash symbol") at the beginning of a line of code, anything we write after that pound sign on the same line will be ignored by the Python interpreter. Typically code-writing applications have a specific color that they will give to comments (usually gray or green). In Temzee notebooks comments are green in color.

Go ahead and try running the code cell above. You'll see the execution count increment indicating that the notebook ran the cell, but nothing else will happen because there isn't any code to be run. The comment is being ignored.

"Commenting Out" a line of code.

We can take something that would normally be a line of code and cause that line of code to be skipped by the interpreter –by turning it into a comment.

In the code cell below we are printing out a string so the string is being displayed in the output section of the code cell.

Go ahead and add a pound sign: # to the very front of the first line of code to "comment out" that line. Then re-run the code cell. Once the cell has been run and the line of code is being successfully ignored by the Python interpreter, the output section of the code cell should disappear –the color will also have changed when the symbol was added. Both of those results are indicators that you've commented out the line successfully.

Is this text showing in the output section of the code cell?

Commenting out lots of lines at once

Sometimes we have tens or even hundreds of lines of code that we would like to temporarily hide from Python by turning all of those lines of code into a comment. We can do this with a very helpful keyboard shortcut –which you will see me use from time to time.

It can be really tedious to add individual pound signs to the beginning of dozens of lines of code, so instead, use your cursor to select all of the lines of code that you would like to comment out, and then press...

On Apple Computers:

cmd + /

On Windows Computers:

ctrl + /

Try commenting out all of the lines in the cell below using the keyboard shortcut. You can also un-comment multiple lines at once using the same keyboard shortcut.

12345678910

Multiline Comments

Multiline comments are useful when we want to turn a big block of text (that may wrap across multiple lines) into a comment. For comments that span multiple lines, these kinds of comments typically look a little bit cleaner than adding pound signs at the beginning of each line.

We create a multiline comment by wrapping the text in a set of three quotation marks """ at the beginning and end of the text. It doesn't matter if you use single quotation marks or double quotation marks as long as you use the same type at the beginning and at the end –just like traditional strings.

''' This would work as a multiline string.'''

""" This would also work as a multiline string (look at the quotation marks)."""

You'll notice that multiline comments typically are colored like strings rather then like comments by our code syntax highlighter. This is because this text is really just a string. The reason why Python skips this string and doesn't do anything with it is because this string is never assigned to a variable. If we were to assign it to a variable then it would be actually be treated just like a regular string.

This is a big 'ol comment that is going ot stretch across many lines of text. I *could* in theory remove the quotation marks and put pound signs at the beginning of each line and accomplish the same thing, but this typically looks better for long blocks of text. Here's some lorem ipsum text (nonsense) to help get the point across: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

If you don't have any other code in a code cell the notebook will display the comment in the output section. This is because notebook cells automatically try and display the last thing written inside of them.

Docstrings

Many professional 3rd-party Python packages that we will be using in the future use multi-line comments to document important segments of their code. These so-called "docstrings" help teach us how to use those tools successfully and we'll be studying them a lot in the future. It's okay if reading them feels like reading a foreign language to you right now.

pandas dataframe docstring