Python
  Home arrow Python arrow Page 2 - Python Sets
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 Sets
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-12-03

    Table of Contents:
  • Python Sets
  • Adding to a Set
  • Copying Sets and Testing for Membership
  • Removing Data from A Set
  • Don't Make Me Repeat Myself!
  • Using Operators on Sets

  • 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


    Python Sets - Adding to a Set


    (Page 2 of 6 )

    Let's say the Burger King wasn't having things his way and decided to quit and join the McDonald's gang. After a rough initiation (he gunned down Jack from Jack in the Box), he is allowed in. Since Ronald is a demanding boss, we want to add him to our Set. We could do so this way:


    #!/usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese'])

    print mcdonaldgang

    mcdonaldgang.add('Burger King')

    print mcdonaldgang

    The above would first print out our original set, then add the Burger King to it, and print it once more:

      Hamburglar, Grimace, Mayor Mccheese

      Hamburglar Burger King Grimace Mayor Mccheese

    Let's say not only the Burger King, but Wendy herself wanted to join the gang. To add more than one new element at the same time, we would use the Update function:


    !/usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese'])

    print mcdonaldgang

    mcdonaldgang.update(['Burger King', 'Wendy'])

    print mcdonaldgang

    Again, we set the initial values for the set mcdonaldgang, printed those values, then used the update function to add two more elements to the set. The resulting print out would be:

      Grimace Mayor Mccheese Hamburglar

      Hamburglar Mayor Mccheese Grimace Wendy Burger King

    Again note that Sets do not allow for duplicate values. Let's say the Fry Guys all wanted to join the McDonald's Gang. Let's add them to our database and see what happens.


    /usr/local/bin/python


    mcdonaldgang = Set (['Grimace', 'Hamburglar', 'Mayor Mccheese', 'Burger King', 'Wendy'])

    print mcdonaldgang

    mcdonaldgang.update(['Fry Guy', 'Fry Guy', 'Fry Guy'])

    print mcdonaldgang

    This will result in the print out:

      Grimace Mayor Mccheese Hamburglar Burger King Wendy

      Fry Guy Grimace Mayor Mccheese Hamburglar Burger King Wendy

    As you can see it only adds the one Fry Guy. Too bad for the rest of them. That's what you get for all having the same name.

    More Python Articles
    More By James Payne


       · I am curious, Why didn't the author use the Python shell for the tutorials?-...
       · Thanks for dropping by to read my article. In this issue we discuss Sets in Python;...
       · The tutorial didn't work for me.I got a message saying that "Set" was not...
       · I tested it on my end and it worked. Let me look into it a little more and get back...
       · Thanks James,Here is a bit more information for you. Hope it is useful.The error...
       · The reason it doesn't work is because the name of the function to create a set is...
     

       

    PYTHON ARTICLES

    - 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
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT