Learning Python - Mark Lutz [155]
In the next part, we’ll shift gears, turning to the topic of statement syntax in Python—we’ll explore all of Python’s basic procedural statements in the chapters that follow. The next chapter kicks off that part of the book with an introduction to Python’s general syntax model, which is applicable to all statement types. Before moving on, though, take the chapter quiz, and then work through the end-of-part lab exercises to review type concepts. Statements largely just create and process objects, so make sure you’ve mastered this domain by working through all the exercises before reading on.
Test Your Knowledge: Quiz
How can you determine how large a tuple is? Why is this tool located where it is?
Write an expression that changes the first item in a tuple. (4, 5, 6) should become (1, 5, 6) in the process.
What is the default for the processing mode argument in a file open call?
What module might you use to store Python objects in a file without converting them to strings yourself?
How might you go about copying all parts of a nested structure at once?
When does Python consider an object true?
What is your quest?
Test Your Knowledge: Answers
The built-in len function returns the length (number of contained items) for any container object in Python, including tuples. It is a built-in function instead of a type method because it applies to many different types of objects. In general, built-in functions and expressions may span many object types; methods are specific to a single object type, though some may be available on more than one type (index, for example, works on lists and tuples).
Because they are immutable, you can’t really change tuples in-place, but you can generate a new tuple with the desired value. Given T = (4, 5, 6), you can change the first item by making a new tuple from its parts by slicing and concatenating: T = (1,) + T[1:]. (Recall that single-item tuples require a trailing comma.) You could also convert the tuple to a list, change it in-place, and convert it back to a tuple, but this is more expensive and is rarely required in practice—simply use a list if you know that the object will require in-place changes.
The default for the processing mode argument in a file open call is 'r', for reading text input. For input text files, simply pass in the external file’s name.
The pickle module can be used to store Python objects in a file without explicitly converting them to strings. The struct module is related, but it assumes the data is to be in packed binary format in the file.
Import the copy module, and call copy.deepcopy(X) if you need to copy all parts of a nested structure X. This is also rarely seen in practice; references are usually the desired behavior, and shallow copies (e.g., aList[:], aDict.copy()) usually suffice for most copies.
An object is considered true if it is either a nonzero number or a nonempty collection object. The built-in words True and False are essentially predefined to have the same meanings as integer 1 and 0, respectively.
Acceptable answers include “To learn Python,” “To move on to the next part of the book,” or “To seek the Holy Grail.”
Test Your Knowledge: Part II Exercises
This session asks you to get your feet wet with built-in object fundamentals. As before, a few new ideas may pop up along the way, so be sure to flip to the answers in Appendix B when you’re done (or when you’re not, if necessary). If you have limited time, I suggest starting with exercises 10 and 11 (the most practical of the bunch), and then working from first to last as time allows. This is all fundamental material, though, so try to do as many of these as you can.
The basics. Experiment interactively with the common type operations