# print random.randint(min,max) + random.randint(min, max) # while roll_again = “yes” or roll_again = “y”:
# Add two random numbers together for a sum While roll_again = "yes" or roll_again = "y":
If you remove the hash sign, you enable the code to test it. Doing so lets you test segments of code with and without new additions.įor example, in this simple dice rolling program, there is a section that’s commented out. It can also be helpful to explain why a particular command is used, as in the example above.īecause comments render text invisible to the parser, you can use them to disable commands. For example, you might explain the purpose of a variable, or leave a note about the type of variable created. Inline comments are used to add context for people reading the code. Run additional command # Tricky part: The program breaks if this line is removed. Add the standard hash sign + space to signify an inline comment: function Use an inline comment to point out the exact spot you want to clarify. The best time to use this option is when explaining a complicated operation. You can comment in the same line as a piece of code using an inline comment. This is usually used to create documentation that’s part of the application and can be accessed at runtime. Docstrings let you put human-readable text into the project. If you place """ in one of the places listed above, Python reads it as a dosctring. The triple-quotation mark can be tricky because in some circumstances it creates a docstring if a triple-quote: As long as you don’t add anything that accesses that string of text, it works the same as a regular comment. This method creates a text constant with no function, not a true comment. If you don’t, you will see a syntax error. Note: It is important that you correctly indent the triple-quote mark. They aren’t parsed and interpreted by the compiler. Instead, use multi-line strings wrapped inside triple-quote marks ( """) to achieve a similar function. Multi-line strings do not work as traditional Python comments, since there is no official multi-line functionality. In such instances, you can use multi-line comments. However, commenting a large section takes a lot of time and you may need a quick way to comment out a whole section.
In general, it is recommended to use # at the beginning of each line to mark it as a comment. These tools can save you time commenting out each line. Some text or code editors for programming (like Notepad++ or Atom) allow you to highlight the text, then mouse-click to mark the block as a comment.
To mark a series of lines as a comment, add a hash sign + space at the beginning of each line: # This is a comment A developer uses them to explain more complex code, especially when working in a team. Block comments are longer-form comments that consist of multiple lines in a row.