The comments in the Python program are important. They can tell you in natural language what the function of a piece of code is. When you want to temporarily remove a piece of code, you can also temporarily disable the code in the form of annotations. The next exercise will let you learn to annotate:
# A Comment, this was so you can read the your program later.
# Anything after the # are ignored by Python.
Print "I could has code like this." # and the comment after are ignored
# can also use a comment to "disable" or comment out a piece of code:
# print "This won ' t run."
Print "This is run."
From now on, I will write the code in this way. I've been stressing "exactly the same", but you don't have to understand it literally. Your program may appear somewhat different on the screen, but it is important that the text you enter in the text editor is correct. In fact, I can write this program with any editor, and the content is exactly the same.
Of course it is in English, we can write in Chinese, please forgive me to install X
Results
$ python ex2.py
I could has code like this.
This would run.
$
Again, I'm not going to put on any more screens. You should understand that the above content is the literal translation of the output content, and $python ... And the last $ is what you should be concerned about.
Readers can look at the following exercises
1. Figure out the function of the "#" symbol. And remember its name. (Chinese is the pound sign, English is Octothorpe or poundcharacter).
2. Open your ex2.py file and check back and forth from the rear. Start with the last line, and check back one word at a words backwards.
3. Have you found anything wrong? Some words will be corrected.
4. Read the exercises you have written and pronounce each character. Have you found any more errors? Correct them as well.
answers to frequently asked questions
Are you sure the name of the # symbol is pound character?
I call it Octothorpe, this name is not used in any country, but all people can understand its meaning. Every country feels that their names are the most correct and brightest. It's an arrogant idea for me, and you don't have to worry about the details, it's more important to learn programming.
If # is the meaning of the note, then why #-*-Coding:utf-8-*-can play a role?
Python still does not treat this line as code, which is just a trickery scheme that allows character formatting to be recognized, or a way out of the way. You can also see a similar note in the editor settings.
Why print "Hi # there." The # is not ignored?
The # in this line of code is inside the string, so it is part of the string before the end of the quotation mark, when it is just a normal character, not the meaning of the annotation.
How do I do multiple annotations?
Put a # in front of each line to do it.
Why do I have to read the code backwards?
This avoids letting your brain follow the meaning of each piece of code, which allows you to process each fragment with precision, making it easier to find errors in your code. This is a good error-checking technique.
Reference: "Dumb way to learn Python"
Thank you for reading the Shanghai Shang School Python training article, more content or support, please click on the Shanghai Python Training
Comments and # Numbers in the Python program