Multi-Line Statements in Python
This article will talk about multiline statements in python. And also it will talk about types of continuation which includes Explicit and implicit Line Continuation.
A statement in python is a logical command to the python interpreter to execute tasks. We will be focussing on Multi-Line Statements in Python. Statements in Python are of two types as explained below:
• Assignment Statements: An Assignment statement is used to assign names to python objects. The equal sign (=) is used for the assignment of names to objects, where the entity before the equal sign is the name, and the entity after the equal sign can be any expression that computes an object. It is important to note that the assignment statements create a new reference object instead of copying the object itself.
• Expression Statements: Expression statements are a combination of operators and operands that computes to some value. In these expression statements, the computation is based on the precedence of the operators in the statement. The operands are generally python objects like integers, strings, lists, etc, whereas the operators are generally either mathematical operators like Addition(+), Multiplication(**), or Logical operators like and, or, not, etc.
Now that we are familiar with the concept of statements in python, let’s take a look at the concept of breaking python statements into multiline statements.
Best-suited IT & Software courses for you
Learn IT & Software with these high-rated online courses
Multi-Line Statements in Python
When it comes to extending a python statement into valid multi-line statements we can make use of the following entities:
1. Using Braces -> {}
2. Using parenthesis -> ()
3. Using Square brackets -> []
4. Using Semii-colon -> ;
5. Using continuation character slash ->
Take a look at the below example for reference, where we have extended python single line statements to multiple lines using the above entities.
Example:
# Breaks the statement using
# line continuation character("")
a = "1,
2,
3,
4,"
# Breaks the statement using
# brackets("()")
b = (1,
2,
3,4)
# Breaks the statement using
# braces("{}")
c = {1,2,
3,
4}
# Breaks the statement using
# square brackets("[]")
d = [1,
2,
3,
4]
# Breaks the statement using
# semi-colon(";")
print(a);print(b);
print(c);
print(d);
Output:
1, 2, 3, 4,
(1, 2, 3, 4)
{1, 2, 3, 4}
[1, 2, 3, 4]
Now that we have made ourselves familiar with the concept of extending python statements, let’s take a look at its types.
Types of Line Continuation
Essentially the line continuation type is divided into two categories based on multi-line extension entities used that were mentioned above. Let’s take a look at each of those typings.
1. Explicit Line Continuation
In this form of line continuation, we make use of the line continuation character slash(“”) to extend a python statement into multiple lines. Take a look at the below examples for reference.
Example 1: Using line continuation character slash(“”) on strings to extend the python statement.
# Initializing a text using the
# Explicit multi-line statement.
sample_text = "Naukri
E-learning
platform"
print(sample_text)
Output:
Naukri E-learning platform
Example 2: Using line continuation character slash on mathematical expression to extend the python statement.
# Initializing a mathematical expression
# using the Explicit multi-line statement.
compute = 5 +
7 -
5*
4//
2
print(compute)
Output:
2
2. Implicit Line Continuation
In this form of line continuation, we make use of the parentheses ( ), brackets [ ], and braces { }. to extend a python statement into multiple lines. take a look at the below example for reference.
Example 1: Using line continuation parentheses ( ), brackets [ ], and braces { } to extend a python statement.
# Initializing a dictionary using the
# Implicit multi-line statement.
sample_text1 = {"Naukri",
"E-learning",
"platform"}
# Initializing a tuple using the
# Implicit multi-line statement.
sample_text2 = ("Naukri",
"E-learning",
"platform")
# Initializing a list using the
# Implicit multi-line statement.
sample_text3 = ["Naukri",
"E-learning",
"platform"]
print(sample_text1)
print(sample_text2)
print(sample_text3)
Output:
{‘Naukri’,’ E-learning’, ‘platform’}
(‘Naukri’, ‘E-learning’, ‘platform’)
[‘Naukri’, ‘E-learning’, ‘platform’]
Example 2: Using line continuation characters parentheses ( ), brackets [ ], and braces { } on mathematical expression to extend the python statement.
# Initializing a mathematical expression
# using the Implicit multi-line statement.
compute1 = (5 +
7 -
5*
4//
2)
# Initializing a mathematical expression
# using the Implicit multi-line statement.
compute2 = [5 +
7 -
5*
4//
2]
# Initializing a mathematical expression
# using the Implicit multi-line statement.
compute3 = {5 +
7 -
5*
4//
2}
print(compute1)
print(compute2)
print(compute3)
Output:
2
[2]
{2}
Conclusion
In this article we have managed to explore the following concepts in regard to multi-line statements in Python:
1. What are Python Statements?
2. Types of python Statements
3. Concept of Multi-line Statements in Python
4. Types of Multi-line Statement