Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Chapter 13. Tuples

We'll look at tuples from a number of viewpoints: semantics, literal values, operations, comparison operators, statements, built-in functions and methods. Additionally, we have a digression on the immutability of strings. Additionally, we have a digression on the Σ operator.

Tuple Semantics

A tuple is a container for a fixed sequence of data objects. The name comes from the Latin suffix for multiples: dou ble , tri ple , quadr uple , quin tuple . Mathematicians commonly consider ordered pairs; for instance, most analytical geometry is done with Cartesian coordinates ( x , y ), an ordered pair, double, or 2-tuple.

An essential ingredient here is that a tuple has a fixed and known number of elements. For example a 2-dimensional geometric point might have a tuple with x and y . A 3-dimensional point might be a tuple with x , y , and z . The size of the tuple can't change without fundamentally redefining the problem we're solving.

A tuple is an immutable sequence of Python objects. Since it is a sequence, all of the common operations to sequences apply. Since it is immutable, it cannot be changed. Two common questions that arise are how to expand a tuple and how to remove objects from a tuple.

When someone asks about changing an element inside a tuple, either adding, removing or updating, we have to remind them that the list, covered in Chapter 14, Lists , is for dynamic sequences of elements. A tuple is generally applied when the number of elements is fixed by the nature of the problem. For example, 2-dimensional geometry, or a 4-part internet address, or a Red-Green-Blue color code. We don't change tuples, we create new ones.

This tuple processing even pervades the way functions are defined. We can have positional parameters collected into a tuple, something we'll cover in the section called “Advanced Parameter Handling For Functions”.

  Published under the terms of the Open Publication License Design by Interspire