Comments and Document Strings

Python has two ways to document, or explain, what your code is doing. One way uses code comments; the other uses document strings.

Comments

Comments, unlike the code itself, don't tell Python what to do. They're more like notes to yourself, and to other programmers who may want to modify or edit your code later, that say, “This is why I wrote it this way.” In Python, a comment starts with the pound (#) character, which tells the interpreter to ignore the text after it until the end of the line. Each line of a comment must start with this character.

As the following example shows, you can put comments on their own line or you can add them after a statement.

 #The following example demonstrates #printing hello world ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.