Class 11:Python Statements

Python Statement

Instructions that a Python interpreter can execute are called statements.

For example,
a = 1
is an assignment statement.
if statement, for statement, while statement etc. are other kinds of statements which will be discussed later.

 

Multi-line statement

In Python, 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 (\). For example:

>>>a = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9

This is an explicit line continuation.

>>> a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

Output:

>>> a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9
>>> a
45
>>> 

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)

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

Output:

>>> a=(1+2+3+
   4+5+6+
   7+8+9+10)
>>> a
55
>>> 
>>> a=[2+3+4+
   4+5]
>>> a
[18]
>>> 
>>> a={1+2+3+
   4+5+6}
>>> a
{21}
>>> 

Here, the surrounding parentheses ( ) do the line continuation implicitly. Same is the case with [ ] and { }. For example:
colors = [‘red’,
‘blue’,
‘green’]

We could also put multiple statements in a single line using semicolons, as follows
a=10
b=20
c=30

or

a=10; b=20; c=30

Both are same