Python
  Home arrow Python arrow Page 3 - Python Conditionals, Lists, Dictionari...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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 Conditionals, Lists, Dictionaries, and Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-11-26

    Table of Contents:
  • Python Conditionals, Lists, Dictionaries, and Operators
  • Elif
  • Lists
  • Tuples
  • Dictionaries
  • Operators

  • 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
     
     
    CIO Insight
     
    ADVERTISEMENT

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Help Simplify Virtualization by IBM

    Python Conditionals, Lists, Dictionaries, and Operators - Lists
    (Page 3 of 6 )

    We covered variables in our previous tutorial. Now we are going to cover Lists. You may have heard of arrays in other programming languages; a list is the Python equivalent. Whereas a variable holds a value, a list holds a bunch of values. Each value can be referenced by its position in the list, starting with the number 0 and moving forward. Here is an example:


    #!/usr/local/bin/python


    suess = ['I', 'like', 'green', 'eggs', 'and', 'spam']


    print suess

    print suess[0]

    The above program creates a List named suess, and assigns a bunch of data to it, separating each piece of data with a comma (,). It then prints the entire List, and finally prints one element in the list, in this case the first one (0). Here it is!

      I like green eggs and spam

      I

    You can also change the value of an element in a list later on:


    #!/usr/local/bin/python


    suess = ['I', 'like', 'green', 'eggs', 'and', 'spam']

    print “This is the data in the list originally:”, suess


    suess[5] = 'ham'

    print “This is the data in the list after the lawsuit:”, suess

    Again, we fill up our suess List with data. Then we print it. Next, we change one specific element in the suess List from spam to ham (remember the first element is 0, so spam would be element 5). We then print it out with the new data, like so:

      This is the data in the list originally: I like green eggs and spam

      This is the data in the list after the lawsuit: I like green eggs and ham

    The number of elements in a list is not a set number; you can add and delete elements until the cows come home. And let's face it, once those cows have had a taste of the good life, they are never returning to your crummy studio apartment.


    #!/usr/local/bin/python


    suess = ['I', 'like', 'green', 'eggs', 'and', 'spam']

    print “This is the data in the list originally:”, suess


    suess[6] = 'yummy!'

    print 'Here is the list with appended elements:', suess

    This results in:

      This is the data in the list originally: I like green eggs and spam

      Here is the list with appended elements: I like green eggs and spam yummy!

    More Python Articles
    More By James Payne


       · In [7]: suess = ['I', 'like', 'green', 'eggs', 'and', 'spam']In [8]: print...
       · I used a combo box in form along with text boxes.. now when the user submit the...
     

       

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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