Comments and # signs in Python programs

Comments in Python programs are very important. They can tell you in natural language what a piece of code does. When you want to temporarily remove a piece of code, you can also temporarily disable this piece of code with annotations. The next exercise will teach you to comment:
 
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."

From now on, I will write code in this way. I've been emphasizing "exactly the same", but you don't have to take it literally either. Your program may look a little different on the screen, but what matters is the correctness of the text you enter in the text editor. In fact, I can write this program with any editor and the content is exactly the same.

Of course it's in English, we can write it in Chinese, please forgive me for installing X

 Turns out
$ python ex2.py
I could have code like this.
This will run.
$
 
Again, I won't be posting any more screenshots. You should understand that the above is a literal translation of the output, and that between $python ... and the final $ is what you should care about.
 
Readers can look at the following exercise
1. Figure out the role of the "#" symbol. And remember its name. (Hash sign in Chinese, octothorpe or poundcharacter in English).
2. Open your ex2.py file and check line by line from back to front. Starting from the last line, check back word by word backwards.
3. Did you find any errors? Correct if there are any.
4. Read the exercises you wrote aloud, reading each character. Did you find more bugs? Correct it if there are any.
 
FAQ Answers
Are you sure the name of the # symbol is the pound character ?
I call it octothorpe, which is not used in any country, but everyone can understand what it means. Every country feels that their name is the most correct and brightest. To me this is megalomaniac thinking, and you don't have to worry about the minutiae, learning to program is more important.
 
If # is for annotation, then why does # -*- coding: utf-8 -*- work?
In fact, Python still does not treat this line as code. This usage is just a tricky scheme to make the character format recognized, or it is a no way. You can also see a similar annotation in the editor settings.
 
Why is the # in print "Hi # there." 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. At this time, it is just a normal character and does not mean a comment.
 
How to do multi-line comments?
Just put a # in front of each line.
 
Why make me read code backwards?
This saves your brain from following the meaning of each piece of code, and allows you to process each piece with precision, making it easier to spot bugs in your code. This is an excellent troubleshooting technique.

Reference: "Stupid Way to Learn Python"
Thank you for reading the python training article of Shanghai Shangxuetang. For more content or support, please click   Shanghai python training

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324972472&siteId=291194637