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  
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 101 (part 4): Feeding The Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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 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

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