Logo         Community
The Company
Your Account
Contact Us
Python 101 (part 4): Feeding The Snake
Luke Skywalker has the Force. Python programmers have dictionaries and tuples. What's the connection?

| Running The Lights |

I'll begin with tuples, which share most of their properties and methods with lists. In fact, they even look alike - take a look:

Python 1.5.2 (#1, Aug 25 2000, 09:33:37)  [GCC 2.96 20000731 (experimental)] on
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> CreepyCrawlies = ("spiders", "ants", "lizards")
>>> CreepyCrawlies
('spiders', 'ants', 'lizards')

As you can see, a tuple is another "container" variable, which can contain one or more values. In the example above, "CreepyCrawlies" is a tuple containing the values "spiders", "ants" and "lizards".

Unlike lists, which are enclosed within square braces, tuples are enclosed within parentheses, with values separated by commas. The various elements of the tuple are accessed via an index number, with the first element starting at zero. So, to access the element "spiders", you would use the notation

>>> CreepyCrawlies[0]


>>> CreepyCrawlies[2]

- essentially, the tuple name followed by the index number enclosed within square braces. You'll remember this "zero-based indexing" from lists.

Defining a tuple is simple - simply assign values (enclosed in square braces) to a variable, as illustrated below:

>>> pasta = ("macaroni", "spaghetti", "lasagne", "fettucine")

The rules for choosing a tuple name are the same as those for any other Python variable - it must begin with a letter, and can optionally be followed by more letters and numbers. Like a list, a tuple can mix different types of elements - the following example creates a tuple containing strings, numbers, lists and even another tuple.

>>> allMixedUp = ("ola", 67, "pink fox, yellow moon", 43534.57, [4, "four"], ("red", "blue", "green"))
>>> allMixedUp
('ola', 67, 'pink fox, yellow moon', 43534.57, [4, 'four'], ('red', 'blue', 'green'))
>>> allMixedUp[0]
>>> allMixedUp[2]
'pink fox, yellow moon'
>>> allMixedUp[4]
[4, 'four']
>>> allMixedUp[4][1]
>>> allMixedUp[5][2]

It should be noted that when Python sees two or more comma-separated elements, it automatically creates a tuple for them...which means that the following code snippets are equivalent.

>>> lights = "red", "green", "blue"
>>> type(lights)
<type 'tuple'>
>>> lights = ("red", "green", "blue")
>>> type(lights)
<type 'tuple'>

That said, it's a good idea to always explicitly enclose your tuples within parentheses; this avoids confusion when you review your code seven years later, and also makes it easier to read.

The type() function above is used to find out the type of a specific variable - whether string, list, tuple or little green hybrid.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback