1
00:00:00,000 --> 00:00:04,990
Comments in Python are quite simple. Let's take a look.

2
00:00:05,000 --> 00:00:08,990
We'll start by making a working copy of comments.py,

3
00:00:09,000 --> 00:00:13,990
and we'll call it comments-working.py.

4
00:00:14,000 --> 00:00:15,990
We'll go ahead and open the working copy.

5
00:00:16,000 --> 00:00:20,990
And here we have a little program that generates a list of prime numbers.

6
00:00:21,000 --> 00:00:25,990
Now, you'll notice that at the top of the file, we have a number of lines that

7
00:00:26,000 --> 00:00:27,990
are introduced by a Pound sign.

8
00:00:28,000 --> 00:00:31,990
I call it a Pound sign some people called the Hash Mark, or that tick-tack-toe-

9
00:00:32,000 --> 00:00:34,990
looking thingie, whatever we call it.

10
00:00:35,000 --> 00:00:38,990
That is the symbol that indicates a comment in Python.

11
00:00:39,000 --> 00:00:42,990
So everything starting with the Pound sign, and all the way to the end of the

12
00:00:43,000 --> 00:00:45,990
line, is ignored by the Python interpreter.

13
00:00:46,000 --> 00:00:47,990
So all these are comments.

14
00:00:48,000 --> 00:00:50,990
You'll notice that we don't have any comments in the rest of the code.

15
00:00:51,000 --> 00:00:55,990
And this is a piece of code that may very well benefit from some comments.

16
00:00:56,000 --> 00:01:06,990
So for example, this line here, I might have a comment that says # generate a

17
00:01:07,000 --> 00:01:09,990
list of prime numbers.

18
00:01:10,000 --> 00:01:14,990
And now when somebody comes back and looks at the code later on he'll, see this:

19
00:01:15,000 --> 00:01:16,990
for n in primes generate a list of prime numbers.

20
00:01:17,000 --> 00:01:19,990
Oh, well I guess that's what that does.

21
00:01:20,000 --> 00:01:23,990
Now, when you're reading comments you want to be careful that you don't just

22
00:01:24,000 --> 00:01:26,990
trust the comment, that you actually look at the code and make sure that the

23
00:01:27,000 --> 00:01:28,990
code actually does what you think it does.

24
00:01:29,000 --> 00:01:34,990
Use the comment as a guideline, but oftentimes as people are writing code they

25
00:01:35,000 --> 00:01:38,990
might put in a comment and then maybe change the code later, forget to change

26
00:01:39,000 --> 00:01:44,990
the comment, or perhaps their terminology is a little bit different than what you're expecting.

27
00:01:45,000 --> 00:01:45,990
So comments are very useful.

28
00:01:46,000 --> 00:01:50,990
The purpose of comments is to make the code more readable by human beings,

29
00:01:51,000 --> 00:01:56,990
because something like this here, I wrote it, so I know what it does, but you're

30
00:01:57,000 --> 00:02:00,990
looking at it and you might say, hmm, what algorithm is he using to generate

31
00:02:01,000 --> 00:02:02,990
these prime numbers?

32
00:02:03,000 --> 00:02:06,990
And so a couple of little comments might make it a lot more readable. For example,

33
00:02:07,000 --> 00:02:13,990
I can say # one is never prime by definition.

34
00:02:14,000 --> 00:02:24,990
And over here I might say # found a divisor, not prime.

35
00:02:25,000 --> 00:02:30,990
And over here, because we have a generator function and a lot of people who

36
00:02:31,000 --> 00:02:35,990
aren't familiar with Python might not know what a generator function is,

37
00:02:36,000 --> 00:02:41,990
I can say, # yield makes this a generator.

38
00:02:42,000 --> 00:02:45,990
And it'll at least give somebody a clue. They can look that up if they don't

39
00:02:46,000 --> 00:02:47,990
know what that means.

40
00:02:48,000 --> 00:02:53,990
So the principle here is to use comments to make the code a little bit more clear.

41
00:02:54,000 --> 00:02:59,990
There is a danger, of course, if you use too many comments, if you comment every

42
00:03:00,000 --> 00:03:01,990
line, or if you comment way too many things.

43
00:03:02,000 --> 00:03:03,990
The comments might become a distraction.

44
00:03:04,000 --> 00:03:07,990
But just keep in mind that the purpose of the comment is to make the code more

45
00:03:08,000 --> 00:03:11,990
clear to somebody who is reading it for the first time and may not be familiar

46
00:03:12,000 --> 00:03:13,990
with the algorithm that you are using.

47
00:03:14,000 --> 00:03:18,990
So comments in Python are introduced by a Pound sign or Hash Mark.

48
00:03:19,000 --> 00:03:24,990
And everything from that Pound sign to the end of the line is ignored by the

49
00:03:25,000 --> 00:03:35,000
interpreter, and therefore considered a comment.

