Site icon DataFlair

Python Comment | Python Indentation | Python Statement

Python Comment, Python Indentation and Python Statement

Python course with 57 real-time projects - Learn Python

In this tutorial, we will revise basic syntax- Python Comment, Python indentation, and a Python statement with their subtypes, syntax, and examples.

So, let’s start learning. Do check the Popular Python Interview Questions from this topic at the end.

Python Statement

The Python interpreter deals with statements. The following is a Python statement.

>>> a*=3

1. 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=\
    10\
    +20
>>> a

Output

30
>>> "Hello\
hi"

Output

‘Hellohi’

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

>>> a=(
                10+
                20)
>>> a

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Output

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

2. Multiple Python Statement in One Line

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

>>> a=7; print(a)

Output

7

You can also do this to an if-statement.

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

Output

2

3. Strings Python Statements

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

>>> "Hello 'user'"

Output

“Hello ‘user'”

If you use double quotes outside, use single quotes wherever you need to use a quote inside.

4. Blank Lines Python Statements

The interpreter simply ignores blank lines.

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:
                print("2")

Output

2

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:
                print("1")
                 print("2")

Output

SyntaxError: unexpected indent

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
>>>

1. 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
Hi""")

Output

Hello
Hi

2. 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, you will learn what a python function/class does.

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

Output

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

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

Output

‘\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():
	print("Hi")
	"""
	This function prints Hi
	"""           
>>> sayhi.__doc__
>>>

Python Interview Questions on Python Indentation, Comment and Statement

  1. What are the various types of Python Statements?
  2. How to write comments in Python?
  3. What is the use of Hash # in Python
  4. Why do we need proper indentation in Python?
  5. Write a code explaining Docstring Python Comment.

Conclusion

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

Don’t forget to try out your own combinations.

Exit mobile version