Python
  Home arrow Python arrow Page 3 - Python Conditionals, Lists, Dictionaries, and Operators
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 Conditionals, Lists, Dictionaries, and Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    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:
      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 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
     

       

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek