<< 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

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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