Lesson 10. Tuple objects: sequences of any kind of object

 

After reading lesson 10, you’ll be able to

  • Create a sequence of any kind of object by using a tuple
  • Do a few operations on tuple objects
  • Swap variable values by using tuples

Suppose I give you the simple task of keeping track of your favorite superhero characters. Let’s say you have three: Spiderman, Batman, and Superman.

Using what you know so far, you could try to create a string containing every one of these names, separated by a space, like so: "Spiderman Batman Superman". Using the commands you learned in lessons 7 and 8, you’d be able, with a little effort and care, to keep track of indices in the string and extract each name as needed.

But what if you kept full names in the string, like so: "Peter Parker Bruce Wayne Clark Kent". It now becomes considerably harder to extract each person’s name because the first and last names are also separated by spaces. You could use other special characters, such as a comma, to separate full names, but this doesn’t solve the most annoying problem with using strings to store this data: it’s tedious to extract items of interest because you have to keep track of starting and ending indices.

10.1. Tuples as sequences of data

 
 
 

10.2. Understanding operations on tuples

 
 
 

Summary

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage