MyInternships.in

Python Basics

Python Comments

Comments are lines in your code that Python ignores when running the program. They exist purely to help humans understand what the code does.


Single-Line Comments

In Python, anything after a hash symbol (#) on a line is treated as a comment and is not executed. You can put a comment on its own line or after a statement.

Single-line comments
Python
# This program greets the user
name = "Karan"  # storing the user's name
print(name)

Multi-Line Comments

Python does not have a dedicated multi-line comment symbol. The common workaround is to start each line with #, or to use a multi-line string (triple quotes) that is not assigned to anything, which Python simply ignores as an expression.

Multi-line comment workaround
Python
# This function calculates the total price
# including tax and discount
# Author: Learning Python Team

"""
This is technically a string, but since it
is not assigned to a variable, Python
treats it like a multi-line comment.
"""

Docstrings

A docstring is a special string written as the first statement inside a function, class or module to describe what it does. Docstrings use triple quotes and, unlike regular comments, can be accessed using the built-in help() function.

A function docstring
Python
def add_numbers(a, b):
    """Return the sum of two numbers."""
    return a + b

print(add_numbers(4, 5))

Why Comments Matter

  • They explain why code does something, not just what it does
  • They help teammates (and future you) understand the logic faster
  • They make debugging and code reviews much easier
  • Well-documented code is expected in real internship and job projects
💡

Write comments that explain intent and reasoning, not obvious things — the code itself already shows what a simple line does.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships