TweetFollow Us on Twitter

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.

 
AAPL
$112.77
Apple Inc.
+0.99
MSFT
$47.80
Microsoft Corpora
+0.14
GOOG
$522.26
Google Inc.
+5.91

MacTech Search:
Community Search:

Software Updates via MacUpdate

NeoOffice 2014.6 - Mac-tailored, OpenOff...
NeoOffice is a complete office suite for OS X. With NeoOffice, users can view, edit, and save OpenOffice documents, PDF files, and most Microsoft Word, Excel, and PowerPoint documents. NeoOffice 3.x... Read more
LibreOffice 4.3.5.2 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
CleanApp 5.0.0 Beta 5 - Application dein...
CleanApp is an application deinstaller and archiver.... Your hard drive gets fuller day by day, but do you know why? CleanApp 5 provides you with insights how to reclaim disk space. There are... Read more
Monolingual 1.6.2 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. It requires a 64-bit capable Intel-based Mac and at least... Read more
NetShade 6.1 - Browse privately using an...
NetShade is an Internet security tool that conceals your IP address on the web. NetShade routes your Web connection through either a public anonymous proxy server, or one of NetShade's own dedicated... Read more
calibre 2.13 - Complete e-library manage...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
Mellel 3.3.7 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
ScreenFlow 5.0.1 - Create screen recordi...
Save 10% with the exclusive MacUpdate coupon code: AFMacUpdate10 Buy now! ScreenFlow is powerful, easy-to-use screencasting software for the Mac. With ScreenFlow you can record the contents of your... Read more
Simon 4.0 - Monitor changes and crashes...
Simon monitors websites and alerts you of crashes and changes. Select pages to monitor, choose your alert options, and customize your settings. Simon does the rest. Keep a watchful eye on your... Read more
BBEdit 11.0.2 - Powerful text and HTML e...
BBEdit is the leading professional HTML and text editor for the Mac. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides a... Read more

Latest Forum Discussions

See All

Living Room 3D for IKEA Makes All In-App...
Living Room 3D for IKEA Makes All In-App Purchases Free Until 2015 Posted by Jessica Fisher on December 22nd, 2014 [ permalink ] | Read more »
Crossbow Warrior – The Legend of William...
Crossbow Warrior – The Legend of William Tell Review By Lee Hamlet on December 22nd, 2014 Our Rating: :: MISSES THE MARKUniversal App - Designed for iPhone and iPad Crossbow Warrior details the entertaining adventures of legendary... | Read more »
Workflow: Powerful Automation Made Simpl...
Workflow: Powerful Automation Made Simple Review By Campbell Bird on December 22nd, 2014 Our Rating: :: GO WITH THE FLOWUniversal App - Designed for iPhone and iPad This powerful app lets users accomplish multiple tasks at touch of... | Read more »
My Little Monster Review
My Little Monster Review By Jordan Minor on December 22nd, 2014 Our Rating: :: IT'S ALIVE!Universal App - Designed for iPhone and iPad What’s it worth to make your own monster?   | Read more »
Galaxy Trucker Pocket (Games)
Galaxy Trucker Pocket 1.0.8 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0.8 (iTunes) Description: Galaxy Truckers Wanted!================================================================= (5/5) "Galaxy Trucker isn’t... | Read more »
Make your own Tribez Figures (and More)...
Make your own Tribez Figures (and More) with Toyze Posted by Jessica Fisher on December 19th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
So Many Holiday iOS Sales Oh My Goodness...
The holiday season is in full-swing, which means a whole lot of iOS apps and games are going on sale. A bunch already have, in fact. Naturally this means we’re putting together a hand-picked list of the best discounts and sales we can find in order... | Read more »
It’s Bird vs. Bird in the New PvP Mode f...
It’s Bird vs. Bird in the New PvP Mode for Angry Birds Epic Posted by Jessica Fisher on December 19th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Telltale Games and Mojang Announce Minec...
Telltale Games and Mojang Announce Minecraft: Story Mode – A Telltale Games Series Posted by Jessica Fisher on December 19th, 2014 [ permalink ] | Read more »
WarChest and Splash Damage Annouce Their...
WarChest and Splash Damage Annouce Their New Game: Tempo Posted by Jessica Fisher on December 19th, 2014 [ permalink ] WarChest Ltd and Splash Damage Ltd are teaming up again to work | Read more »

Price Scanner via MacPrices.net

13-inch 2.4GHz Retina MacBook Pro (Apple refu...
The Apple Store has previous-generation Apple Certified Refurbished 13″ 2.4GHz/128GB Retina MacBook Pros available for $999. Apple’s one-year warranty is standard, and shipping is free: - 13″ 2.4GHz/... Read more
Apple resellers offer free overnight shipping
The Apple Store is now offering free next-day shipping on all in stock items if ordered before 12/23/14 at 10:00am PT. Local store pickup is also available within an hour of ordering for any in stock... Read more
Holiday sales continue: MacBook Airs for up t...
 B&H Photo has 2014 MacBook Airs on sale for up to $120 off MSRP, for a limited time, for the Thanksgiving/Christmas Holiday shopping season. Shipping is free, and B&H charges NY sales tax... Read more
Holiday sale continues: 13-inch Retina MacBoo...
 B&H Photo has new 13″ MacBook Pros on sale for up to $150 off MSRP as part of their Holiday pricing. Shipping is free, and B&H charges NY sales tax only: - 13″ 2.5GHz MacBook Pro: $979 save... Read more
Holiday sale continues: 15-inch Retina MacBoo...
 B&H Photo has the new 2014 15″ Retina MacBook Pros on sale for up to $300 off MSRP for a limited time. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro: $... Read more
Holiday sale: 13-inch 128GB MacBook Air for $...
 Best Buy has the 2014 13-inch 1.4GHz 128GB MacBook Air on sale for $849.99, or $150 off MSRP, on their online store. Choose free home shipping or free local store pickup (if available). Price valid... Read more
13-inch 2.6GHz Retina MacBook Pro on sale for...
Best Buy has lowered their price on the 2014 13″ 2.6GHz/128GB Retina MacBook Pro to $1149.99 on their online store for a limited time. That’s $150 off MSRP and the lowest price available for this... Read more
Kodak Returns to CES With New Consumer Produ...
Former photography colossus Kodak is returning to CES for the first time in three years where the Kodak booth (#21818 South Hall 1) will showcase a wide range of innovative, imaging-related products... Read more
Invaluable Launches New Eponymously -Named A...
Invaluable, the world’s largest online live auction marketplace, hhas announced the official launch of the Invaluable app for iPad, now available for download in the iTunes App Store. Invaluable... Read more
IDC Reveals Worldwide Mobile Enterprise Appli...
International Data Corporation (IDC) last week hosted the IDC FutureScape: Worldwide Mobile Enterprise Applications and Solutions 2015 Predictions Web conference. The session provided organizations... Read more

Jobs Board

*Apple* Store Leader Program (US) - Apple, I...
…Summary Learn and grow as you explore the art of leadership at the Apple Store. You'll master our retail business inside and out through training, hands-on experience, Read more
Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.