Python Statements & Comments

In this tutorial you will learn about the Python Statements & Comments and its application with practical example.

Python Statement

In Python Programming, any executable instruction, that tell the computer to perform a specification action is refer to as statements. These statements are used to build program’s main logic. Program statement can be an input-output statements, arithmetic statements, control statements, simple assignment statements and any other statements and it can also includes comments.

Multi-line statement:- In Python, each of the executable statement must be ended with a newline character. But, if it required you can extend a statement over multiple lines explicitly with the line continuation character (\) as follows –

Example 1:-

You can also extend a statement over multiple lines implicitly using surrounding parentheses ( ), brackets [ ] and braces { }.

Example 2:-

Above multi-line statement is same as in the Example 1

Example 3:-

In Python, if you want to put multiple statements in a single line then each of the executable statement must be ended with semicolon, as follows –

Python Indentation

In Python, a block of code is defined using line indentation, any block of code is started with indentation and ends with the first un-indented line. The amount of spaces is an indentation must be consistent throughout that same block. For example –

Below is a valid block of code –

Below block of code result into an IndentationError error –

The use of indentation makes the code look clean and consistent.

Python Comments

In Python, Comments are a set of statements that are ignored by the python interpreter. The use of comments makes it easy for humans to understand the source code.Usually comments gives you inside or explanation about the variable, method, class or any statement that exists in source code. The comment statements are ignored during the execution of the program.

Single-line Comments:-

A hash sign (#) is used to specify a single line comment, which extends up to the newline character.

Multi-line Comments:-

If you want to comment multiple lines then you can do it using hash sign (#) as follows –

You can do it other way using a triple quotes, either ”’ or “”” as follows –

Docstring in Python

A Python Docstring is a string that is used to specify what a function/class/module does. It usually appear as first statement in a module, function, class, or method definition.

You can write a docstrings using a triple quotes either ”’ or “”” as follows –

the above Docstring is available us as a attribute __doc__ of the function.

In this tutorial we have learn about the Python Statements & Comments and its application with practical example. I hope you will like this tutorial.