SunQuest
 
       Python
  Home arrow Python arrow Page 5 - 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 
VeriSign Whitepapers 
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 - Looking Up The Dictionary


    (Page 5 of 6 )

    Unlike tuples, dictionaries are a "mutable" object type, which means that the elements contained within them can be changed at will.

    As you've already seen, adding a new element to a dictionary is as simple as assigning a value to a key.

    >>> characters = {"new hope":"Luke", "teacher":"Yoda", "bad guy":"Darth"} >>> characters["princess"] = "Leia" >>> characters["worse guy"] = "The Emperor" >>> characters {'princess': 'Leia', 'teacher': 'Yoda', 'new hope': 'Luke', 'bad guy': 'Darth', 'worse guy': 'The Emperor'} >>>
    You can also use this technique to update existing dictionary elements with new values,

    >>> characters["teacher"] = "Obi-Wan" >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth', 'worse guy': 'The Emperor'} >>>
    or use the del() method to remove individual key-value pairs from the dictionary.

    >>> del (characters["worse guy"]) >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>>
    A built-in update() method makes it possible to copy elements from one dictionary to another.

    >>> charactersCopy = {} >>> charactersCopy.update(characters) >>> charactersCopy {'teacher': 'Obi-Wan', 'princess': 'Leia', 'new hope': 'Luke', 'bad guy': 'Darth'} >>>
    while a clear() method allows you to empty the container of all elements,

    >>> charactersCopy.clear() >>> charactersCopy {} >>>
    The built-in len() function can be used to calculate the number of key-value pairs in a dictionary.

    >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> len (characters) 4 >>>
    It should be noted at this point that dictionaries do not support concatenation or repetition, and, since they don't use numerical indices, it's not possible to extract slices of a dictionary either. In fact, attempting this will cause Python to barf all over your screen,

    >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> opposites {'day': 'night', 'white': 'black', 'yes': 'no'} >>> newDict = characters + opposites Traceback (innermost last): File "", line 1, in ? TypeError: bad operand type(s) for + >>>
    as will the deadly sin of trying to access a key which doesn't exist.

    >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> characters["robot"] Traceback (innermost last): File "", line 1, in ? KeyError: robot >>>
    As a matter of fact, you can use the has_key() built-in method to check whether or not a particular key exists, thereby making it easier to avoid errors like the one above.

    >>> characters {'princess': 'Leia', 'teacher': 'Obi-Wan', 'new hope': 'Luke', 'bad guy': 'Darth'} >>> characters.has_key("teacher") 1 >>> characters.has_key("princess") 1 >>> characters.has_key("robot") 0 >>>

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