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:-
1 2 3 |
x = 1 + 2 + 3 + \ 4 + 5 + 6 + \ 7 + 8 + 9 |
You can also extend a statement over multiple lines implicitly using surrounding parentheses ( ), brackets [ ] and braces { }.
Example 2:-
1 2 3 |
x = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) |
Above multi-line statement is same as in the Example 1
Example 3:-
1 2 3 4 5 6 7 |
Weekdays= ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] |
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 –
1 |
x = 5; y = 10; z = 15 |
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 –
1 2 3 |
if True: print("Hello") print("World") |
Below block of code result into an IndentationError error –
1 2 3 |
if True: print("Hello") print("World") |
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.
1 2 |
# This is a single comment print("Hello World") # It prints Hello World |
Multi-line Comments:-
If you want to comment multiple lines then you can do it using hash sign (#) as follows –
1 2 3 |
# This is a multi line comment, line 1. # This is a multi line comment, line 2. # This is a multi line comment, line n. |
You can do it other way using a triple quotes, either ”’ or “”” as follows –
1 2 3 |
"""This is a multi line comment, line 1. This is a multi line comment, line 2. This is a multi line comment, line n.""" |
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 –
1 2 3 |
def triple(n): """Function to triple the value""" return 3*n |
the above Docstring is available us as a attribute __doc__ of the function.
1 2 |
>>> print(triple.__doc__) Function to triple the value |