Chapter 9. Tuples, Files, and Everything Else

This chapter rounds out our in-depth tour of the core object types in Python by exploring the tuple, a collection of other objects that cannot be changed, and the file, an interface to external files on your computer. As you’ll see, the tuple is a relatively simple object that largely performs operations you’ve already learned about for strings and lists. The file object is a commonly used and full-featured tool for processing files on your computer. Because files are so pervasive in programming, the basic overview of files here is supplemented by larger examples in later chapters.

This chapter also concludes this part of the book by looking at properties common to all the core object types we’ve met—the notions of equality, comparisons, object copies, and so on. We’ll also briefly explore other object types in Python’s toolbox, including the None placeholder and the namedtuple hybrid; as you’ll see, although we’ve covered all the primary built-in types, the object story in Python is broader than I’ve implied thus far. Finally, we’ll close this part of the book by taking a look at a set of common object type pitfalls and exploring some exercises that will allow you to experiment with the ideas you’ve learned.

Note

This chapter’s scope—files: As in Chapter 7 on strings, our look at files here will be limited in scope to file fundamentals that most Python programmers—including newcomers to programming—need to know. In particular, Unicode text files were previewed in Chapter 4, but we’re going to postpone full coverage of them until Chapter 37, as optional or deferred reading in the Advanced Topics part of this book.

For this chapter’s purpose, we’ll assume any text files used will be encoded and decoded per your platform’s default, which may be UTF-8 on Windows, and ASCII or other elsewhere (and if you don’t know why this matters, you probably don’t need to up front). We’ll also assume that filenames encode properly on the underlying platform, though we’ll stick with ASCII names for portability here.

If Unicode text and files is a critical subject for you, I suggest reading the Chapter 4 preview for a quick first look, and continuing on to Chapter 37 after you master the file basics covered here. For all others, the file coverage here will apply both to typical text and binary files of the sort we’ll meet here, as well as to more advanced file-processing modes you may choose to explore later.