Python
  Home arrow Python arrow Page 2 - Python 101 (part 3): A Twist In The Ta...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
PYTHON

Python 101 (part 3): A Twist In The Tail
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2001-06-13

    Table of Contents:
  • Python 101 (part 3): A Twist In The Tail
  • Here Comes A Hero
  • Making Friends And Influencing People
  • We Don't Need Another Hero
  • Looping The Loop
  • Twist And Turn
  • Within Range()
  • Just Passin' Through

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 3): A Twist In The Tail - Here Comes A Hero


    (Page 2 of 8 )

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

    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, asyour Python programs grow in complexity, you're going to need more advanceddata structures to store and manipulate information. And that's exactlywhere lists come in.

    Unlike string and number objects, which typically hold a single value, alist can best be thought of as a "container" variable, which can containone 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 thelist are accessed via an index number, with the first element starting atzero. So, to access the element "Superman", you would use the notation

    >>> superheroes[0] 'Spiderman' >>>
    while

    >>> superheroes[3] 'Batman' >>>
    - essentially, the list name followed by the index number enclosed withinsquare braces. Geeks refer to this as "zero-based indexing".

    Defining a list is simple - simply assign values (enclosed in squarebraces) 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 anyother Python variable - it must begin with a letter, and can optionally befollowed by more letters and numbers.

    If you've worked with other programming languages, it should now be obviousthat lists in Python are the equivalent of arrays in Perl, PHP and C.However, unlike these languages, Python does not restrict lists to elementsof a specific object type, and can mix strings, numbers and even otherlists 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] 4 >>> allMixedUp[4][1] 'four' >>>

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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway