Multi-line Statements
A brief introduction to multi-line statements and usage.
Python codes are merely just text, aka physical lines. Python compiles physical lines into logical lines then tokenise them.
Physical line end with a physical newline character. Logical line end with a logical NEWLINE token.

Physical newline vs logical newline

Physical newlines are ignored in order to combine multiple physical lines into a single logical line of code terminated by a logical NEWLINE token.
Conversion from physical line to logical line can either be implicit or explicit.

Implicit

Implicit conversion of multiple physical lines into a logical line happen automatically by Python without the user.
These expressions support are implicitly convert multiple physical lines into a logical line:
  • list literals: [ ]
Python implicitly understand that there is continuation of lines
Simple list
Comment within a list
1
[2,
2
4,
3
6]
Copied!
1
[2, #comment 1
2
4, #comment 2
3
6 #comment 3
4
]
Copied!
  • tuple literals: ( )
  • dictionary literals: { }
  • set literals: { }
  • function arguments / parameters
    • supports inline comments
Create function
Call the function
Output
1
def new_fun(value_1, #comment
2
value_2):
3
sum = value_1 + value_2
4
return print(sum)
Copied!
1
new_fun(10, # first value
2
20)
Copied!
1
30
Copied!

Explicit

Explicit conversion of multiple physical lines into a logical line happen where the user explicitly tells Python.
You can break up statements over multiple lines explicitly, by using the (backslash) character. Multi-line statements are not implicitly converted to a single logical line.
When we write codes, we do not want to have very long lines and have to keep readability in mind. In this example the code works as intended.
Code
Output
1
value_1 = 3
2
value_2 = 15
3
value_3 = 100
4
if value_1 < 5 and value_2 > 10 and value_3 > 50:
5
print("conditions are met")
Copied!
1
conditions are met
Copied!
But this one fails because Python cannot implicitly understand that there is continuation.
Code
Output
1
if value_1 < 5 and
2
value_2 > 10 and
3
value_3 > 50:
4
print("conditions are met")
Copied!
1
File "<ipython-input-2-c6eef7781e00>", line 1
2
if value_1 < 5 and ^
3
​
4
SyntaxError: invalid syntax
Copied!
We can explicitly tell Python that there is continuation by adding backslash \.The code runs successfully and prints the below output.
Code
Output
1
if value_1 < 5 and \
2
value_2 > 10 and \
3
value_3 > 50:
4
print("conditions are met")
Copied!
1
conditions are met
Copied!
We can also use parenthesis ()to explicitly tell the physical lines about continuation.
Code
Output
1
if (value_1 < 5 and
2
value_2 > 10 and
3
value_3 > 50):
4
print("conditions are met")
Copied!
1
conditions are met
Copied!

Multi-line Strings

Multi-line string literals can be created using triple delimiters (' single or " double)
Single quote multi-line string
Double quote multi-line string
1
'''You can create
2
multi-line string this way'''
Copied!
1
"""You can create
2
multi-line string this way too"""
Copied!
A multi-line string is just a regular string.
Basically, whatever you type is part of your multi-line comment such as tabs, spaces, newlines etc. are part of your multi-line statement
You can use escaped characters (e.g. \n, \t), use string formatting.
Multi-line strings are not comments, although they can be used as such, especially with special comments called docstrings.
​