
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multi-Line Statements in Python
In Python, statements are nothing but instructions given to a Python interpreter to understand and carry out. These statements are usually written in a single line of code. But, that does not mean Python does not have a provision to write these statements in multiple lines.
There are two types of statements in Python. They are assignment statements and expression statements. Both of them can be broken into multiple lines of statements and Python interpreter will have no problem understanding them.
There are various ways to structure these multi-line statements in Python. Some of them include the following ?
Using the ?' operator
Using Parenthesis ()
Using {} brackets
Using [] brackets
Using \ Operator
Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. If the ?' operator is written at the end of a line, Python interpreter automatically goes to the next line as a continuation to the statement. This is known as explicit line continuation.
Example
In the following example, we are trying to perform a simple arithmetic operation, by adding two numbers. This statement is divided into three lines using the line continuation operator.
total = 12 + \ 22 + \ 33 print(total)
Output
The output will be produced as follows ?
67
Using Brackets
If you do not want to use the line continuation character, multiple lines of a single statement can be grouped together using brackets. These brackets can be parenthesis (), curly brackets {}, or square brackets []. This is known as implicit line continuation and python interpreter will understand them all.
Example
In this example, we will try to group the multi-line statements together using brackets
total = (12 + \ 22 + \ 33) print(total) total = {13 + \ 65 + \ 19} print(total) total = [45 + \ 6 + \ 77] print(total)
Output
On executing the program above, the result is produced as ?
67 {97} [128]