Structuring Python Programs


Structuring Python Programs

Structuring Python Programs

In this article, you’d come back to understand regarding correct structuring and info your python programs.

Python Statements generally, the interpreter reads and executes the statements line by line i.e consecutive. Though, there area unit some statements which will alter this behaviour like conditional statements.
Mostly, python statements area unit written in such a format that one statement is just written in an exceedingly single line. The interpreter considers the ‘new line character’ because of the killer of 1 instruction. But, writing multiple statements per line is additionally doable that you just will realize below.
Examples:

# Example 2

x = [1, 2, 3, 4]

# x[1:3] means that start from the index
# 1 and go up to the index 2
print(x[1:3])

“In the above-mentioned format, the first
the index is included, but the last index is not
included.”””

python programs to practice

Multiple Statements per Line we will conjointly write multiple statements per line, however, it’s not an honest apply because it reduces the readability of the code. attempt to avoid writing multiple statements in an exceedingly single line. But, still, you’ll be able to write multiple lines by terminating one statement with the assistance of ‘;’. ‘;’ is employed because of the killer of 1 statement during this case.
as an example, contemplate the subsequent code.

# Example

a = 10; b = 20; c = b + a

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

Line Continuation to avoid left and right scrolling
Some statements could become terribly long and should force you to scroll the screen left and right often. you’ll be able to suit your code in such how that you just don’t ought to scroll here and there. Python permits you to jot down one statement in multiple lines, additionally referred to as line continuation. Line continuation enhances readability additionally.

Types of Line Continuation
In general, their area unit 2 sorts of line continuation

Implicit Line Continuation

This is the foremost easy technique in writing an announcement that spans multiple lines.
Any statement containing gap parentheses (‘(‘), brackets (‘[‘), or curling braces (‘plausible to be incomplete till all matching parentheses, sq. brackets and curling braces are encountered. Until then, the statement is often implicitly continuing across lines while not raising miscalculation.
Examples:

# Example 1

# The following code is valid
a = [
[1, 2, 3],
[3, 4, 5],
[5, 6, 7]
]

print(a)

# Example 2

“This example will demonstrate
multiple comments “””

“The following
a variable contains the
string ‘How old are you?’
“””
a = ‘How old are you?’

“The following statement prints
what’s inside the variable a
“””
print(a)

python programs

Explicit Line Continuation
Explicit Line connection is employed principally once implicit line connection isn’t applicable. during this technique, you have got to use a personality that helps the interpreter to grasp that the actual statement is spanning quite one line.
A backslash (\) is employed to point that an announcement spans quite one line. the purpose is to be noted that ” should be the last character therein line, even white-space isn’t allowed.
See the subsequent example for clarification

# Example

x = \
1 + 2 \
+ 5 + 6 \
+ 10

print(x)

Comments in Python

Writing comments within the code square measure vital and that they facilitate in code readability and conjointly tell additional regarding the code. It helps you to put in writing details against an announcement or a bit of code. Interpreter ignores the comments and doesn’t count them in commands. during this section, we’ll learn the way to put in writing

# Example 1

####### This example will print Hello World ####### print(‘Hello World’) # This is a comment

comments in Python

Symbols used for writing comments embrace Hash (#) or Triple Double Quotation marks(“). Hash is employed in writing single-line comments that don’t span multiple lines. Triple Quotation Marks square measure wont to write multiple line comments. 3 triple quotation marks to start out the comment and once more 3 quotation marks to finish the comment.
Consider the subsequent examples:

# Example 2

“This example will demonstrate
multiple comments “””

“The following
a variable contains the
string ‘How old are you?’
“””
a = ‘How old are you?’

“The following statement prints
what’s inside the variable a
“””
print(a)

Structuring Python Programs
Python programming language concept. Woman developer with her hand holding a modern sign with word python, on wire background.
YouTube Channel

Important differences between Python 2.x and Python 3.x

Python The new generation Language

Have any Question or Comment?

Leave a Reply

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

Categories

%d bloggers like this: