Home arrow Python arrow Python Sets

Python Sets

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

Setting It Up

Although I said Lists do not have indexes in the previous article, it isn't quite true, as I demonstrated. Sets, which are used to hold a bunch of unordered values, truly have no index. And unlike Tuples and Lists, Sets can contain no duplicate data. To assign value to a set, simple do this:


#!/usr/local/bin/python


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

gangpassword = Set ("rubble")


print mcdonaldgang

print gangpassword

The above code creates a set and assigns values to it (Grimace, Hamburglar, and Mayor McCheese). We then create a second set with the gang's password in it (rubble). Finally, we print out the values of each. Note that there is no index in Sets, so there is no particular order that the values would print:

  Grimace Mayor Mccheese Hamburglar

  Rebubl



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