Skip to content
Home » Blog » Python Statement, Indentation, and Comments | Python Tutorial #3

Python Statement, Indentation, and Comments | Python Tutorial #32 min read

Python statement indentation and comments

In this Tutorial we will be learning about Statements, Indentation & Comments In Python, So if you Have Not Read the Previous Parts of this Course You can Access them Here:

Python Statement

Statements that the Python interpreter can execute are called statements. For Example a = 6 where there is value assigned to a variable. There are Statements In Python Like if statement, for statement & while statement.

Multi-line statement

In Python, the end of a statement is marked with a newline character. However, you can use the line continuation character () to span statements across multiple lines. for example:

Example:

variable = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(variable )

This is explicit line continuation. In Python, line continuation is implied within brackets ( ), square brackets [ ], and braces { }. For example, the multiline statement above could be implemented as follows:

a = (1 + 2 + 3 +
    4 + 5 + 6 +
    7 + 8 + 9)

Here, the surrounding parentheses ( ) implicitly take over line continuation. The same goes for [ ] and { }. for example:

colors = ['pink',
          'yellow',
          'white']

Here, we can also go for this:

a = 1; b = 2; c = 3

Python Indentation

Most programming languages ​​such as C, C++, and Java use curly braces { } to define blocks of code. However, Python uses indentation. Blocks of code (function bodies, loops, etc.) start with an indent and end with the first non-indented line. The amount of indentation is up to you, but must be consistent throughout the block. In general, 4 spaces are used for indentation, tabs are recommended. Here is an example.

if i in in range(100):
print(i)
if i == 20:
 break
# This will give a Indent Error

To Avoid Python Indentation, you must Know how to write a clean code in python, it is a Good practise For a Python Developer. So You can Copy the Following Method to write a Clean Code:

if i in in range(100):
  print(i)
if i == 20:
 break

# this will not give any indent Error

Python Comments

Comments in Python are used to Describe your Code, And Writing Comments in Python is a Good Practise. It extends up to the newline character. Comments are for programmers to better understand a program. Python Interpreter ignores comments.

It is Represented By the Symbol ‘#‘ Example:

# This is a Comment In Python!

Python Multi-line Comments

In order to Write Comments In Multiple Lines, we use Multi-line Comments In Our Python Code, We can Write Multi-line Comments with Triple Quotes. Example:

""" This is 
    the Best Example of
    Multi-line Comment"""

Python Docstrings

As Comments, We also Use Docstrings In Python, to Describe our Code, It is a Short-Documentation About Code. We can add Docstrings in Our Code Using Triple Quotes. For Example:

a = 10
b = 20
"""This will add Both the Number"""
 print(a + b)

<<Keywords & Identifiers In Python | Python Course #2

Tanmay Sinha

1 thought on “Python Statement, Indentation, and Comments | Python Tutorial #32 min read

  1. Pingback: Python Data Types | Python Tutorial #5 - Mr Programmer

Leave a Reply