Python
  Home arrow Python arrow Page 6 - 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  
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 
Actuate Whitepapers 
Moblin 
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 4): Feeding The Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python 101 (part 4): Feeding The Snake - Of Keys And Locks


    (Page 6 of 6 )

    Two of the most important and useful methods associated with Python dictionaries are the keys() and values() methods, used to return a list of the keys and values stored in a dictionary respectively. The next example should make this clearer:

    >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> characters.keys() ['princess', 'teacher', 'new hope', 'bad guy'] >>> characters.values() ['Leia', 'Obi-Wan', 'Luke', 'Darth'] >>>
    Since dictionaries do not use numeric indices, it would not normally be possible to iterate through a dictionary using a "for" loop. However, since both keys() and values() return a list (which *is* compatible with "for"), they can come in extremely handy. Take a look at the following Python program, which sets up a dictionary mapping years to director names, and then iterates through the dictionary with a "for" loop.

    #!/usr/bin/python # define a dictionary directors = {1995:"Mel Gibson", 1996:"Anthony Minghella", 1997:"James Cameron", 1998:"Steven Spielberg", 1999:"Sam Mendes", 2000:"Steven Soderbergh"} # loop through for x in directors.keys(): print "And the Oscar for Best Director in", x, "goes to", directors[x]
    Here's the output.

    And the Oscar for Best Director in 1999 goes to Sam Mendes And the Oscar for Best Director in 1998 goes to Steven Spielberg And the Oscar for Best Director in 1997 goes to James Cameron And the Oscar for Best Director in 1996 goes to Anthony Minghella And the Oscar for Best Director in 1995 goes to Mel Gibson And the Oscar for Best Director in 2000 goes to Steven Soderbergh
    In case you'd like it in chronological order, you can always use the sort() function on the list.

    #!/usr/bin/python # define a dictionary directors = {1995:"Mel Gibson", 1996:"Anthony Minghella", 1997:"James Cameron", 1998:"Steven Spielberg", 1999:"Sam Mendes", 2000:"Steven Soderbergh"} # get and sort the list years = directors.keys() years.sort() # loop through for x in years: print "And the Oscar for Best Director in", x, "goes to", directors[x]
    In this case, I've first obtained a list of keys, sort()ed them, and then used the sorted list in the "for" loop.

    As opposed to keys() and values(), the items() method returns something a little more complex - a list of tuples, each tuple containing a key-value pair from the dictionary. It's actually quite elegant - take a look!

    >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> characters.items() [('princess', 'Leia'), ('teacher', 'Obi-Wan'), ('new hope', 'Luke'), ('bad guy', 'Darth')] >>>
    And that's about it for today. While this article was not as code-intensive as the previous ones (hey, even you need a break!), we've nevertheless broken a lot of new ground. You now know about two of Python's more unusual data structures, the tuple and the dictionary, have a fair idea of the capabilities and features of each, and will be able to impress your friends with your exhaustive knowledge of Best Director Oscar-winners.

    In the next article, we'll be moving away from built-in Python data structures to something a little more interesting: interacting with files on the system. Python can be used to read data from, and write data to, files on your filesystem - and I'll be giving you all the gory details next time. Make sure you come back for that!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    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 1 hosted by Hostway