Structuring In Python Programming Language

In this lesson, you will learn about basic and must to know things about how should your python programs be structured or formatted.

Firstly we will learn about Statements in python programming. Generally, the interpreter reads and executes all the statements in sequential order mean it first reads line 1 then 2 and ongoing. But remember not all statements execute with this manner there are some exceptional cases like such as conditional statements like python if statement etc. These kinds of statements can change the reading and executing order of interpreter.

Python is a very user-friendly and easy to understand language as most of the statement written on a single line and the interpreter takes a single line as one instruction after encountering the termination of the line. But it is not a hard and fast rule that you should only write single a statement on a single line but you can write multiple statements on a single line. See the example below:

# This example shows single instruction on a single line

print('Welcome To The Python World')

Code Output:

Welcome To The Python World

The above is the example of a single statement on a single line.

Now you will learn how to write multiple statements on a single line. We can write more than one statement on a single line without encountering any error but as far as we concern about good programming practices so, it is not a good practice because in this way python code looks messy and difficult to read.

But other than this if you want to do so you can write multiple statements on a single line by terminating each statement with a semi-colon”;” symbol at the end of each statement. See the example below:

# Below code shows multiple lines of code on a single line

x = 5; y = 10; z = x + y 
print(x); print(y); print(z)

Code Output:


In some cases, when you have to calculate more than one value with variable having long names so, it goes outside the visible part of the screen and you have to scroll left and right frequently to see and change it and it is very irritating. But the good thing is that you can solve this problem by writing a single long statement on multiple lines, and this is called as “line continuation method”. It makes easy to handle long statements and also increases the readability of the code.  See the example below:

# Code is not readable and you have to scroll left or right

a = 10
b = 20
c = 30
no_of_professor = a
no_of_male_students = b
no_of_female_students = c

if (no_of_professor == 10 && no_of_female_students == 30 && no_of_male_students == 20 && (a + b) == 30):
        print('You can take this course')

Now by going deeper in line continuation commonly, there are two types of line continuation which are as follows:

  1. Implicit Line Continuation
  2. Explicit Line Continuation 

Now we will have a look at both of them briefly.

Implicit Line Continuation:

This is the most common, simple and easy type of writing statement that will go on more than a single line. This type of statement generally starts with an opening parentheses “( “, brackets “[” or curly braces “{” like this and continues no matter on what number of lines until all the statements meet their closing parentheses, brackets, and curly braces.  See the example below:

# The code below shows the Implicit Line Continuation
x = [ 
    [0, 1, 2], 
    [2, 3, 0], 
    [8, 4, 0] 

Code Output:

[[0, 1, 2], [2, 3, 0], [8, 4, 0]]

Explicit Line Continuation:

To span statements on multiple lines with explicit line continuation method you have to tell the compiler that you ending the statement here and starts from the next line. For this Backslash( \ ) is used to tell the interpreter that the statement is spanning on multiple lines. Don’t leave empty space or white space at the end of the line but you must end your line with a backslash ( \ ).  See the example below:

# Below code shows you Explicit Line Continuation
addNumbers =  \
    10 + 20  \
    + 51 + 66  \
    + 100


Code Output:



Recommended:  First Program Hello World In Python Programming Language





Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Leave a Reply

Close Menu