Mac in the Shell: Learning Python on the Mac: Code Blocks
Volume Number: 25 (2009)
Issue Number: 01
Column Tag: Mac in the Shell
Mac in the Shell: Learning Python on the Mac: Code Blocks
Aka: the big indent
by Edward Marczak
Introduction
The previous two columns have begun to introduce us to the Python scripting language, and some OS X-specific ways to start scripting, and OS X-specific issues. The first article got into the very basics of Python and the Python interpreter. The second article got into the specifics of the string collection type. This month, we'll see how Python implements flow control and how to specify blocks of code.
Get Used To It
Love it or leave it, but in any case, get used to it: Python uses whitespace indentation levels to specify blocks of code. If you're new to scripting altogether, this may not seem odd. However, if you already have experience with another language, C, perl or PHP, perhaps-a curly brace language-this will be something you need to adjust to. Whitespace isn't just used to make code readable or pretty, it's a function of the language. You'll realize this if you tried any of the samples from last month and mistakenly indented anything. For those of you who are perfect typists, though, here's the effect (note the leading space on the second print line):
#!/usr/bin/python
print "Indentation is important"
print "in Python!
Running this code produces this error:
IndentationError: unexpected indent
Program exited.
So, what's this all about? It all comes down to flow control: choosing one path of code over another based on some comparison or value. The chosen path will run or skip a block of code. These code blocks are grouped together by a common level of indentation. Before we begin, you should understand that the whitespace used can be any whitespace - tabs or any number of spaces. However, choose one style! Choose one and stick to it. Spaces vs. tabs can become a bit of an argument. I used to be a tab adherent, but now use two spaces. Spaces are fixed in a fixed width font, display easily in a browser or on paper, and make it easy on tidy-like apps. Choose the style that suits you best; just be consistent. I'll be using two spaces in my articles. Any decent text editor (BBEdit, TextMate, Xcode, and so on) should let you set what the tab key actually inserts.
This particular column may seem to jump around a bit, but if you read through, you'll see how it builds and all ties together by the end. Let's get started with the most basic: the if statement.
if
The if statement checks a condition and if it is true, runs a block of code statements. The general form is:
if condition:
That's it. It's nicer to see in action, though. You can type this in and run it:
#!/usr/bin/python
x=5
if x > 10:
print "The value of x is greater than 10!"
print "Yes, it really is."
print "Done."
Since the value of x is not greater than 10, the two indented print lines (a code block) is skipped. All this program prints is "Done.". However, if we change x to, say, 20, and run the program again, the condition in the if statement becomes true (yes, x is greater than 10), and the two print statements in the if code block are executed.
No curly braces, no BEGIN or END statements, just the level of indentation. Code blocks can also be nested. If there is a condition that you'd like to check that only applies within a particular code block, its code will start at a new level of indentation. For example:
#!/usr/bin/python
x=55
if x > 10:
print "The value of x is greater than 10!"
print "Yes, it really is."
if x > 50:
print "In fact, it's greater than 50!"
print "It is %s." % x
print "Done."
All of the print statements in this program will be executed.
Truth
Comparison operators reduce their decisions to, and act on one thing: truth. Not some philosophical, grey area truth, but a Boolean True or False. Like many other languages, "false" is 0. True, then can only be one thing: not false. While most people then interpret true as "1", it's actually any non-zero value. The following is true:
if 1:
print "It's true!"
But be aware that this is true also:
if 18:
print "It's true!"
and this:
if -12:
print "It's true!"
A genuine Boolean type was added in Python version 2.3. This introduced the keywords True and False (note the capitalization). While True and False are actually just another representation of 1 and 0, respectively (Booleans are a subclass of the int class), the abstraction helps make code more readable. If you're looking at someone else's code and the value "1" is being passed into functions or returned from functions, it's tough to derive its meaning. However, "True" and "False" give a much better indication of their meaning at first look. Here's a simple code snippet that illustrates Boolean values in Python:
print_it=True
if print_it:
print "The variable is true!"
print "It's type is %s." % type(print_it)
This foundation will help you better understand what the comparison operators are doing.
Comparison Operators
When writing a comparison statement, you will need to use an operator that specifies the comparison to make. Here is a list of Python's comparison operators:
< Less than
> Greater than
== Equality
!= Not equal
>= Greater than or equal to
<= Less than or equal to
is Equal
not Not equal/negation
You've already seen the > operator in action. The < operator works similarly. The double equal sign (==) is used to test for equality. This is different than the assignment operator (=, or, single equal). It's a common mistake in C, C++ and PHP to use the assignment operator when you meant to use the equality comparison operator. Fortunately, the python interpreter will flag this as an error. Running this code snippet:
if x=5:
print "x is 5"
...at runtime will yield :
SyntaxError: invalid syntax
The remaining operators are pretty self-explanatory, with perhaps the exception of the "is" operator. is tests for object equivalence, and is different than the simple equality comparison operator. As outlined in the first article in this series, everything in Python is an object. Object identifiers may be pointing to the same object, and this is what is tests. Let's take a look at an example to solidify this concept:
#!/usr/bin/python
x=5
y=x
if y is x:
// y and x point to the same object
print "Yes, y is x: y is at %s and x is at %s." % (id(y), id(x))
x=7
y=7
if y is x:
// y and x both point to the same int object
print "Still, y is x: y is at %s and x is at %s." % (id(y), id(x))
x=5
y=10-5
if y is x:
// 5 is 5, no matter how it's derived
print "Once again, y is x: y is at %s and x is at %s." % (id(y), id(x))
y=2
x=3
if y is x:
print "y is now not x, so, this will not print."
Integers can be compared directly, too:
if x is not 0:
blah
Like the Boolean class, using this style greatly increases the readability of code. However, in a future article when we get into writing our own classes and creating objects, the is operator becomes much more important than syntactic sugar.
While
The while statement also implements a type of flow control. Unlike the if statement, which decides to run a block of code entirely or not, while forms a loop, which repeatedly tests a given condition before running a block of code, and keeps running its block of code while the condition is true. Look at this common snippet of code:
the_number = 0
while the_number < 10:
print "The count is %s." % the_number
the_number+=1
print "Done!"
Running this snippet will print the numbers 0 through 9, and then print "Done!" When the interpreter encounters the while loop, the variable the_number meets the condition: it is less than 10, so the while code block is executed. Immediately we print "The count is..." followed by a line that increments the_number by one (the plus-equals notation is the equivalent of taking the variable on the left hand side and adding a value to itself. In other words, variable = variable + 1). When the interpreter reaches the end of its code block, it loops back to the top and runs the test again. If it is still true, the code block is run again. If the condition is now false, the code block is skipped, and execution is resumed at the statement following the while code block.
The For Loop
Another type of flow control loop is the for loop. You may have seen a for loop in other languages, but don't let those be "false friends" - the for loop in Python is different than any of them.
Python's for loop iterates over a sequence. We learned about sequences in the first Learn Python on the Mac article in the November issue. Sequences cover many of Python's data types, including lists, dictionaries and even strings. We can generate a numeric list sequence using the range function. An example using the interactive interpreter (which you access simply by typing python in a shell):
>>> print range(1,5)
[1, 2, 3, 4]
Now that we see range returns a sequence, let's look at the for loop. The generic form of the loop is:
for [variable] in [sequence]:
code block
As a more direct example:
for i in range(1, 5):
print i
...which outputs:
1
2
3
4
Remember that the Python for loop works with all sequences. Even strings are just sequences of characters. We can see this back in the interactive interpreter:
>>> x = "hello"
>>> for i in x:
... print i
...
h
e
l
l
o
What Else?
Each flow control type presented above can have an optional else clause. The else clause code block is executed when the condition being tested is false. The easiest case to visualize is the if statement:
x = 5
if x > 10:
print "x is greater than 10"
else:
print "x is less than 10"
This reads in a very English-like manner: if x is greater than 10, run the first code block. If x is anything else, run the else code block. This also applies to the while loop. Modifying the while loop we used earlier:
the_number = 0
while the_number < 10:
print "The count is %s, residing at %s." % (the_number, id(the_number))
the_number+=1
else:
print "running the else block"
print "Done!"
This runs the while code block, but when x is incremented to 10 and the condition is tested again, the else block will run. Unlike the if statement, in the case of the while (and for) loop, the else block will always be invoked in simple cases. The next section will describe two flow control statements that can change this.
Continue and Break
For one reason or another, you may want to skip a particular iteration of a loop, or, end it early. The continue and break statements, respectively, do just this.
continue stops running the code block that it's in, and starts the loop again at the top. For example, this code snippet will output odd numbers:
for i in range(1, 11):
if i % 2 == 0:
continue
print i
Inside the loop, the first statement decides if i is even. If it is, we simply continue - skipping the remainder of the code block and starting the loop from the top.
The break statement breaks out of the code block it's in and terminates a loop.
the_number = 0
while True:
print "The count is %s" % the_number
the_number+=1
if the_number > 10:
break
This is a contrived example, but nicely illustrates the break statement. A while statement with a condition of True would normally never terminate. Inside of this loop, however, we test the value of the_number. If the_number is greater than 10, we issue break and terminate the loop.
As alluded to in the previous section, the break statement entirely breaks out of a loop. This means skipping the loop's else portion as well.
Whew
We covered a good amount of ground this month. Flow control is really the brains behind any code. Python has everything one would expect from a language. The if/else, while/else and for/else loops, crafted wisely, lead your code down the right path. From the beginning of this series through now, you can write some very basic (command-line) applications with logic. Next month, we'll get into Python functions and libraries.
Media of the month: Martin Luther King Jr.'s, "I have a Dream" speech. Read it here: http://www.usconstitution.net/dream.html, or possibly more powerful, listen to it here: http://video.google.com/videoplay?docid=1732754907698549493. It's an amazing speech, good to be familiar with.
Hopefully, you're reading this at Macworld and/or you have paid us a visit at the MacTech booth! Enjoy the show, and see you next month.
Ed Marczak is the Executive Editor of MacTech Magazine. He lives in New York with his wife, two daughters and various pets. He has been involved with technology since Atari sucked him in, and has followed Apple since the Apple I days. He spends his days on the Mac team at Google, and free time with his family and/or playing music. Ed is the author of the Apple Training Series book, "Advanced System Administration v10.5," and has written for MacTech since 2004.