Structuring In Python Programming Language

In this article, 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 lines 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 the order of an interpreter.

Python is a very user-friendly and easy to understand language as most of the statements 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:

Code Output:

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:

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 a “line continuation method”. It makes easy to handle long statements and also increases the readability of the code.  See the example below:

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 what number of lines until all the statements meet their closing parentheses, brackets, and curly braces.  See the example below:

Code Output:

Explicit Line Continuation:

To span statements on multiple lines with an explicit line continuation method you have to tell the compiler that you ending the statement here and start 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:

Code Output:

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 That Come In Seeking Education. This Website Is The Free Source Of Learning About New Technology And Up To Date Knowledge.

Leave a Reply