The Wayback Machine - https://web.archive.org/web/20190403050752/https://www.geeksforgeeks.org/structuring-python-programs/amp/

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output:
Welcome to Geeks for Geeks
filter_none

edit
close

play_arrow

link
brightness_4
code

# 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."""
chevron_right

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

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 && no_of_female_students == 30 && no_of_male_students == 20 && (x + y) == 30):
        print('The course is valid')

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

Article Tags :