Amazon.co.uk Review
First off, Learning Python shows the relationships among Python scripts and their interpreter (in a mostly platform-neutral way). Then, the authors address the mechanics of the language itself, providing illustrations of how Python conceives of numbers, strings and other objects as well as the operators you use to work with them. Dictionaries, lists, tuples and other data structures specific to Python receive plenty of attention including complete examples.
Authors Mark Lutz and David Ascher build on that fundamental information in their discussions of functions and modules, which evolve into coverage of namespaces, classes and the object-oriented aspects of Python programming. There's also information on creating graphical user interfaces (GUIs) for Python applications with Tkinter.
In addition to its careful expository prose, Learning Python includes exercises that both test your Python skills and help reveal more elusive truths about the language.
Review
Product Description
Learning Python is an introduction to the increasingly popular Python programming language. Python is an interpreted, interactive, object-oriented scripting language. Python is growing in popularity because:
- It is available on all important platforms: Windows NT, Windows 95, Windows 98, Linux, all major UNIX platforms, MacOS, and even the BeOS.
- It is open-source software, copyrighted but freely available for use, even in commercial applications.
- Its clean object-oriented interface makes it a valuable prototyping tool for C++ programmers.
- It works well with all popular windowing toolkits, including MFC, Tk, Mac, X11, and Motif. Learning Python is written by Mark Lutz, author of Programming Python and Python Pocket Reference; and David Ascher, a vision scientist and Python user. This book starts with a thorough introduction to the elements of Python: types, operators, statements, classes, functions, modules, and exceptions. By reading the first part of the book, the reader will be able to understand and construct programs in the Python language. In the second part of the book, the authors present more advanced information, demonstrating how Python performs common tasks and presenting real applications and the libraries available for those applications. All the examples use the Python interpreter, so the reader can type them in and get instant feedback. Each chapter ends with a series of exercises. Solutions to the exercises are in an appendix.
From the Publisher
About the Author
Mark Lutz is an independent Python trainer, writer, and software developer, and is one of the primary figures in the Python community. He is the author of the O'Reilly books Programming Python and Python Pocket Reference (both in 2nd Editions), and co-author of Learning Python (both in 2nd Editions). Mark has been involved with Python since 1992, began teaching Python classes in 1997, and has instructed over 90 Python training sessions as of early 2003. In addition, he holds BS and MS degrees in computer science from the University of Wisconsin, and over the last two decades has worked on compilers, programming tools, scripting applications, and assorted client/server systems. Whenever Mark gets a break from spreading the Python word, he leads an ordinary, average life with his kids in Colorado. Mark can be reached by email at , or on the web at http://www.rmi.net/~lutz.
David Ascher is the lead for Python projects at ActiveState, including Komodo, ActiveState's integrated development environment written mostly in Python. David has taught courses about Python to corporations, in universities, and at conferences. He also organized the Python track at the 1999 and 2000 O'Reilly Open Source Conventions, and was the program chair for the 10th International Python Conference. In addition, he co-wrote Learning Python (both editions) and serves as a director of the Python Software Foundation. David holds a B.S. in physics and a Ph.D. in cognitive science, both from Brown University.
Excerpted from Learning Python by Mark Lutz, David Ascher. Copyright © 1999. Reprinted by permission. All rights reserved.
In this chapter:
Data Structure Manipulations
Manipulating Files
Manipulating Programs
Internet-Related Activities
Bigger Examples
Exercises
At this point, we have covered the syntax of Python, its basic data types, and many of our favorite functions in the Python library. This chapter assumes that all the basic components of the language are at least understood and presents some ways in which Python is, in addition to being elegant and "cool," just plain useful. We present a variety of tasks common to Python programmers. These tasks are grouped by categories--data structure manipulations, file manipulations, etc.
Data Structure Manipulations
One of Python's greatest features is that it provides the list, tuple, and dictionary built-in types. They are so flexible and easy to use that once you've grown used to them, you'll find yourself reaching for them automatically.
Making Copies Inline
Due to Python's reference management scheme, the statement a = b doesn't make a copy of the object referenced by b ; instead, it makes a new reference to that object. Sometimes a new copy of an object, not just a shared reference, is needed. How to do this depends on the type of the object in question. The simplest way to make copies of lists and tuples is somewhat odd. If myList is a list, then to make a copy of it, you can do:
newList = myList[:]
which you can read as "slice from beginning to end," since you'll remember from Chapter 2, Types and Operators, that the default index for the start of a slice is the beginning of the sequence (0), and the default index for the end of a slice is the end of sequence. Since tuples support the same slicing operation as lists, this same technique can also copy tuples. Dictionaries, on the other hand, don't support slicing. To make a copy of a dictionary myDict, you can use:
newDict = {}
for key in myDict.keys():
newDict[key] = myDict[key]
This is such a common task that a new method was added to the dictionary object in Python 1.5, the copy() method, which performs this task. So the preceding code can be replaced with the single statement:
newDict = myDict.copy()
Another common dictionary operation is also now a standard dictionary feature. If you have a dictionary oneDict, and want to update it with the contents of a different dictionary otherDict, simply type oneDict.update(otherDict). This is the equivalent of:
for key in otherDict.keys():
oneDict[key] = otherDict[key]
If oneDict shared some keys with otherDict before the update() operation, the old values associated with the keys in oneDict are obliterated by the update. This may be what you want to do (it usually is, which is why this behavior was chosen and why it was called "update"). If it isn't, the right thing to do might be to complain (raise an exception), as in:
def mergeWithoutOverlap(oneDict, otherDict):
newDict = oneDict.copy()
for key in otherDict.keys():
if key in oneDict.keys():
raise ValueError, "the two dictionaries are sharing keys!"
newDict[key] = otherDict[key]
return newDict
or, alternatively, combine the values of the two dictionaries, with a tuple, for example:
def mergeWithOverlap(oneDict, otherDict):
newDict = oneDict.copy()
for key in otherDict.keys():
if key in oneDict.keys():
newDict[key] = oneDict[key], otherDict[key]
else:
newDict[key] = otherDict[key]
return newDict
To illustrate the differences between the preceding three algorithms, consider the following two dictionaries:
phoneBook1 = {'michael': '555-1212', 'mark': '554-1121', 'emily': '556-0091'}
phoneBook2 = {'latoya': '555-1255', 'emily': '667-1234'}
If phoneBook1 is possibly out of date, and phoneBook2 is more up to date but less complete, the right usage is probably phoneBook1.update(phoneBook2). If the two phoneBooks are supposed to have nonoverlapping sets of keys, using newBook = mergeWithoutOverlap(phoneBook1, phoneBook2) lets you know if that assumption is wrong. Finally, if one is a set of home phone numbers and the other a set of office phone numbers, chances are newBook = mergeWithOverlap(phoneBook1, phoneBook2)is what you want, as long as the subsequent code that uses newBook can deal with the fact that newBook['emily'] is the tuple ('556-0091', '667-1234').