0% found this document useful (0 votes)
11 views3 pages

PYTHON100 MOD5 COMMENTS V3 en

Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

PYTHON100 MOD5 COMMENTS V3 en

Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

(upbeat music)

- Hello and welcome back to Python Fundamentals.


This is module five and we're gonna talk about comments.
Comments are super important in computer programming
and they really aren't code.
They're a way for you to note
what's happening in your code in English
for others to take a look at
or yourself maybe to remind you about
what's happening in the code.
So comments, what are they?
They're valuable information for other humans.
They don't affect the code at all.
They're not actually run when you run Python code.
Two basic reasons to put comments in your code.
One is to identify variables
so that you know what's inside the variables,
why you created the variables
and what kind of values are being stored in those variables.
The other reason to use comments is
to explain complicated processing.
Again, you may be certain that when you write your code,
you know what the variables are
and why you're doing the processing,
but you might come back to it later
and you don't remember and what was x and what was y
or why did you write that loop that way.
It's also really valuable for other people
because often we work in groups in programming
and other people may be looking at your code,
maybe inheriting your code to do things with
and they won't know your thought process.
So it's a way for you to note in your thought,
you know, in shorthand, in English,
why you did those things in the code.
Okay, so let's look at how to do comments in Python.
There's basically two approaches to comments.
One is to just do single line comments
which is probably the most popular.
We use a pound sign for single line comments.
The other option is to use triple quotes
for multiple line comments.
So let's go ahead and bring up Anaconda,
if you don't have it up already.
And I'm actually going to use the code we did in module four
to illustrate comments.
So in module four,
we had created four variables if you recall, right here.
There are four variables that we had in computer memory,
x, y, capital X, and sum.
So they're already created in our code.
So if we go to look at our code.
Here, we can see I have module four open,
module four brought up here in the editor.
And I can see that I have 14 lines of code there
from our previous module.
So I'm gonna go in the first line in line 1,
I'm gonna click right in front of the x
and hint the Enter key to create a blank space.
And you can see that now that I've done that,
Jupyter Notebooks is being very helpful.
It's identifying that
this particular code snippet has changed.
I have a colored bar, I have this dot.
It looks different than the other code snippets
'cause I'm editing it, changing it,
and I haven't run it yet.
It still says number 1 though.
So I'm gonna click up here into the top
the new line that was added,
I'm gonna put in a pound sign
and I'm gonna write this as a comment.
So I've done that there, put a comment in.
And now I'm going to run it.
And you can see that the code snippet
has now gotten a new number and it's turned gray.
The bar has moved down to the next snippet anticipating.
So Jupyter Notebooks is anticipating
that I might wanna do something in the next snippet,
but if I look back at snippet 19,
it turned gray, it ran,
it didn't have any errors or anything like that.
And it's important to note that it's a new number,
that the order of processing has now changed a little bit
in my notebook.
So if you go back and think about
what we did in module four.
We did a code snippet and ran it,
a code snippet and ran it, a code snippet and ran it,
in a linear fashion,
from the top to the bottom of our notebook.
So it is possible in your code to go back to an old cell
and change things and run it again.
But you should now know that it's got a new number
and it's now the most recent cell that was run.
And we'll talk about that in a future module about
how that might affect future cells.
But let's get back to comments.
So I've put a comment in there, this is a comment.
That's not really that useful, right?
To somebody else who's reading my code.
So a better way to comment is to do something like this.
X stores a person's age, okay?
So if I had originally created x
because I wanted to store somebody's age
then I should comment that.
I should say, hey, this variable's got age in it.
You know how old, how many years somebody is.
This is now a much better comment,
explains what x is.
So I can run it and it runs fine.
I might wanna comment y.
I can say this stores the price of an object, right?
Maybe that's what I had envisioned for y.
So those are single line comments.
Single line comments,
again, pound sign and a short phrase or short sentence
telling about the code.
We typically put the comments
right before the code we're explaining.
Single line comments.
It's possible that
we have some really complicated things to say
and we wanna put in a comment that's more than one line.
So let's take a look at how to do that.
So we could do it with a bunch of pound signs.
So for example, here in this code snippet,
I could say something like
the type command will show
the type of the variable.
So now I have a comment that's multiple lines.
So one way to do that is to show two pound signs.
Keep putting a pound sign
in front of every single line of the comment.
I run it and it runs fine.
Another way to do multiple line comments is
to start it with three double quotes
and then write multiple lines.
This multiple line comment
starts and ends with triple quotes.
I'm using the type command.
Okay, so I can run this one and it also runs fine.
Okay.
So here, in this module,
we've looked at doing a single line comment,
a multiple line comment,
and both of these are using the pound sign.
And then if I wanna do a more extensive comment
and I don't like the pound signs in front,
I can start it and end it with triple quotes.
The other thing I did illustrate in this module is that
the ability of you to go back
to previous code that you've
typed in the Jupyter Notebook
and change it and run it again,
Jupyter will continue to update the code snippet numbers.
And those numbers are an indication to you
of what order you've executed code in.
So your numbers may not exactly match mine
especially if you've typed some things in wrong
and rerun code and change things,
it'll continuing renumber.
But it's not important if you're,
that your code snippet numbers match mine at all,
but it is important that you know what they mean.
So what we've gone over in this lecture is
all about comments,
and I'll see you in the next module.
(upbeat music)

You might also like