Comments in Python

Comments in Python are used to provide explanations or notes within the code. They help developers understand the code and make it easier to maintain. Python ignores comments during execution, so they have no effect on the program's output.

Why Use Comments?

  • Improve Readability: Comments make code easier to understand for others (or yourself in the future).
  • Explain Logic: Helps to explain what a certain part of the code is doing.
  • Temporary Disable Code: You can comment out code that you don’t want to run temporarily.
  • Documentation: Provides important details about a function, module, or script.
  • Debugging: Debugging using comments means temporarily disabling some parts of the code by adding a # at the beginning. This helps in testing and fixing programs without deleting the original code.

Types of Comments

  1. Single-Line Comment

    In Python, a single-line comment starts with a # symbol. Anything written after the # on the same line is ignored by the Python interpreter. It is used to explain a specific line or part of the code.

    πŸ’» Example:

    # This is a single-line comment
    print("Hello, From ShikshaSanchar!")  # This comment is after code
    
    Output:

    Hello, From ShikshaSanchar!

    πŸ“ Explanation:

    • The first line # This is a single-line comment is completely ignored when the program runs. It is only for human readers.
    • The second line has a print statement and after it, a comment is added on the same line using #.
    • Adding comments next to the code is useful to quickly describe what that line is doing.
  2. Multi-Line Comments

    Python does not have a separate syntax for multi-line comments like some other languages. But we can use triple quotes β€” either( ''' or """ ) β€” to create block comments.

    πŸ’» Example:

    '''
    This is a multi-line comment
    explaining the next part of the code.
    '''
    
    print("Welcome to ShikshaSanchar")
    Output:

    Welcome to ShikshaSanchar

    πŸ“ Explanation:

    • Triple quotes are usually used for writing docstrings (documentation inside functions), but they can also act as multi-line comments.
    • When placed outside any function or not assigned to a variable, Python will ignore them β€” just like a comment.
    • This helps us explain large parts of code in one go.

Best Practices for Writing Comments

  • Keep comments short and meaningful.
  • Do not repeat the obvious (e.g., # Print Hello above print("Hello")).
  • Update comments if the code changes.
  • Use consistent style and grammar.
  • Use comments to explain why something is done, not just what is done.

Example with Comments

In this program, instead of taking input from the user, we use predefined numbers and add them. Comments are added to explain each step clearly.

πŸ’» Program:

# This program adds two predefined numbers
              
           # Defining two numbers
           num1 = 8
           num2 = 12
              
           # Adding the numbers
           sum = num1 + num2
              
           # Printing the result
           print("The sum is:", sum)

Output:

The sum is: 20

πŸ“ Explanation:

  • num1 and num2 are predefined with values 8 and 12.
  • The + operator is used to add the two numbers.
  • The print() function displays the sum on the screen.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations β€” designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity β€” this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels β€” beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar