Python Comment | Python Indentation | Python Statement

1. Python Comment, Python Indentation and Python Statement

Now that we’re getting used to Python and its ways, we think it is time to sharpen python syntax in our brains. In this tutorial, we will revise basic syntax- Python Comment, Python indentation, and a Python statement with their sub types, syntax and examples.

So, let’s start learning Python Comment, Python Indentation, and Python Statement.

Python Comment, indentation, and Python Statement

Python Comment, indentation, and Statement

2. Python Statement

The Python interpreter deals with statements. Through our journey, we have seen the conditional statements in python like ‘if’, ‘for’, ‘while’, and so on. The following is a Python statement.

>>> a*=3
Python Comment - Python Statement

Python Comment – Python Statement

a. Multiline Python Statement

In Python, every statement ends with a newline character. But like we have seen, it is possible to span a statement over multiple lines. We do this using the ‘\’ character.

>>> a=\
>>> a


>>> "Hello\

But you can also use a set of parentheses for this.

>>> a=(
>>> a


>>> type(a)
<class 'int'>

Read: Syntax in Python with Examples

b. Multiple Python Statement in One Line

You can easily put multiple statements in python on one line.

>>> a=7; print(a)

You can also do this to an if-statement.

>>> if 2>1: print("2")


c. Strings Python Statements

To declare strings in python, you may use single or double quotes.

>>> "Hello 'user'"

“Hello ‘user'”
If you use double quotes outside, use single quotes wherever you need to use a quote inside.

d. Blank Lines Python Statements

The interpreter simply ignores blank lines.

3. Python Indentation

Unlike C++ or Java, Python does not use curly braces for indentation. Instead, Python mandates indentation. At this point, our inner monsters are laughing at the lazy programmers around us.

>>> if 2>1:

There are no strict rules on what kind of Python indentation you use. But it must be consistent throughout the block. Although, four whitespaces are usually preferred, and tabs are discouraged. Let’s take an example with an inconsistent indentation in python.

>>> if 2>1:

SyntaxError: unexpected indent

Python Interview Questions

4. Python Comment

Python Comment is a programmer’s tool. We use them to explain the code, and the interpreter ignores them. You never know when a programmer may have to understand code written by another and make changes to it. Other times, give your brain a month’s time, and it might forget what it once had conjured up in your code. For these purposes, good code will hold comments in the right places.
In C++, we have // for single-lined comments, and /* … */ for multiple-lined comments. Here, we only have single-lined python comment.
To declare a Python comment, use the octothorpe (hash) (#).

>>> #This is a comment
Python Comment - Types

Types of Python comment

Read: Python Function Arguments with Types, Syntax and Examples

a. Multiline Python Comment

To have multiline python comment in your code, you must use a hash at the beginning of every line of your comment in python.

>>> #Line 1 of comment
>>> #Line 2 of comment
>>> #Line 3 of comment

You can also use triple quotes (‘’’ ‘’’ or “”” “””) for this purpose.

>>> """This comment
is spanned across
multiple lines"""

‘This comment\nis spanned across\nmultiple lines’
This gives us an output because we type it in the shell. When you create a file and write this in that, there is no output. While triple quotes are generally used for multiline python comment, we can conveniently use them for python comment as well.
Triple quotes will also preserve formatting.

>>> print("""Hello

Read: Python Functions with Syntax and Examples

b. Docstrings Python Comment

A docstring is a documentation string in Python. It is the first statement in a module, function, class, or method in Python. In this, we explain what a python function/class does.

>>> def sayhi():
	This function prints Hi
>>> sayhi()

To check a function’s docstring, use its __doc__ attribute.

>>> def sayhi():
	This function prints Hi
>>> sayhi.__doc__

‘\n\tThis function prints Hi\n\t’
The interpreter is unable to get the docstring to a function if it isn’t the first thing in the python function.

>>> def sayhi():
	This function prints Hi
>>> sayhi.__doc__

Read: Applications of Python Programming Language

So, this was all about Python indentation, comment and statement. Hope you like our explanation.

5. Python Comment – Conclusion

Hope we’ve reconnected you to some basics python Syntax- Python indentation, comments and statement. Don’t forget to try out your own combinations. If you don’t get something in Python indentation, Comment and statement ask us in the comments.
Advantages and Disadvantages of Python.
For reference

10 Responses

  1. jackalex007 says:

    Thanks For Giving the Great Information about the Python Now a Days This Programming Language more useful for the people

    • DataFlair Team says:

      Hi Jackalex,
      Glad to see your review on Python Comment. Yes, Python is getting popularity and its demand is also increasing day by day. You can check our Python Career Opportunities to see the latest python jobs and career options.

  2. Monika says:

    thanks for sharing nice information….

    • DataFlair Team says:

      Hello Monica,
      Thanks for reviewing Python comment tutorial. We have complete list of tutorial to learn Python from beginning to the expert level. Please them us well and share with your peer groups.

  3. JYOTI SHARMA says:

    while checking the docstring of the function , it gives a attribute error
    stating ‘function’ object has no attribute ‘_doc_’

    • DataFlair Team says:

      Hi Jyoti,
      Are you sure you’re doing it right? This code works:

      >>> def sayhello():
      This is the documentation comment of the function sayhello

      >>> sayhello.__doc__
      ‘\n\tThis is the documentation comment of the function sayhello\n\t’
      Use double quotes around doc. Hope this will solve your Python comment query.
      Keep sharing and keep learning from DataFlair

    • padmaja says:

      hi jyothi,
      use two underscores before and end of the doc

  4. raj says:

    hi, very nice explanation but the problem is you guys giving a good example I don’t see any answers
    as of my thoughts, you guys want to try us but if guys provide the answers is also very helpful to every one
    thank you.

    • DataFlair Team says:

      Hello, Raj

      We are glad our reader are taking interest in Python comments, indentation and statements tutorials. Here, we have followed each piece of code with its output (or error information), if any. In case we have misunderstood your query, please let us know.

  5. kannanvengi says:

    Nice Article.Learn to more information,thanks for sharing in this post,Its Wonderful job.
    these tips are really helpful.keep it up.Thanks for sharing the information.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.