Open In App

Structuring Python Programs

In this article, you would come to know about proper structuring and formatting your python programs.

Python Statements In general, the interpreter reads and executes the statements line by line i.e sequentially. Though, there are some statements that can alter this behavior like conditional statements.
            Mostly, python statements are written in such a format that one statement is only written in a single line. The interpreter considers the ‘new line character’ as the terminator of one instruction. But, writing multiple statements per line is also possible that you can find below.
Examples:




# Example 1
  
print('Welcome to Geeks for Geeks'

Output:

Welcome to Geeks for Geeks




# Example 2
  
x = [1, 2, 3, 4]
  
# x[1:3] means that start from the index 
# 1 and go upto the index 2
print(x[1:3])  
  
""" In the above mentioned format, the first 
index is included, but the last index is not
included."""

Output:
[2, 3]

Multiple Statements per Line We can also write multiple statements per line, but it is not a good practice as it reduces the readability of the code. Try to avoid writing multiple statements in a single line. But, still you can write multiple lines by terminating one statement with the help of ‘;’. ‘;’ is used as the terminator of one statement in this case.
        For Example, consider the following code.




# Example
  
a = 10; b = 20; c = b + a
  
print(a); print(b); print(c)

Output:

10
20
30

Line Continuation to avoid left and right scrolling
Some statements may become very long and may force you to scroll the screen left and right frequently. You can fit your code in such a way that you do not have to scroll here and there. Python allows you to write a single statement in multiple lines, also known as line continuation. Line continuation enhances readability as well.

# Bad Practice as width of this code is too much.
 
#code
x = 10
y = 20
z = 30
no_of_teachers = x
no_of_male_students = y
no_of_female_students = z
 
if (no_of_teachers == 10 and no_of_female_students == 30 and no_of_male_students == 20 and (x + y) == 30):
    print('The course is valid')
 
# This could be done instead:
 
if (no_of_teachers == 10 and no_of_female_students == 30
    and no_of_male_students == 20 and x + y == 30):
    print('The course is valid')

Types of Line Continuation
In general, there are two types of line continuation

Article Tags :