Python statement indentation and comments

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

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

Author

  • Tanmay Sinha

    Founder & CEO of MrProgrammer.in | I help Learning Freaks to Step into the World of Programming and Our Mission is to Provide High Quality and Valuable Content to Our Users! We believe that everyone has the potential to learn to code, and we are committed to making that happen. Our courses are designed to be engaging and interactive, and we offer a variety of learning paths to meet the needs of all learners. We are also committed to providing our learners with the support they need to succeed, and we offer a variety of resources to help learners learn at their own pace. If you are interested in learning to code, we encourage you to check out our courses. We are confident that you will find our content to be high-quality, valuable, and makes sense.

One comment

Leave a Reply