<< Chapter < Page Chapter >> Page >
Listing 3 . Blank lines and whitespace.
a = 3 # spaces and commentb = 4 # tab and commentc = (a # continue on next line + b)# a blank line follows print(c)# the output is 7

Indentation

In every other programming environment that I have worked with in the past, indentation is used strictly for cosmetic or readability purposes.

However, indentation is not used strictly for cosmetic or readability purposes in Python. It is such an important topic in Python that I haveseparated it into a major section in this module.

Indentation is used to determine grouping of statements

Unlike most other programming environments, physical indentation is used in Python to determine the grouping of statements.

A blessing and a curse

This can be both a blessing and a curse. The blessing is that it forces you to use proper indentation, which usually leads to more readable code.

The curse is that if you don't use proper indentation, your script probably won't behave properly.

No safety nets

There are few if any safety nets in Python (such as the matching curly brackets used in C, C++, and Java) to protect you from indentation errors.

To make matters worse, such errors often turn up as logical errors (meaning that the script simply doesn't work correctly) rather than interpreter errors (meaning that the interpreter will tell you about the error) .

An example of correct indentation

Although I haven't introduced you to the if statement yet, you probably have a fairly good idea what it is used for. I am going to use it to illustrate theproper indentation of a group of statements with the script in Listing 4 .

Description of an "if" statement:

An if statement means that if some expression evaluates to true , the program will do something specific. Otherwise, the script won't do that specificthing.

Don't be too concerned if the logic of the script in Listing 4 escapes you at this point in time. I will explain the use of the if statement in detail in a futuremodule. The important thing here is to understand the grouping of statements.

Listing 4 . An example of correct indentation.
A = 3 B = 4if B>A: print(A) # begin groupprint(B) print(A + B) # end groupA = 6 # not part of above group print(A)#============================================================ #The output, which is not part of the script, is shown below.3 47 6

Compare A with B and take appropriate action

The script in Listing 4 compares the value of the variable A with the value of the variable B to determine if the value of B is greater than the value of A (if B>A:) .

If the value of B is greater than the value of A (which it is in this case) , the three indented print statements are executed. Otherwise, that group of threestatements is bypassed.

A compound statement

The three statements shown with indentation are either all executed, or they are all bypassed. Hence, they behave as a group.

A group of statements like this is sometimes referred to as a compound statement (a statement made up of two or more individual statements) .

Group behavior

When the three indented statements are executed as a group, the values 3, 4, and 7 are printed on consecutive output lines by the three print statements in the groupof statements, as shown in Figure 2 .

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask