Home arrow Python arrow Page 3 - Python Sets

Copying Sets and Testing for Membership - 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.

  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



If you want to copy a Set, you can do so pretty simply:


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

gang = mcdonaldgang.copy()

print gang

Here would be the result of this program:

  Fry Guy Grimace Mayor Mccheese Hamburglar Burger King Wendy

Suppose the gang had grown so large Ronald couldn't keep track of it in his head. To see if someone was in his gang, he could use the In operator.


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

'Grimace' in mcdonaldgang

'Birdie' in mcdonaldgang

This program checks to see if Grimace and Birdie are in the McDonald's Gang. Here is the result:



Meaning that Grimace is in the gang, but Birdie is not. Who cares...she was annoying anyway. And now you know where those chicken nuggets come from.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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