Logo         Community
The Company
Your Account
Contact Us
Python 101 (part 3): A Twist In The Tail
Add flow control to your Python programs with the "for" and "while" loops.

| Here Comes A Hero |

Thus far, the variables you've used contain only a single value - for example,

Python 1.5.2 (#1, Aug 25 2000, 09:33:37)  [GCC 2.96 20000731 (experimental)] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> i=0
>>> alpha=63453473458348383L
>>> name="god"

For simple Python programs, this is usually more than enough. However, as your Python programs grow in complexity, you're going to need more advanced data structures to store and manipulate information. And that's exactly where lists come in.

Unlike string and number objects, which typically hold a single value, a list can best be thought of as a "container" variable, which can contain one or more values. For example,

>>> superheroes = ["Spiderman", "Superman", "Human Torch", "Batman"]

Here, "superheroes" is a list containing the values "Spiderman", "Superman", "Human Torch", and "Batman".

Lists are particularly useful for grouping related values together - names, dates, phone numbers of ex-girlfriends et al. The various elements of the list are accessed via an index number, with the first element starting at zero. So, to access the element "Superman", you would use the notation

>>> superheroes[0]


>>> superheroes[3]

- essentially, the list name followed by the index number enclosed within square braces. Geeks refer to this as "zero-based indexing".

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

>>> oldFlames = ["Jennifer", "Susan", "Tina", "Bozo The Clown"]

The rules for choosing a list 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.

If you've worked with other programming languages, it should now be obvious that lists in Python are the equivalent of arrays in Perl, PHP and C. However, unlike these languages, Python does not restrict lists to elements of a specific object type, and can mix strings, numbers and even other lists within a single list "container".

>>> allMixedUp = ["ding dong", 23, "abracadabra", 26346.3, [4, "four"]]
>>> allMixedUp
['ding dong', 23, 'abracadabra', 26346.3, [4, 'four']]
>>> allMixedUp[0]
'ding dong'
>>> allMixedUp[4]
[4, 'four']
>>> allMixedUp[4][0]
>>> allMixedUp[4][1]

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