Learn to Program Using Python: Program Construction
Something for everyone
Beginners start at the beginning, and experienced programmers jump in further along. Learn to Program using Python: Lesson 1, Getting Started provides an overall description of this online programming course.
Program ConstructionConceptually, programs are composed of statements, and statements are composed of expressions. In practice, you need to know how to construct statements from a physical viewpoint.
A Python program is divided into a number of logical lines. A logical line is constructed from one or more physical lines.
What is a physical line?
A physical line ends with the character(s) used by your platform for terminating lines.
On Unix, this is the linefeed character. On DOS/Windows, it is the carriage return character followed by the linefeed character. On Macintosh, it is the carriage return character.
We refer to this in programming jargon as the newline character (even though it is actually two characters on DOS/Windows systems).
Please be more specific
Basically, a physical line is what you get when you type some characters into your text editor and press the Enter key, the Return key, or whatever you call that key on your keyboard. (Actually you don't even need to type characters before pressing the Enter key, in which case you get blank lines.)
This is the key that causes the cursor to go down to the next line and return to the left side of the editing window.
For example, on my Dell laptop, I pressed the Enter key immediately following the --> in the two lines shown in Figure 1.
will end the line here -->
and end the next line here -->
This produced a newline in each case. I also pressed the Enter key a couple more times following that as well.
A side trip into HTML
Just in case you are interested in HTML, the Netscape Composer program that I am using to create this document inserts a <BR> tag into the HTML code each time I press the Enter key (but that has nothing to do with Python, or computer programming either for that matter).
What is a logical line?
A logical line is constructed from one or more physical lines.
Line joining rules (described later) can be used to construct a logical line from two or more physical lines.
Statements and logical lines
A statement cannot cross logical line boundaries except where the syntax allows for the newline character, such as in compound statements. I will show you an example of a compound statement later in this lesson.
We learned about comments in an earlier lesson. To summarize, a comment starts with a hash character (#) that is not part of a string literal (we also learned about string literals in an earlier lesson). The comment ends at the end of the physical line.
Explicit line joining
You can join two or more physical lines to produce a logical line, using the backslash character as shown in Figure 2.
b = 4
# construct c=a+b
# the output is 7
The backslash character
When a physical line ends in a backslash that is not part of a string literal or comment, that line is joined with the following physical line forming a single logical line.
The backslash and the following end-of-line character are deleted (or ignored by the compiler) and do not become part of the logical line.
In Figure 2, the expression c=a+b is created by joining five consecutive physical lines to create one logical line.
For illustration only
Obviously, this is not how you would want to write a long program, but it is not unusual to break long expressions into two or more physical lines to make them fit onto a prescribed page width.
No spaces or comments allowed
Be careful to make certain that no space characters follow the backslash.
You may not place a comment following the backslash, and a backslash does not continue a comment.
A backslash is illegal elsewhere on a line except inside a string literal.
Implicit line joining
Expressions in parentheses, square brackets, or curly braces can be split over more than one physical line without using backslashes as shown in Figure 3.
b = 4
c = (a # continue on next line
# the output is 7
You can also place a comment on a line that is being continued implicitly as I did in the third line of Figure 3.
Also, you can indent the continuation line however you want. This is very useful for making the code more readable.
What about blank lines?
Lines containing only spaces, tabs, formfeeds, and comments are ignored in scripts, but the behavior may be different in interactive mode, depending on the implementation.
An example of some spaces and blank lines is shown in Figure 4.
# spaces and comment
b = 4
# tab and comment
c = (a # continue on next line
# a blank line follows
# the output is 7
IndentationIn every programming environment that I have worked with in the past, indentation is used strictly for cosmetic or readability purposes.
Not just for cosmetics in Python
However, indentation is such an important topic in Python that I have separated it into a major section in this tutorial lesson.
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 program probably won't behave properly.
No safety nets
There are no safety nets in Python (such as the matching curly braces 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 program simply doesn't work correctly) rather than compiler errors (meaning that the compiler or 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 the proper indentation of a group of statements.
|An if statement means that if some expression evaluates to true, do something specific. Otherwise, don't do it.|
Don't be too concerned if the logic of this program escapes you at this point in time. I will explain the use of the if statement in detail in a subsequent lesson. The important thing here is to understand the grouping of statements.
Compare A with B and take appropriate action
The sample program in Figure 5 compares the value of the variable A with the value of the variable B (if B > A:).
B = 4
if B > A:
The program logic
If the value of B is greater than the value of A (which it is in this case), the three (red) statements are executed. Otherwise, that group of three statements is bypassed.
(Note that I made the statements red here to make them stand out. You would not make them red when actually writing the program.)
A compound statement
The three statements shown in red 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).
When the three 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 group of statements, as shown in Figure 6.
Last statement is not part of the group
Then the value 6 is printed by the last statement in the program, which is not part of the group.
Another sample program
Now, let's make a change. The program shown in Figure 7 is identical to the one above except that I switched the values of A and B to cause the group of red statements to be bypassed (B is no longer greater than A).
B = 3
if B > A:
In this case, the output screen looks something like Figure 8. Only one value (6) is printed because the three print statements in the group were bypassed as a group.
What makes them into a group?
The important point here is that the three red statements constitute a group because of their common indentation level.
(Note that I used boldface and red in the above programs to emphasize certain parts of the program. The original script did not contain boldface and did not contain any red color. Python scripts must be written in plain text, and control codes, such as bold or color, are not allowed.)
I personally don't like the idea of using indentation to create grouping. Although it sounds nice in theory, it can be very labor intensive in practice. Once you have written a script, one simple change can often require you to go back and modify the indentation level of almost every statement in the script.
I guess the good news is that this will certainly encourage you to write your program as a series of short, concise independent modules rather than as a single long rambling program.
Now let's talk about the details of indentation.
Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation of the line. This, in turn, is used to determine the grouping of statements.
What about tabs?
My advice is to avoid the use of tabs altogether. Use spaces instead, and use the same number of spaces for each statement in the group.
However, if you must use tabs, there are a few things that you should know. Here is what the Python Reference Manual has to say on the subject.
|"First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line's indentation. |
Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.
Cross-platform compatibility note: because of the nature of text editors on non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the indentation in a single source file."
The bottom line on indentation
Use either spaces, or tabs, but not both to cause the indentation level of all statements in a group of statements to be indented to the same level.
Review1. Logical lines are constructed from one or more _________ (fill in the blank).
Ans: Logical lines are constructed from one or more physical lines.
2. What constitutes a physical line?
Ans: A physical line is what you get when you type some characters into your text editor and press the Enter key, the Return key, or whatever it is called on your keyboard.
3. Can statements cross logical line boundaries?
Ans: Yes, but only in those cases where the syntax allows for the newline character, such as in compound statements.
4. What character is used for explicit line joining?
Ans: The backslash character can be used to join two physical lines into one logical line.
5. Explain implicit line joining in your own words.
Ans: Expressions in parentheses, square brackets, or curly braces can be split over more than one physical line without using backslashes.
6. What are the indentation rules for lines that have been implicitly joined?
Ans: You can indent the continuation line however you want.
7. Indentation in Python is used for cosmetic purposes only. True or False?
Ans: False. Indentation is used in Python to determine the grouping of statements, which causes it to be critical to the success of a program.
Copyright 2000, Richard G. Baldwin
About the authorRichard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.