Describing Code

Programs can be quite complicated and are often thousands of lines long. It can be helpful to write a comment describing parts of the code so that when you or someone else reads it the meaning is clear.

In Python, any time the # character is encountered, Python will ignore the rest of the line. This allows you to write English sentences:

 >>>​​ # Python ignores this sentence because of the # symbol.

The # symbol does not have to be the first character on the line; it can appear at the end of a statement:

 >>>​​ ​​(212​​ ​​-​​ ​​32)​​ ​​*​​ ​​5​​ ​​/​​ ​​9​​ # Convert 212 degrees Fahrenheit to Celsius.
 100.0

Notice that the comment doesn’t describe how Python works. Instead, it is meant for humans reading the code to help them understand why the code exists.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.70.247