Questions

What are the Different Types of Comments in Python?

Answer: Comments in Python are mainly of four types:

  1. Single-line comments

Example: 

                    

# List of numbers

numbers = [76, 5, 3, 68, 43, 23, 54, 42, 11]

  1. Multi-line comments

Example:

                    

# Python code is
# easy to understand 
# with comments

  1. String literals as comments

Example:

                    

""I can write a comment for list of numbers like this""
numbers = [76, 5, 3, 68, 43, 23, 54, 42, 11]

 Here, the sentence in the quotes is a string literal that acts as a comment. This is because it is not assigned to a variable.

  1. Python docstrings

Example:

                    

def hello(name):

    """

    This function greets
    the person passed as
    a parameter
    """

    print("Hello, " + name + ". How are you?")

hello('Lisa')

 Output:

In the above code, the lines written between the triple quotes are a docstring, and they explain the hello() function.

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.