Structuring Python Programs

Here we will learn how to structure and format Python programs properly.

Python Statements: Except a few statements like the conditional statements, the python interpreter executes the statements sequentially. 

In general, with each new line, a new statement is considered, and the interpreter with each new line character considers termination of an instruction. But we can write multiple statements also as given in the below example.

# Example 1

print(‘Welcome to PST Analytics’)

 

Output:

Welcome to PST Analytics

 

# Example 2

x = [1, 2, 3, 4]

 

# x[1:3] it denotes that begin from the index

# 1 and go upto index 2 

print(x[1:3])

 

“”” In the above example, the starting

index is considered, but the end index is not 

considered.”””

 

Multiple Statements per Line: Multiple statements per line can be written, but it reduces the readability of the written code. Multiple lines can be written with the help of ‘;’. But we should avoid writing multiple statements per line. 

# Example

a = 20; b = 40; c = b + a

print(a); print(b); print(c)

 

Output:

20

40

60

 

Avoid Left and Right Scrolling by Line Continuation: The statements may become very long sometimes and force us to scroll left and right, which can be irritating at times. Python allows the user to write a single lengthy statement in multiple lines known as line continuation.

Types of Line Continuation:

There are two types of line continuation,

Implicit Line continuation

Python presumes all statements consisting of brackets ’[‘, parenthesis ’(‘ and curly bracket ‘{‘ as incomplete and does not show any error. This will continue until and unless all the matching brackets, parenthesis and curly brackets are encountered. Until that time he statement can be implicitly continued.

# Example 1

# The following code is valid

a = [

 [1, 2,

4],

 [4, 7, 8],

 [8, 2, 5]

            ]

print(a)

 

Output:

[[1, 2, 4], [4, 7, 8], [8, 2, 5]]

 

# Example 2

# The following code is also valid

person_1 = 18

person_2 = 20

person_3 = 12

if (

person_1 >= 18 and

person_2 >= 18 and

person_3< 18

):

            print(‘2 Persons should have ID Cards’)

 

Output:

2 Persons should have ID Cards

 

Explicit Line Continuation: It comes in handy when implicit line continuation is not applicable. We take the help of a character to make the interpreter understand that the statement spans more than one line.

The backslash (\) is used as a special character to make the interpreter know that the statement has not ended. We should keep in mind that the last character in the line has to be ‘\’ and not even white space is allowed.

# Example

x = \

            1 + 3 \

            + 8+ 9 \

            + 10

 

print(x)

Output:

31

Comments in Python: These do not take part in the output of the program but helps the users understand the programs written in a better way. Without comments, programming can be very confusing. Comments are used by developers to provide information on the source code used by them. Interpreters do not read the comments, and they do not consider it as a command. 

Hashtags (#) and triple-double quotations (“””) are used to denote comments in Python. While # is used for single-line comments, “”” are used to denote multiple line comments.

# This is a comment

# Print “PST Analytics”

“””

A multi-line comment spans across

more than one line and denoted

by delimiters. It helps the reader to

understand the source code well.

“””

White Spaces:

Generally, the following characters are used as white spaces,

The Python interpreter mostly ignores white spaces and not required. It can be omitted where it is clear where one token ends and the other one begins. This happens when special non-alphanumeric characters are involved.

# Example 1

#whitespace can improve readability

a = 2-2 # Better way is a = 2 – 2

print(a)

 

# Example 2

# Whitespace here can improve readability.

x = 12

tag =(x == 12)and(x<14)

print(tag)

 

“”” Readable form could be as follows

x = 12

tag = (x == 12) and (x < 14)

print(tag)

“””

But white spaces are useful rather necessary when we want to separate the keywords from variables or any other keywords.

# Example

x = [1, 2, 3]

y = 1

“”” Statement provided below is incorrect , and will generate syntax error

z = yin x

“””

 

# Corrected version is written as

z = y in x

print(a)

 

Whitespace as Indentation

Although Python has an easy syntax, special care should be taken in writing the code. Indentation is very useful in writing the python code.

Whitespaces before a statement can have different and significant meaning. Let us understand this with an example.

# Example

print(‘python’) # Correct

 

print(‘python’) # This will generate an error

 

# The error would be ‘unexpected indent’.

 

Leading whitespaces are useful for determining the grouping of statements like in control structures and loops.

# Example

x = 10

 

while(x != 0):

if(x > 4): # Statement 1

print(‘x > 5’) # Statement 2

else:  # Statement 3 

print(‘x < 5’) # Statement 4

x -= 2  # Statement 5 

“””

Statements 1, 3, 5 are on same level

Statement 2 will only be executed if,‘if’ condition becomes true.

Statement 4 will only be executed if,‘if’ condition becomes false.

“””

Output:

x> 4

x> 4

x> 4

x< 4

x< 4

Leave a Reply

Your email address will not be published. Required fields are marked *