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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.