TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Comment vs Multiline Comment
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Python Comment vs Multiline Comment

Last updated: Nov 06, 2023 10:14 pm
By Meenakshi Agarwal
Share
7 Min Read
Python Comment, Multiline Comment, & DocString
SHARE

Comments in Python are lines of code that are ignored by the interpreter. Python supports single-line and multi-line comments. A single-line comment makes the interpreter skip one line of code while a multiline comment can disable more than one line from execution.

Contents
Single-line commentsPython multiline commentUsing comments to explain the purpose of a functionUsing comments to document the codeUsing comments to prevent execution

Writing comments is a good programming practice. They are non-executable parts of the code, yet quite essential in a program. These not only help other programmers working on the same project but the testers can also refer them during white-box testing.

Must Read: How do you comment on multiple lines in Python?

What are Comments in Python?

Comments in Python are non-executable lines of text used to provide explanations, notes, or annotations within the code. They are ignored by the Python interpreter, but they are displayed in the interpreter’s output so that you can see them.

How to write comments in Python

There are two ways to write comments in Python:

Single-line comments

Single-line comments start with a hash symbol (#) and continue to the end of the line.

# Calculate the area of a rectangle
area = length * width

# Increment the count of wizards by one
wizards += 1  # Magical!

# TODO: Implement the magic_sort function to sort the elements in ascending order
def magic_sort(elements):
    # ... magical implementation ...

Also Check: Different ways to create multiline strings in Python

Python multiline comment

Multi-line comments start with three double quotes (""") or three single quotes (''') and end with the same punctuation.

'''
This is a multi-line comment.
It can span across multiple lines.
'''
def add_numbers(a, b):
    """
    This is another multiline comment.
    It can also span across multiple lines.
    """
    return a + b

Both triple single quotes and triple double quotes are equivalent and can be used interchangeably to create multiline comments.

While you are here, let us share one of our most followed resources, 100 Python interview questions. Whether you are preparing for a job interview or want to put your skills to the test, we highly recommend checking them out.

Python comment block

A Python comment block, also known as a multiline comment or docstring, is a section of code that is ignored by the Python interpreter. It is used to explain the code to other programmers, or to yourself so that you can understand what the code is doing.

In Python, a comment block can be created using three double quotes (""") or three single quotes ('''). The comment block must start and end with the same punctuation, and the text between the punctuation is ignored by the interpreter.

Multiline comment, docstring, and comment block are interchangeable terms.

For example, the following code creates a comment block:

"""This is a comment block.
It can be used to explain the code or to prevent the interpreter from executing certain lines of code."""

Comment blocks are a great way to explain what your code is doing. They can also be used to prevent the interpreter from executing certain lines of code. For example, if you are debugging your code, you can use comments to temporarily disable certain lines of code.

Examples of comments in Python

Here are some unique, practical, and innovative examples of comments in Python:

Using comments to explain the purpose of a function

The following code defines a function called calculate_area(). The comment block explains what the function does, and how to use it.

def calculate_area(width, height):
  """
  This function calculates the area of a rectangle.

  Args:
    width: The width of the rectangle.
    height: The height of the rectangle.

  Returns:
    The area of the rectangle.
  """

  area = width * height
  return area

Using comments to document the code

The following code defines a class called Circle. The comment block explains what the class does, and what its attributes and methods are.

class Circle:
  """
  This class represents a circle.

  Attributes:
    radius: The radius of the circle.

  Methods:
    __init__(self, radius):
      Initializes the circle.

    calc_area():
      Calculates the area of the circle.

    calc_circumference():
      Calculates the circumference of the circle.
  """

  def __init__(self, radius):
    self.radius = radius

  def calc_area(self):
    area = math.pi * self.radius ** 2
    return area

  def calc_circumference(self):
    circumference = 2 * math.pi * self.radius
    return circumference

Using comments to prevent execution

The following code defines a function called debug(). The function is commented out, so it will not be executed. This can be useful for debugging code, or for temporarily disabling code that you are not currently using.

# def debug():
#   print("This is a debug function.")

# debug()

We hope these examples give you some ideas for using comments in your own Python code.

Wrap up – Python comment and docstring

Comments, comment blocks, and docstrings add value to a program. They make your programs more readable and maintainable. Even if you need to refactor the same code later, then it would be easier to do with the comments available.

Software spends only 10% time of its life in development and rest of 90% in maintenance.

Hence, always put relevant and useful comments or docstrings as they lead to more collaboration and speed up the code refactoring activities.

If you get something new to learn from this tutorial, do share it with others and follow us on our social media accounts to see more of this.

Best,

TechBeamers

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python Statement, Multi-line Statement, Expression And Indentation Python Statement and Indentation Explained
Next Article Python Data Types - Learn from Basic to Advanced 8 Most Important Python Data Types to Know

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x