Multi-Line Statements in Python

Multi-Line Statements in Python

3 mins read2.1K Views Comment
Updated on Oct 6, 2022 11:03 IST

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.

2022_10_MicrosoftTeams-image-1.jpg

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.

Recommended online courses

Best-suited IT & Software courses for you

Learn IT & Software with these high-rated online courses

1.11 L
3 years
– / –
5 days
Free
5 hours
Free
6 months
1.68 L
3 years
2.68 L
2 years
Free
7 hours
– / –
5 days
Free
15 hours

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);
Copy code

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.

Find the Second Occurrence of a Substring in Python String
Find the Second Occurrence of a Substring in Python String
During Python programming, you will come across instances where you would need to find the second occurrence of a substring in Python a given string. For example, finding the second...read more
How to use pass statement in Python
How to use pass statement in Python
A pass statement in Python is a null statement that act as a placeholder for future code. In this article, we will briefly discuss how to use pass statement with...read more
Introduction to Iterators in Python
Introduction to Iterators in Python
Have you ever wondered how Python handles looping through collections such as lists or dictionaries? This is where iterators come into play. An iterator in Python is an object that...read more

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)
Copy code

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)
Copy code

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)
Copy code

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)
Copy code

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

About the Author
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski Read Full Bio