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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.