Chapter 6

Basic Data Types II:  Sequence and Dictionary

For Zen students a weed is a treasure

—Shunryu Suzuki, Zen Mind, Beginner’s Mind
Zen Mind, Beginners Mind


Single- and double-quoted strings
Figure 1.
String behaviour
Figure 2.
Escape codes
Figure 3.
Escape codes shown in IDLE
Figure 4.
An ASCII dump
Figure 5.
More string behaviour
Figure 6.
Figure 7.
Slicing and splicing
Figure 8.
Checking strings
Figure 9.
Tuples are immutable
Figure 10.
Figure 11.
Selecting the year from a date
Figure 12.
More time
Figure 13.
Turning time into a list
Figure 14.
List operations
Figure 15.
Figure 16.
Figure 17.
Figure 18.

Example Programs

There are four example programs for chapter 6:  download chapter6.zip.

  1. ascii.py
  2. lbm.py
  3. sort.py
  4. xtst.py


The best general book on data types and structures is Data Structures and Algorithms, by Aho, Hopcroft and Ullman, published by Addison Wesley, ISBN: 0-201-00023-7.  While it provides pseudocode algorithms for creating and accessing various types and structures, you will find that Python already does most of these things for you; it’s always good to understand more of what’s going on underneath the hood, though, and there remains a wealth of useful information about the data types we have covered in this hour and the material we will cover in the central third of this book.

Data Structures and Algorithms

If you want to get a head start on topics covered later in this book, then look at the code for lbm.py (Listing 6.3) and see if you can figure out what all the lines are doing, including the ones we haven’t talked about (go through your Python HTML documentation).  Can you rewrite lbm.py to include less code?

For a little relaxation with a time machine, consider reading Larry Niven’s, Rainbow Mars.  You’ll appreciate Guido’s Time Machine a little more, I think.

Rainbow Mars

Valid HTML 4.01 Transitional