In this video, learn three ways that comments can help programmers when they're writing code.
- Comments are notes to your future self and others to describe what your code does. Right now, we have small programs. We can keep the flow of it all in our heads, but over time as you continue to grow your programming skills, you'll be writing much longer programs. Some may even be thousands of lines of code spread over dozens of files. It would be impossible to keep all of that in your head. By adding comments to your code, you can document the purpose of the different variables, functions, and classes. You can think of them as virtual sticky notes. Here we are with our program that asks the user for their name and also asks if they're enjoying the course. Comments are started with hash marks. Let's add our very first comment here on line number one. We'll type the hash mark and then a space and type greet the user. Notice how the entire line is now colored green. This is how the IDE is letting us know that this line is a comment. We can go ahead and save it. Now there's another way that you can include comments in your code. You can also add them to the same line as existing code. Let's come down here to line number four, and right after we have our input function, we'll add a space and another hash mark, and we'll type ask the user their name, and just like that, we have another comment. So when Python sees this hash mark, it knows that it can ignore the rest of the line. Let's save this and see if it has any impact on our output. We'll right-click in the file and go to Run Python File In Terminal. Just like before, we get our greeting, and it asks us what's our name. After hitting enter, we're asked if we're enjoying the course. So let's type yes, and we get the same result that we had before. The comments do not impact the output of our code. Other than serving as reminders for us, we can also use comments to help temporarily ignore some code. Programmers use this technique all of the time. It's called commenting out code. Let's try it. Let's say we didn't want to ask the user how they feel about the course anymore. Instead of just deleting those lines of code, we can comment them out. Let's close the terminal and then come to line number seven, and at the very beginning, we'll put a hash mark and a space. Let's do the same thing for lines nine through 12. Hash mark and a space. Once again, on line number 11 and then finally on line number 12. Let's save our file and check the output. We can right-click and go to Run Python File In Terminal. So we get our greeting, and then it asks us for our name. Let's type Jill, and when we hit enter, that's it. It doesn't ask us how we feel about the course. That's pretty cool, right? Comments are a powerful tool for programmers, and as you've seen, we can use them to serve as reminders, provide explanations for other developers, and even help us with debugging code.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages