Python
  Home arrow Python arrow Page 2 - Python 101 (part 4): Feeding The Snake
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Python 101 (part 4): Feeding The Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2001-06-25


    Table of Contents:
  • Python 101 (part 4): Feeding The Snake
  • Running The Lights
  • Strange Food
  • Unbreakable
  • Looking Up The Dictionary
  • Of Keys And Locks

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python 101 (part 4): Feeding The Snake - Running The Lights
    ( Page 2 of 6 )

    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 linux-i386 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] 'spiders' >>>
    while

    >>> CreepyCrawlies[2] 'lizards' >>>
    - 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] 'ola' >>> allMixedUp[2] 'pink fox, yellow moon' >>> allMixedUp[4] [4, 'four'] >>> allMixedUp[4][1] 'four' >>> allMixedUp[5][2] 'green' >>>
    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) >>> lights = ("red", "green", "blue") >>> type(lights) >>>
    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.

     
     
    >>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek