Python Comments - All You Need To Know (And Stuff You Didn't!)
Oct 31, 2022
In Python, comments are lines of text that are not executed as part of the program. Comments allow you to write notes to yourself or other programmers about what your code does. This video covers how to write comments in Python and some best practices for doing so. Article: https://www.ceos3c.com/python/python-comment-multiple-lines/ ⮘-=[⭐All important links in one place⭐]=-⮚ ↬ https://linktree.stefanrows.com ⮘-=[⭐All the stuff I use | Stefan's Amazon Store⭐]=-⮚ ↬ https://www.amazon.com/shop/ceos3ctutorials ⮘-=[⭐Join this channel to get access to perks⭐]=-⮚ https://www.youtube.com/channel/UCDqZyVCTwg9UyRWKgQ7Gizg/join ⮘-=[Subscribe]=-⮚ ↬ https://www.youtube.com/stefanrows?sub_confirmation=1
View Video Transcript
0:00
Hey guys, welcome back to a new video
0:02
In today's video, I want to talk about comments in Python or more specifically, multi-line comments in Python
0:10
Before we dive into some code examples, let's quickly talk about why do we actually need comments
0:14
in programming or more specifically in Python. So usually you use comments for describing
0:20
the functions of your code. So you describe what your code actually does
0:24
Now, this has two reasons. First, you describe this or you comment code for yourself
0:30
in case you need to look that up later again. The longer your program gets
0:35
the harder it will be for you to get back into it if you have put it off for a while
0:40
if it didn't work on your project for a while and you get back to it later on
0:45
there's a high chance that you have forget almost all of the functionality
0:49
if you don't regularly look at it. The other reason for commenting in programming
0:53
is if you work together in a team with other people or if you work for a company
0:58
and you collaborate on a certain project or on a certain piece of code with other people
1:03
Now you will write some code and then somebody else need to do something
1:07
with your code as well. If you don't write a specific comments
1:10
then nobody knows what your code actually does or they might figure it out
1:16
but they have to read all your code and figure it out on their own instead of you just providing information
1:21
You can see it as a piece of documentation and making the life of other programmers and yourself a little bit easier
1:28
Especially if you're a beginner, I highly recommend writing extensive comments describing your own code
1:34
because sometimes we end up following some tutorials where we only understand half of it
1:39
And if you actually write out comments to all of the functions and what they are doing
1:43
that's a great way of solidifying the knowledge. All right, let's look at some examples
1:47
So what is a comment in Python? Let's say we want to print something out
1:51
By the way guys, you can pull up the article that I will link in the video
1:55
Then you can follow along step by step and you can copy all of the code pieces here directly
2:01
into repl.it. I'm using repl.it for this example. If you go on this article and you click on the fork repl button, you can pull it up and
2:08
code along with me. Okay, so the first thing what we do here, we print a normal line of Python code
2:15
So let say this line gets printed You should be familiar with that if you are new This is a print statement in Python And if you just click on run here on the screen run button you can see that this line gets printed So this string of text gets printed out to the console
2:30
Now, what if we create another line, another print statement here, and we say this line doesn't get printed
2:39
And we want that or we want to avoid this line from printing
2:43
So the way of doing a single line comment in Python is by simply putting a hashtag in front of the line
2:49
and if we now run that, this line gets ignored by the compiler
2:54
and is not printed out, or it gets recognized as a comment and doesn't get printed out
2:58
If we remove this hashtag again, we run the code again, this line does in fact get printed
3:04
So a way to do that again is simply putting in hashtag, and what I usually like to do
3:08
is put a little space between the hashtag and the actual comment line. What you can also do is
3:14
In case you want to set a comment behind a single line of code
3:19
you can just go at the end of the line of this code and put a hashtag here and put a comment
3:25
This is a comment right behind the line of code. So this is something you will see pretty regularly that people do that in that way
3:34
I usually like to write comments above my code rather than to the side of it
3:38
but that's completely up to personal preference here. All right, let's comment this line out again
3:43
So this probably raises the question then how do we comment out multiple lines of code in case we want to test something for debugging or just cutting something out of our code that we don't want to run for a while and stuff like this or just simply writing multi-line comments that are more informative
4:02
So we can do that in a very simple fashion. There are actually two ways to go about this, and there is no set way
4:08
Per default, there is no real multiple line comment method in Python like there is in JavaScript
4:14
where you can write multi-line comments and stuff like this with a special syntax
4:19
That's not available to Python out of the box. But there is one method that allows you to comment out multiple lines
4:25
and that is by using triple-double quotes. So if we put triple double quotes here and we put anything really in here print
4:35
We don't want Any of this I just going to copy the code from the article So it easier for you guys to follow along in the end All right and then we do print to be printed Okay so this would be a normal print statement and as you can see
4:53
it already doesn't recognize it as code so it is already commented out and if
4:59
we would go ahead and we would remove that here then we can actually go ahead and print those two lines and then if we go ahead
5:07
and put triple quotes in here again, we can go ahead and comment that out
5:12
And if we run it again, then in fact it does not run
5:16
only the first statement from up here does run. Another way to comment out multiple lines in Python
5:22
that you will see regularly is by using the hashtag method once again
5:26
So if you remove those triple double quotes here again, and we just put a hashtag in front of all of those lines
5:33
and we can go on and on forever like this. Some people prefer it that way, Some people prefer it the other way
5:39
As you can see, the syntax is slightly different here. So this text is immediately marked in green here
5:45
Whereas if we put the triple quotes, you can see that the text is marked in orange
5:50
And this really comes down to personal preference. Now, if I would write a huge bunch of text here and there will be multiple lines, like maybe steps, step one, two, step two, and stuff like this
6:02
I would rather not use hashtags because it's kind of it looks a little bit weird and
6:08
I think if you have to write larger comments, then the preferred way would be triple double quotes
6:14
But there is no real rules for that. So you can do that in a way whatever you like better
6:21
Another question I get asked fairly regularly is how to create multi-line comments with VS code
6:28
VS code is probably the most popular editor out there and a lot of Python
6:31
programmers are using VS Code, myself included, and I'm going to quickly show you how to do that
6:37
So I'm launching up WSL2 here in my terminal. If you don't know what that is, we have a whole
6:42
article series on WSL2 that you get your terminal looking exactly like mine here. You find that on
6:48
seosac.com in the WSL2 section that you find from the navigation bar. And if we look in here
6:54
we go to tutorials, and I think I have something created here. Ah, that's not it. Let's see in here
7:01
There we go, we go in this folder. And I think then it Python And comments py so I do code dot to open my VS code And I have a whole course on WSL2 actually
7:15
You can check that out if you want to see more about my developer workflow
7:21
It's available on Udemy. You also find that in the WSL2 section
7:25
Okay, let's make that smaller. And as you can see here, those are some statements
7:30
And I already made a comment in here. You also find this information in the written article
7:34
But to comment out stuff, either a single line or multiple lines, you just highlight the line that you want to comment out and you press control and hashtag to do that
7:44
And to undo that, so to say to uncomment it again, you again mark all of it and you press control and hashtag again
7:51
And the same works for multiple lines here. So you mark multiple lines, you press control hashtag and you press control hashtag again to uncomment it
8:00
Now, depending on your VS code configuration, that can differ a little bit from setup to
8:07
setup because some people have different keyboard layouts. Usually it is control and hashtag
8:14
In some cases it can be that you need to press control, then K on the keyboard and then followed
8:18
by a C for comments. So, you press control K, C to comment things out
8:23
That's also something I regularly see. Alright, guys, that concludes this tutorial
8:27
I hope you liked it. a rather short tutorial on Python comments, there is not really that much we can talk about
8:32
when it comes to comments and multi-line comments in Python. But I think this is the most important information
8:38
and we don't need to unnecessarily inflate this topic. So to summarize, you can comment a single line of Python
8:44
using the hashtag syntax, you can comment multi-line comments in Python using triple, double quotes
8:52
or you can use multiple hashtags in a row and the shortcut to comment out single lines
8:57
multiple lines of Python code in VS code is pressing Ctrl and hashtag or Ctrl K and followed by a C
9:05
Alright guys, thanks for watching. If you like this video, you know what to do. Subscribe and so on and so forth
9:11
I'll see you back in the next one. Thanks for watching. Thank you
#Computers & Electronics