* Gearhead columnist Mark Gibbs digs deeper into the language of Python
Last week we looked at the language Python (https://www.nwfusion.com/columnists/2003/0526gearhead.html), and this week we’ll dig deeper. But first, a letter from reader Greg Martin, who wrote: “You might mention that Python program structure is all based on white space. This is kind of cool, but requires you to pay attention! There are no
if condition
do this
now move on
The ‘do this’ will get executed if condition is true and ‘now move on’ always will get executed. Also, the iterative loop construct is wild. Python iterates over lists, which can be a lot of things. But if you want to iterate i from 1 to 10 you need to build a list using the range function, ‘For i in range(10):’ “
Thanks, Greg. This white-space thing – otherwise called ‘indentation’ – really does require your attention! Each statement in Python is terminated by a new line or a semicolon (“;”) and if you need to span multiple lines you can use the line-continuation character, “”. Thus:
if x:
statement1
statement2
else:
statement3
statement4
Now the “if” clause above is fine because the indentation is consistent, but Python will gag on the “else” clause because of the extra indent before statement4.
On the other hand, line-continuations aren’t required for triple-quoted strings (strings enclosed by a set of three quotes at both ends specifically for spanning multiple lines), lists, tuples (lists that can’t be modified or extended with new elements after creation) and dictionaries (associative arrays that pair keys with values). Some examples:
fruit = [ “Orange”, # a list – note that indexes start at 0
“Lemon”, #and this one is spread over
“Apple” # multiple lines
]
layout = (“Bin1”, “Bin2”, “Bin3”) # a tuple on a single line
fruitsection = {
“Orange : “Bin1”, # a dictionary with three keys
“Lemon” : “Bin2”, # on multiple lines
“Apple” : “Bin3”
}
You could also define fruitsection as: fruitsection = { fruit[0] : layout[0], fruit[1] : layout[1], fruit[2] : layout[2] }
Note that in the last example we’ve compressed the layout of the dictionary definition block to a single line. Want to change the fruitsection dictionary?
fruitsection [“Pear”] = “Bin5” # add a new member
fruitsection [“Apple”] = “Bin7” # change a member
del shop [“Pear”] # delete a member
Python has a rich set of methods (functions) for manipulating variables, lists, tuples and dictionaries. And you can create compound lists:
shop = [“hat”, fruitsection, [12.3, “gearhead”, 6]]
The reference shop [1][“Lemon”] would result in “Bin2”, while shop[1][2] would fail because fruitsection is a dictionary. On the other hand, shop[2][2] would return 6.
Also note that in Python you don’t have to declare variables before using them and can change the type of a variable by assigning a new value to it of a different type:
mynumber = 10 # mynumber is an integer
multiplier = 1.5 # multiplier is a floating-point
mynumber = mynumber * multiplier #mynumber is now a floating-point
Python also supports long integers and complex numbers.
Our final piece of Python for this week concerns loops. There are several types of loops you can use:
while test:
dosomething
Or:
for n in range(1,10):
dosomething
Note that Greg used the shortened form for range, which actually loops from 0 to the value given (also see the xrange() function in the Python documentation). Finally, you can use:
for a in “Hello world”: # you can use strings, lists, tuples and dictionaries
print a
Your thoughts to gearhead@gibbs.com




