The first four articles in this "Discover Python" series introduced the basic data types commonly used in Python programs, including:
- Built-in numeric data types
The previous articles in this series also showed some simple Python examples that manipulated variables that held data of these four types. Although I didn't state it then, I naturally assumed you would read and interpret the code just as if you were reading a book. The natural progression (at least, in the English language) is to start at the top of a page or program and read a line from left to right. After reaching the end of a line, you start at the beginning (or left-hand side) of the next line, and so on down the page -- or, in this case, the program.
The Python interpreter, at its simplest level, operates in a similar manner, starting at the top of a program and sequentially executing program statements in a linear fashion. For example, Listing 1 shows several simple statements. When you type them into the Python interpreter (or if you save them in a file and execute them as a Python program), the statements are read starting on the left side and progressing to the right. When an end-of-line character (such as a newline character) is read, the Python interpreter advances to the next line and continues until there are no more lines of code.
Listing 1. A simple Python program
>>> i = 1 >>> type(i) <type 'int'> >>> l = [0, 1, 2, 3, 4] >>> l * i [0, 1, 2, 3, 4]
In this example, the statements follow each other in a simple progression. But life isn't always so linear. Think about a personal example. You may have woken up this morning and listened to the traffic or weather report (or both). Based on the traffic report, you might have chosen a different route to work; or, similarly, depending on the weather report, you might have planned a different activity for your weekend. Your actions aren't simple; the natural progression of life takes twists and turns, depending on information you acquire along the way.
Python, like most programming languages, can also operate in this way, by using flow control statements. In Python, there are three primary flow control statements:
ifstatement, which executes a particular block of statements based on the result of a test expression
whileloop, which executes a block of statements as long as a test expression is true
forloop, which executes a block of statements a certain number of times
This list is fairly simple, and you may recognize these flow control statements from other programming languages. But you might wonder what is meant by block of statements. In Listing 1, you saw several simple statements, including a variable initialization, a method invocation (the
type method), and a multiplication operation. These statements perform a single operation -- hence, they're called simple.
Python also has what are known as compound statements: groups of related statements, including simple and (possibly) additional complex statements. For example, a compound statement might perform different operations or repeat an operation multiple times, depending on the value of an expression (in personal terms, this might be the answer to a question like, "Will the weather be sunny and warm today?"). This description may seem similar to the flow control description in the previous paragraph. It should because flow control statements are compound statements.
A compound statement consists of a flow control instruction, followed by a colon character (
:), followed by a block of program statements. The block of statements is made up of one or more simple and compound statements. A simple pseudocode example is provided in Listing 2.
Listing 2. A pseudocode example showing simple and complex statements
simple statement one compound statement one: simple statement two simple statement three compound statement two: simple statement four simple statement five
This syntax may look both familiar and odd, and both feelings arise from the same thing: the indentation. When you make an outline or a to-do list, you probably use different levels of indentation to separate items, making your work more readable in the process. Python follows this model, using indentation to separate blocks of code from the rest of the program. Other programming languages use special characters to distinguish blocks of code, like the curly brace characters (
} ) in C-based languages. These other languages also encourage programmers to use indentation, in an effort to improve the readability of programs.
Python, on the other hand, requires indentation to indicate a code block. If you don't properly indent, the Python interpreter throws an exception. You can use tab characters to mark the indentation, but it's generally recommended that you use spaces. (For consistency, I always use four spaces to indent a block.) The reason is simple: A space character is always interpreted the same way. A tab character, on the other hand, can be interpreted different ways -- as two, four, six, or even eight spaces, depending on the platform or tool being used.
Enhancing program readability
The indentation requirement is probably the best example of one of Python's fundamental guiding principles: A Python program should be easy to read and understand. As is this case with any tool, however, a determined user can write Python code that is obtuse. For example, a screwdriver is made to be used with screws, but at some point you may have used one to open a can of paint.
Two other features help you write easy-to-read programs in Python, and they both follow from the book analogy used earlier. First, lines in a book don't continue past the end of the page; they have a fixed length. Second, the lines in a book aren't terminated by a special symbol, like a semicolon. Both of these features are built into the way Python programs are written.
If a program line is too long, you can continue the line onto the next physical line in the file. No hard rule dictates how long a line of code should be. But common sense implies a limit of 80 characters, which fits easily on a printed page on most monitors. There are several ways to spread a code statement over more than one line:
- Triple-quoted strings can extend over multiple lines.
- Expressions enclosed in parentheses can extend over multiple lines.
- You can use the continuation character (
\) to split statements over multiple lines.
In Python, you don't need to use a special character (or symbol) to indicate the end of a statement. This is different from some languages. For example, C-based languages use a semicolon (
;) to indicate the end of a line of code. Sometimes, however, it's desirable to place multiple program statements on a single line -- when you're initializing variables, for example. In these cases, you can use semicolons to separate individual statements.
Both of these techniques are demonstrated in Listing 3.
Listing 3. Demonstrating Python's readability techniques
>>> i1 = 10 ; i2 = 20 ; i3 = 30 >>> >>> b = ((i1 < 20) and ... (i2 < 30) and ... (i3 < 40)) >>> b True >>> >>> b = (i1 < 20) and \ ... (i2 < 30) and \ ... (i3 < 40) >>> >>> b True
Notice how the program statements in Listing 3 that extend over more than one line are indented to improve readability. In this instance, the indentation isn't mandatory, as it is with a compound statement. But as you can see, indenting improves the appearance of the program and is highly recommended.
The simplest flow control statement is the
if statement, whose basic syntax is demonstrated in the pseudocode shown in Listing 4. The
if statement executes a block of program statements if a Boolean expression evaluates True. The
if statement supports an optional
else clause that indicates a block of program statements that should be processed if the Boolean expression evaluates False.
Listing 4. The basic syntax for the
if(expression one): # Action to take if expression one evaluates True else: # Action to take if all expression one evaluates False
This syntax may look both familiar and unusual if you've been exposed to other programming languages. The similarity derives from the general format of the
if statement, in the name, the evaluation of an expression to determine how to branch the flow of statement execution, and the presence of an
else clause to handle the case when the expression evaluates
False. But two items are fairly specific to Python: the termination of the
if and the
else statements with the colon character, and the indentation of the code within the
if and the
else blocks. As mentioned, these two characteristics are required in Python for flow control statements.
In Listing 5, a simple if/else condition tests whether a given number is even or odd and prints out the result.
Listing 5. A simple
if statement example
>>> i = 8 >>> if(i % 2): ... print "Odd Number" ... else: ... print "Even Number" ... Even Number
One item that may seem confusing is the presence of the three dots (
...) at the start of all the lines following the
if statement. When you type in the
if statement and the terminating colon, then press Return (or Enter) on your keyboard, the Python interpreter realizes you have entered a compound statement. As a result, it changes the prompt from three greater than symbols (
>>>) to the three dots (
...). Because Python requires indentation to offset the block of statements that should be executed if the expression evaluates
False, the two
The expression used in the
if statement (and in the
elif clause and the
while loop discussed later in this article) can be as complex as necessary. It can be composed of multiple subexpressions that use the different relational operators supported in Python. And the subexpressions can be combined using the
not logical operators. "Discover Python, Part 1" contains more information about Boolean expressions and the different relational and logical operators in Python.
Up to this point, you've seen how the
if statement can be used to execute one of two blocks of program statements, depending on the value of a particular Boolean expression. In some cases, however, you may want more choices. Fortunately, Python provides a simple extension to the
if statement. The solution provided is remarkably simple: you add an extra
if statement to the
else clause. The result is an
else if statement, which is shortened to
elif, as demonstrated in Listing 6.
Listing 6. Using the
>>> i = -8 >>> if(i > 0): ... print "Positive Integer" ... elif(i < 0): ... print "Negative Integer" ... else: ... print "Zero" ... Negative Integer
This example includes only one
elif statement, but in practice you can include as many as the program needs. Although it isn't the optimal solution, multiple
elif statements can be used to mimic the
switch case statement present in some other languages.
The second type of flow control statement in Python is the
while loop, which executes a block of program statements while an expression evaluates
while loop, as was the case with the
if statement, supports an optional
else clause containing a block of program statements executed when the expression is
False. For a
while loop, however, this means the code in the
else clause is executed once, after the loop is terminated (see the pseudocode shown in Listing 7).
Listing 7. Pseudocode for the
while (expression): # statements to execute while loop expression is True else: # statements to execute when loop expression is False
Once you understand the
if statement, the
while loop is fairly simple to understand, as well. Be aware, however, that the loop will execute until the expression evaluates
False. This means the program statements executed in the loop body must change the value of the expression -- or else the loop will never end. This is demonstrated in Listing 8.
Listing 8. A simple example of the
>>> i = 0 ; x = 10 >>> while(x > 0): ... i+=1 ; x -= 1 ... else: ... print i, x ... 10 0
This example demonstrates several things. First, it combines variable initializations and, later, variable modifications on one line: In this case, the
x variables. Second, the shorthand operators
-= are used to increase the value of
i and decrease the value of
x, respectively. In this case, the loop starts with
x having the value 10. Each time through the loop, the value of
x is decreased by 1. Finally,
x has the value 0, at which point the loop is exited and the code in the
else clause is executed, printing out the values of the two variables.
while loop (like the
for loop introduced later in this article) supports three additional statements:
break statements are used inside a
while loop to continue with the next pass through the loop or break out of the loop, respectively. Often, these two statements are placed in the body of an
if statement so that the continue or break action is triggered by a special condition. One special feature of the
break statement is that it breaks completely out of the loop, thereby skipping any
else clause that follows the loop.
pass statement is a do-nothing statement. It's used as a placeholder when a statement is required, but the program logic doesn't require an action. All three statements are demonstrated in Listing 9.
Listing 9. Using the
>>> i = 1 >>> while(i < 1000): ... i *= 5 ... if(i % 25): ... continue ... if not (i % 125): ... break ... if not (i % 1000): ... pass ... else: ... print i ... >>> print i 125
This rather contrived example loops until the variable
i is greater than or equal to 1,000. Inside the loop, you multiply
i by 5, then test to see whether
i is evenly divisible by 25. Remember that you execute the body of an
if statement only if the expression is
True, which for this expression happens when the variable
i is not evenly divisible by 25. (A nonzero number is evaluated as Boolean True in a Python expression.)
The next statement in the loop body is a second
if statement that tests whether the variable
i is evenly divisible by 125, but this expression is preceded by the
not operator. As a result, the body of the second
if statement is executed when the value of the variable
i is evenly divisible by 125. At this point, the
break statement causes program execution to break out of the
while loop, skipping the
if statement is never executed and is used to demonstrate how the
pass statement is coded in a program. In future articles, you'll see cases where the
pass statement is more relevant.
Tracing the logic flow through the program, you see that the first time through the loop, the value of the variable
i becomes 5. The first
if statement evaluates
True, because 5 isn't evenly divisible by 25. This starts the second trip through the
while loop, where the variable
i becomes 25. Now the first
if statement evaluates
False because 25 is evenly divisible by 25. The second and third
if statements also evaluate
False, meaning you start the third pass through the loop. This time the variable
i becomes 125, and the first
if statement evaluates
But now the second
if statement evaluates
True because the variable
i is evenly divisible by 125 (and the
not operator turns the resulting 0 into a Boolean True). This causes the
break statement to be executed, breaking out of the loop. The
else clause is never executed, so nothing is printed out until you explicitly use a separate
for loop in Python is special and is closely tied to the container data types built into the Python programming language. When you have a container object in real life, such as a bag, you often want to look at all the items it contains. The same holds true when you're writing Python programs. When you need to do something a certain number of times (as you would for each item in a container), you use the
for loop. The
for loop is demonstrated in pseudocode format in Listing 10.
Listing 10. Pseudocode for the
for item in container: # action to repeat for each item in the container else: # action to take once we have finished the loop.
Given the rich nature of the Python container types, the
for loop is quite powerful. Fundamentally, the
for loop involves what is known as an iterator, which is used to move through the collection item by item. The next article in this series will take a closer look at the
for loop and how to use it properly with the different container types.
Controlling the flow
This article has introduced three Python program statements: the
if statement, the
while loop, and the
for loop. These three statements let you change the flow of a program by selecting which statements are executed or by executing a set of statements multiple times. These statements will be put to greater -- and more interesting -- uses in future articles. The nature of compound statements introduced the nature of proper indentation in Python programs, which makes Python programs easy to read and understand.
- When you have a working Python interpreter, the Python Tutorial is a great place to start learning the language.
- "Discover Python, Part 1" contains more information about Boolean expressions and the different relational and logical operators in Python.
- Part 2 of this series discusses the object nature of the language, initially for the built-in simple types. The Python
tupleclass is also introduced and used to demonstrate the concept of a container type.
- Part 3 of this series introduces the
stringclass and demonstrates different ways in which you can use a string within Python.
Python provides a number of useful features, of which the
listclass is one of the most important. Part 4 of this series introduces the
listclass and demonstrates some of the many ways in which you can use it to simplify difficult programming tasks.
- IBM developerWorks has published many articles on Python, including the "Charming Python" series, by David Mertz.
- Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
Get products and technologies
- Download Python from Python.org.
- Innovate your next open source development project with IBM trial software, available for download or on DVD.
- Get involved in the developerWorks community by participating in developerWorks blogs.