SunQuest
 
       Python
  Home arrow Python arrow Page 6 - 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 
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 Sets
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    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

    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 Sets - Using Operators on Sets


    (Page 6 of 6 )

    In addition to the normal mathematical operators you can use with Sets, the following are also useful:

    Union

    If you wish to merge two Sets, you can do so with the Union operation:


    usr/local/bin/python


    firstset = Set([1, 2, 3])

    secondset = Set([1, 4, 5, 6])

    firstset.union(secondset)


    print firstset

    This will create a union between the two Sets and print the following (remember: it will not store duplicate data):

      1 2 3 4 5 6

    Intersection

    If you want to find out if two Sets have any data that are the same, you can use the Intersection operation. Behold!


    usr/local/bin/python


    firstset = Set([1, 2, 3])

    secondset = Set([1, 4, 5, 6])


    firsttest.intersection(secondset)

    This will return any data that is the same in both Sets. The result here would be:

      1

    This is because both sets have an element with the value 1 in it.

    Symmetric Difference

    If you want to print the values in two Sets that are not the same, you can use the Symmetric Difference function.


    usr/local/bin/python


    firstset = Set([1, 2, 3])

    secondset = Set([1, 4, 5, 6])


    firstset.symmetric_difference(secondset)

    This would result in the values 2, 3, 4, 5, 6. It will not show 1, because one is in both Sets.

    Set Difference

    If you want to find what values are in firstset that are not in secondset, you can use the Set Difference operation. I know you are dying to see it, so here it is:


    usr/local/bin/python


    firstset = Set([1, 2, 3])

    secondset = Set([1, 4, 5, 6])


    firstset.difference(secondset)

    This results in the values 2 and 3. This is because the values 2 and 3 are not in the secondset Set, whereas the number 1 is.

    So we did not get to discuss Operators in this article; however, we will definitely discuss them in our next episode, as they will be the focus. In the meantime, here is that Operator Table again, just to refresh your memory:


    Symbol

    Type

    What it Does

    +

    Mathematical

    Addition

    -

    Mathematical

    Subtraction

    *

    Mathematical

    Multiplication

    /

    Mathematical

    Division

    //

    Mathematical

    Truncating Division

    **

    Mathematical

    Powers

    %

    Modulos

    Returns the remainder from a division

    <<

    Shift

    Left Shift

    >>

    Shift

    Right Shift

    &

    Logical

    And

    |

    Logical

    Or

    ^

    Logical

    Bitwise XOR

    ~

    Logical

    Bitwise Negation

    <

    Comparison

    Less than

    >

    Comparison

    Greater than

    '=='

    Comparison

    Equal to

    !=

    Comparison

    Not Equal To

    >=

    Comparison

    Greater than or Equal To

    <=

    Comparison

    Less than or Equal To

    '='

    Assignment

    Assigns a value

    +=

    Assignment

    Adds and assigns a value

    -=

    Assignment

    Subtracts and Assigns a value

    *=

    Assignment

    Multiplies and assigns a value

    /=

    Assignment

    Divides and assigns a value

    //=

    Assignment

    Truncate Divides and assigns a value

    **=

    Assignment

    Powers and assigns

    %=

    Assignment

    Modulus and assigns

    >>

    Assignment

    Shifts and assigns

    <<

    Assignment

    Shifts and assigns

    And

    Boolean


    Or

    Boolean


    Not

    Boolean


    Till then...


    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.

       · 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

    - 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