Comment and Pount Characters in Python
Comments are very important in your programs. They are used to tell you what something does in English, and they also are used to disable parts of your program if you need to remove them temporarily. Here’s how you use comments in Python:
# A comment, this is so you can read your program later.
2 # Anything after the # is ignored by python.
3
4 print "I could have code like this." # and the comment after is ignored
5
6 # You can also use a comment to "disable" or comment out a piece of code:
7 # print "This won't run."
8
9 print "This will run."
From now on, I’m going to write code like this. It is important for you to understand that everything does not have to be literal.
Your screen and program may visually look different, but what’s important is the text you type into the file you’re writing in your text editor. In fact, I could work with any text editor and the results would be the same.
Multiline comment in python
'''
This is a multiline
comment.
'''
Again, I’m not going to show you screenshots of all the Terminals possible. You should understand that the above is not a literal translation of what your output should look like visually, but the text between the fi rst $ Python … and last $ lines will be what you focus on.
Study Drills
1. Find out if you were right about what the # character does and make sure you know what it’s called (octothorpe or pound character).
2. Take your ex2.py fi le and review each line going backward. Start at the last line, and check each word in reverse against what you should have typed.
3. Did you fi nd more mistakes? Fix them.
4. Read what you typed previously out loud, including saying each character by its name. Did you fi nd more mistakes? Fix them.
Common Student Questions
Are you sure # is called the pound character?
I call it the octothorpe and that is the only name that no country uses and that works in every country. Every country thinks its way to call this one character is both the most important way to do it and also the only way it’s done. To me this is simply arrogance and, really, y’all should just chill out and focus on more important things like learning to code.
If # is for comments, then how come # – *- coding: utf- 8 – *- works?
Python still ignores that as code, but it’s used as a kind of “hack” or workaround for problems with setting and detecting the format of a fi le. You also find a similar kind of comment for editor settings.
I can’t fi gure out how to type a # character on my country’s keyboard?
Some countries use the Alt key and combinations of those to print characters foreign to their language. You’ll have to look online in a search engine to see how to type it.