Jonathan's Corner
(Sitemap)
> Orthodox Books Online, and More >
Technology >
Usability, the Soul of Python: An Introduction to Programming Python Through the
Eyes of Usability
Previous
1
2
3
4
5
6
7
8
9
10
Next
Printer-Friendly Version
There are some cases where the backslash is optional and discouraged: in particular, if you have open parentheses or square/curly braces, Python expects you to complete the statement with more lines:
stooges = [
u'Larry',
u'Moe',
u'Curly',
]
opposites = {
True: False,
False: True,
}
falsy = (
False,
0,
0.0,
'',
u'',
{},
[],
(),
None,
)
The three statements above represent three basic types: the list, the dictionary, also called the hash, dict, or occasionally associative array, and also the tuple. The list, denoted by square braces ("[]") and tuple, which is often surrounded by parentheses ("()") even though it is not strictly denoted by them unless a tuple is empty, both contain an ordered list of anything. The difference between them is that a tuple is immutable, meaning that the list of elements cannot be changed, and a list is mutable, meaning that it can be changed, and more specifically elements can be rearranged, added, and deleted, none of which can be done to a tuple. Lists and tuples are both indexed, with counting beginning at zero, so that the declaration of stooges above could have been replaced by creating an empty list and assigning members:
stooges = [] stooges[0] = u'Larry' stooges[1] = u'Moe' stooges[2] = u'Curly'
I will comment briefly that zero-based indices, while they are a common feature to most major languages, confuse newcomers: it takes a while for beginning programmers to gain the ingrained habit of "You don't start counting at 1; you start counting at 0."
The dictionary is a like a list, but instead of the index automatically being a whole number, the index can be anything that is immutable. Part of my first introduction to Perl was the statement, "You're not really thinking Perl until you're thinking associative arrays," meaning what in Perl does the same job as Python's dictionary, and lists and dictionaries in particular are powerful structures that can do a lot of useful work.
The example of a tuple provided above are some of the few values that evaluate to false. In code like:
if condition:
run_function()
else:
run_other_function()
while condition:
run_function()
The if and while statements test if condition is true, and the variable condition can be anything a variable can hold. Not only boolean variables but numbers, strings, lists, dictionaries, tuples, and objects can be used as a condition. The rule is basically similar to Perl. A very small number of objects, meaning the boolean False, numeric variables that are zero, containers like lists and dictionaries that are empty, and a few objects that have a method like __nonzero__(), __bool__(), or __len__() defined a certain way, are treated as being falsy, meaning that an if statement will skip the if clause and execute the else clause if one is provided; and a while statement will stop running (or not run in the first place). Essentially everything else is treated as being truthy, meaning that an if statement will run the if clause and skip any else clause, and a while loop will run for one complete iteration and then check its condition again to see if it should continue or stop. (Note that there is a behavior that is shared with other programming languages but surprising to people learning to program: if the condition becomes false after some of the statements in the loop has run, the loop does not stop immediately; it continues until all of the statements in that iteration have run, and then the condition is checked to see if the loop should run for another iteration.) Additionally, if, else, while, and the like end with a colon and do not require parentheses. In C/C++/Java, one might write:
if (remaining > 0)
In Python, the equivalent code is:
if remaining > 0:
If-then-else chains in Python use the elif statement:
if first_condition:
first_function()
elif second_condition:
second_function()
elif third_condition:
third_function()
else:
default_function()
Any of the example indented statements could be replaced by several statements, indented to the same level; this is also the case with other constructs like while. In addition to if/else/elif and while, Python has a for loop. In C, the following idiom is used to do something to each element in array, with C++ and Java following a similar pattern:
sum = 0;
for(i = 0; i < LENGTH; ++i)
{
sum += numbers[i];
}
In Python one still uses for, but manually counting through indices is not such an important idiom:
sum = 0
for number in numbers:
sum += number
The for statement can also be used when the data in question isn't something you handle by an integer index. For example:
phone_numbers = {
u'Alice Jones': u'(800) 555-1212',
u'Bob Smith': u'(888) 555-1212',
}
In this case the dictionary is a telephone directory, mapping names to telephone numbers. The key "Alice Jones" can be used to look up the value "(800) 555-1212", her formatted telephone number: if in the code you write, print phone_numbers[u'Alice Jones'], Python will do a lookup and print her number, "(800) 555-1212". If you use for to go through a dictionary, Python will loop through the keys, which you can use to find the values and know which key goes with which value. To print out the phone list, you could write:
for name in phone_numbers:
print name + u': ' + phone_numbers[name]
This will print out an easy-to-read directory:
Alice Jones: (800) 555-1212 Bob Smith: (888) 555-1212
Now let us look at strings. In the examples above, we have looked at Unicode strings, and this is for a reason. If you are in the U.S., you may have seen signs saying, "Se habla español," Spanish for "We speak Spanish here," or "Hablamos español," Spanish for "We don't speak Spanish very well." The difference is something like the difference in Python between:
sum = 0
for number in numbers:
sum += number
and:
sum = 0
index = 0
while index < len(numbers):
sum += numbers[index]
index += 1
Now if one is sticking large block letters on a sign in front of a store, it is acceptable to state, "SE HABLA ESPANOL"; it's appropriate to use an "N" because you don't have any "ñ"s, a bit like how it doesn't bother people to use a "1" because you've run out of "I"s or an upside-down "W" because you've run out of "M"s. And to pick another language, Greeks often seem willing to write in Greek using the same alphabet as English; this is the equivalent of writing "Hi, how are you?" in English but written with Greek letters: "αι ου αρ ιυ:" it works pretty well once you get used to it, but it's really nice to have your own alphabet.
There is one concern people may have: "So how many translations do I have to provide?" I would suggest this way of looking at it. The people in charge of major software projects often try to produce fully internationalized and localized versions of their software that appears native for dozens of languages, but even they can't cover every single language: if you support several dozen languages, that may be full support for 1% of the languages that exist. Even the really big players can't afford an "all-or-nothing" victory. But the good news is that we don't need to take an "all-or-nothing" approach. Russians, for instance, are often content to use forum software that has an interface and a few other things in English, and most of the discussion material in Russian. Perhaps the best thing to offer is a fully translated and localized Russian version of the forum, but many Russians will really do quite well if there is a good interface in English, and if the forum displays Russian discussions without garbling the text or giving errors.
Jonathan's Corner
(Sitemap)
> Orthodox Books Online, and More >
Technology >
Usability, the Soul of Python: An Introduction to Programming Python Through the
Eyes of Usability
Previous
1
2
3
4
5
6
7
8
9
10
Next
Printer-Friendly Version