Lesson 3 of 60
โฎPrev Nextโฏ

PY-101 ยท Getting started with Python

Comments

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

3of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A comment is text in your code that Python ignores. In Python a comment starts with # and runs to the end of the line. Use comments to explain why code does something, and to switch a line off while you debug.

    • Write single-line comments with #
    • Comment out code while debugging
    • Know what a docstring is

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Comments, running
    1. # This line is a comment. Python skips it.
    2. price = 250 # price in rupees
    3. tax = price * 0.18 # 18% GST
    4. # print("debug:", tax)
    5. print("Total:", price + tax)
    6. """
    7. A string on its own like this is also skipped.
    8. """
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou open code you wrote three months ago. One line multiplies by 0.18 and you have no idea why. A five-word comment would have saved you twenty minutes.
    2. The Python idea# a comment on its own lineA comment is text in your code that Python ignores.
    3. In real lifeWhen a bug appears, developers often comment out one line at a time to find which one causes it. It is faster than deleting and retyping.

    Start with a question

    You open code you wrote three months ago. One line multiplies by 0.18 and you have no idea why. A five-word comment would have saved you twenty minutes.

    Code is read far more often than it is written. A short comment that explains a decision saves the next person, often you, an hour.

    Explain it like I'm 10

    A comment is a sticky note for people. Python sees the # and skips the rest of the line, the way you skip the notes in the margin of a book.

    The technical explanation

    Everything after # on a line is ignored. A comment can sit on its own line or after code on the same line.

    Python has no special multi-line comment. Start each line with #. A string in triple quotes on its own line is ignored too, and when it is the first line of a function it becomes a docstring, the help text for that function.

    Good comments say why, not what. # 18% GST is useful. # multiply price by 0.18 repeats the code.

    Syntax

    # a comment on its own line
    x = 5  # a comment after code

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    When a bug appears, developers often comment out one line at a time to find which one causes it. It is faster than deleting and retyping.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Which character starts a comment in Python?
    2. 2. What does Python do with a comment?
    3. 3. What is a docstring?
    4. 4. What is printed by: print(1) # print(2)
    5. 5. Which comment is most useful?

    Questions people ask

    How do I comment multiple lines in Python?

    Put # at the start of each line. Most editors do it for you: select the lines and press Ctrl+/ (Cmd+/ on a Mac).

    Do comments slow down a Python program?

    No. Comments are thrown away when Python reads the file, before anything runs.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How do I comment multiple lines in Python?

    Put # at the start of each line. Most editors do it for you: select the lines and press Ctrl+/ (Cmd+/ on a Mac).

    beginner Do comments slow down a Python program?

    No. Comments are thrown away when Python reads the file, before anything runs.

    intermediate Which character starts a comment in Python?

    #. Python comments start with #. // is used in C, Java and JavaScript.

    intermediate What does Python do with a comment?

    Ignores it. The interpreter skips comments completely.

    Quick revision

    Remember

    • Write single-line comments with #
    • Comment out code while debugging
    • Know what a docstring is
    • Comment code out to switch it off while you test, but delete it before you share. Old commented-out code confuses the next reader.

    Syntax

    # a comment on its own line
    x = 5  # a comment after code

    Official documentation