Now, before you get too far into coding with your code,
it’s useful to get in the habit right away of using comments.
Comments are a way of documenting your code.
By adding a hashtag or a pound symbol
at the beginning of the line of code,
that turns your line into a comment,
which means when the code runs,
it does absolutely nothing.
It’s there for you as a programmer to remember what your code does
or for someone else reading your code to understand what your code does.
So when you run the code,
you aren’t going to see any output in this case.
In fact, you can put comments in front of code
that you’re experimenting with.
Maybe you try a line of code
it isn’t working or it is working,
when you want to try changing something.
Rather than deleting the line of code,
sometimes I’ll comment out that line of code
and then try it again with the difference.
So that way, I don’t lose the old code,
I just comment it out in case I need it back.
So they are useful.
So any line of code that has
that pound or hashtag symbol in front of it,
is not going to execute.
A really good habit to get into is
if you’re calling a function or a method,
and we’re going to cover functions and methods later on in the course.
But if you’re calling one,
add a comment before you call it to remind yourself,
what does this function do?
Why am I calling it here?
It might be really obvious to you
when you’re writing the code initially,
but when you come back to that code in six months,
or a year, or two years to fix a bug or to make a change,
chances are you’re going to forget a lot of the code you did.
So you’ll really appreciate the time you spent
commenting when you did the initial work on it,
when everything was fresh in your mind.
Sometimes you’ll have to do some quirky things
to make things work.
Maybe you’re always using single quotes around all your strings,
and then suddenly you have one print statement with double-quotes.
Why did you do that?
Well, maybe I should add a little note.
If you’re printing a string like it’s a small world after all,
比如你打算输出字符串 it’s a small world after all
there’s a single quote inside that string.
So I would have to use double quotes to enclose the string.
So what I’ve done, is I’ve added a comment here,
so anybody else who was reading my code
would understand why is it I use single quotes
for all my strings except this one line.
So those are the things
when you do something out of the ordinary or make a function call,
add comments so others can understand your code.
It’s also really useful when you are debugging your code as well.
If you’ve got a long program and something is blowing up
but you can’t figure out which line of code isn’t working.
There have absolutely been times
where what I did was I basically commented out.
So turned a whole bunch of lines of code into comments,
and then see if the first five lines of code in my program worked.
The first five lines worked,
let’s uncomment the next five lines.
Did that work? Still no error message.
Uncomment the next five lines,
and so on until I see the error message.
That can help me narrow down
where in the code my error is coming from.
So get in the habit right away of using comments in your code,
both for documenting what you’ve done and for debugging.