Python Statement, Indentation and Comments
--------------------------------------------------------------------------------------------------------------------
Python Statement
------------------------
A statement is an instruction that the Python interpreter can execute. Or Instructions that a
Python interpreter can execute are called statements.
For example,
a = 1 is an assignment statement.
i=i+j
print('python coding')
if statement,
for statement,
while statement, etc.
Multi-line statement
---------------------------------------------------------------------------------------------------------------------
In Python, the end of a statement is marked by a newline character. But we can make a statement
extend over multiple lines with the line continuation character (\).
Explicit line continuation : For example:
a=1+2+3+\
4+5+6+\
7+8+9
In Python, line continuation is implied inside parentheses ( ), brackets [ ], and braces { }.
For instance, we can implement the above multi-line statement as:
a = (1 + 2 + 3 +
4+5+6+
7 + 8 + 9)
the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [ ] and {
}.
For example:
colors = ['red',
'blue',
'green']
We can also put multiple statements in a single line using semicolons, as follows:
a = 1; b = 2; c = 3
Python Indentation
--------------------------------------------------------------------------------------------------------------------
-Most of the programming languages like C, C++, and Java use braces
{ block of code }
{
# block of code
}
{
{
{
# logic here
}
}
}
to define a block of code.
-Python, however, uses indentation.-A code block (body of a function, loop, etc.) starts with
indentation and ends with the first unindented line.
-The amount of indentation is up to you, but it must be consistent throughout that block.
-Generally, four whitespaces are used for indentation and are preferred over tabs.
Here is an example.
for i in range(1,11):
print(i)
if i == 5:
break
-The enforcement of indentation in Python makes the code look neat and clean. This results in
Python programs that look similar and consistent.
-Indentation can be ignored in line continuation, but it's always a good idea to indent.
-It makes the code more readable
For example:
if True:
print('Hello')
a=5
and
if True: print('Hello'); a = 5
-both are valid and do the same thing, but the former style is clearer.
-Incorrect indentation will result in Indentation Error.
Python Comments
Comments can be used to explain Python code. Comments can be used to make the code
more readable.
Comments can be used to prevent execution when testing code. Comments are very
important while writing a program.
Comments are descriptions that help programmers better understand the intent and
functionality of the program.
Comments are the way to improve the readability of a code, by explaining what we have
done in code in simple english.
They are completely ignored by the Python interpreter.
# this a simple program
They describe what is going on inside a program, so that a person looking at the source code
does not have a hard time figuring it out.
In Python, use the hash (#) symbol or special character to start writing a comment.
Types of Comments in Python
There are two types of comments in Python.
1. Single line comment
2. Multiple line comment
Single line comment
# this is a python program .Python Interpreter ignores comments.
It extends up to the newline character.
Comments are for programmers to better understand a program.
Example 1:
Writing Single-Line Comments
print('Hello world') # printing a string
Output
Hello world
Here, the comment is:# printing a string
This line is ignored by the Python interpreter.
Everything that comes after # is ignored. So, we can also write the above program in a single line
as: print('Hello world') #printing a string
The output of this program will be the same as in Example 1.
The interpreter ignores all the text after #.
Multi-Line Comments in Python
------------------------------------------
Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a # for each line:
comments that extend up to multiple lines. One way is to use the hash(#) symbol at the beginning
of each line. Python doesn't offer a separate way to write multiline comments.
use # at the beginning of each line of comment on multiple lines.
Example 2: Using multiple #
# it is a
# multiline
# comment
print('welcome to happy coding')
Here, each line is treated as a single comment and all of them are ignored.
For example:
#This is a long comment
#and it extends
#to multiple lines
print('welcome to Python')
Example
#This is a comment
#written in
#more than just one line
print("Hello, python World!")
Method2:Triple- quoted strings
Although triple quotes (''' or """) create multi-line strings rather than true comments, they are sometimes
used as comments when the string is not assigned to a variable.
"""
This is a multi-line string.
It is sometimes used as a comment,
but it is not a true comment.
"""
print("Hello")
In a similar way, we can use multiline strings (triple quotes) to write multiline comments.
The quotation character can either be ' or ".
' ' ' ' ' ' or " " " """
Example 3: Using String Literals to write Multi-line Comments
''' I am a multiline comment! '''
print("Hello World")
Here, the multiline string isn't assigned to any variable, so it is ignored by the interpreter.
Even though it is not technically a multiline comment, it can be used as one.