Login
📚 Python for Introductory Statistics
Chapters ▾

1.4 Python Comments

Comment is some text in a computer program that is for the human reader and is ignored by the computer when running the program. In Python one line comments begin with a hash symbol #. Anything written after the keyboard key hash # till the end of the line is considered comment and Python does not execute comments. multiple line comments can be done by enclosing them in triple quotes """. Comments (notes) are useful to explain what the program is doing. See example below.

# find the slope of a line that passes through (2,5) and (4,7)
x1 = 2    # 2 came from the given point (2,5)
y1 = 5
x2 = 4    # 4 came from the second point (4,7)
y2 = 7

m = (y1-y2)/(x1-x2)    # this is the formula for a slope
print(m)

""" Or you can directly substitute the given number in to
the formula. See the code below """

m = (5-7)/(2-4)
print("the second way",m)
Show expected output
1.0
the second way 1.0

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.