Page 60 - Python Basics: A Practical Introduction to Python 3
P. 60

3.5. Leave Yourself Helpful Notes


            # This is my first program.
            # It prints the phrase "Hello, World"
            # The comments are longer than the code!
            greeting = "Hello, World"
            print(greeting)

            You can also use comments to comment out code while you’re test-
            ing a program. Putting a # at the beginning of a line of code lets you
            run your program as if that line of code didn’t exist, but it doesn’t ac-
            tually delete the code.


            To comment out a section of code in IDLE, highlight one or more lines
            to be commented and press:

             • Windows: Alt + 3
             • macOS: Ctrl + 3

             • Ubuntu Linux: Ctrl + D

            To remove comments, highlight the commented lines and press:

             • Windows: Alt + 4

             • macOS: Ctrl + 4
             • Ubuntu Linux: Ctrl + Shift + D


            Now let’s look at some common conventions for code comments.


            Conventions and Pet Peeves

            According to PEP 8, comments should always be written in complete
            sentences with a single space between the # and the first word of the
            comment:


            # This comment is formatted to PEP 8.

            #this one isn't

            For inline comments, PEP 8 recommends at least two spaces between



                                                                          59
   55   56   57   58   59   60   61   62   63   64   65