Home arrow Python arrow Page 6 - Python Sets

Using Operators on Sets - Python

In our last article we left off discussing Python's version of arrays (the list and dictionary). I also gave you a brief introduction to some Operators. In this tutorial I will tell you about Python's remaining data holder, Sets, and prepare you for a later discussion of Operators in Python.

TABLE OF CONTENTS:
  1. Python Sets
  2. Adding to a Set
  3. Copying Sets and Testing for Membership
  4. Removing Data from A Set
  5. Don't Make Me Repeat Myself!
  6. Using Operators on Sets
By: James Payne
Rating: starstarstarstarstar / 11
December 03, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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...



 
 
>>> More Python Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: