GithubHelp home page GithubHelp logo

python-p3-sets's Introduction

Sets

Learning Goals

  • Utilize Python's set data type to accomplish several programming tasks.
  • Execute and test Python code using the Python shell.

Key Vocab

  • Sequence: a data structure in which data is stored and accessed in a specific order.
  • Index: the location, represented by an integer, of an element in a sequence.
  • Iterable: able to be broken down into smaller parts of equal size that can be processed in turn. You can loop through any iterable object.
  • Slice: a group of neighboring elements in a sequence.
  • Mutable: an object that can be changed.
  • Immutable: an object that cannot be changed. (Many immutable objects appear mutable because programmers reuse their names for new objects.)
  • List: a mutable data type in Python that can store many types of data. The most common data structure in Python.
  • Tuple: an immutable data type in Python that can store many types of data.
  • Range: a data type in Python that stores integers in a fixed pattern.
  • String: an immutable data type in Python that stores unicode characters in a fixed pattern. Iterable and indexed, just like other sequences.

Introduction

Sets are the last built-in data structure in Python. Sets share the following characteristics:

  • Set elements are unique. Duplicates are not supported.
  • Set elements are unordered. This means that they cannot be accessed by index.
  • Sets are mutable, but can only store immutable data.

Like lists, tuples, and dictionaries, sets can be defined using their class constructor or with brackets:

my_set = set([1, 2, 3])
my_set = {1, 2, 3}

NOTE: Instantiating an empty set requires the use of the set() class constructor. Closed curly brackets {} will instantiate an empty dictionary.


When Are Sets Used?

Sets are much less commonly used than sequences and maps, but they can be helpful in accomplishing a handful of common tasks:

Instantiating a set using a sequence is a good way to isolate unique members:

my_list = [1, 2, 1, 3, 2]
set(my_list)
# {1, 2, 3}

...though it is important to remember that sets are unordered, so our output might not be pretty:

my_string = "the big red cat ate the fat rat"
set(my_string)
# {'g', 'h', 'b', 'r', 'e', 'd', 'f', 'c', 't', 'a', 'i', ' '}

To determine if two collections have the same members, we can check their sets:

set(range(1, 10)) == set([1, 2, 3, 4, 5, 6, 7, 8, 9])
# True

If the sets are not identical, we can use the intersection & operator to see what they have in common:

set_1 = {1, 2, 3}
set_2 = {3, 4, 5}
set_1 & set_2
# 3

...or we can check for differences using the difference - operator:

set_1 = {1, 2, 3}
set_2 = {3, 4, 5}
set_1 - set_2
# {1, 2}
set_2 - set_1
# {4, 5}

Using these operators also allows us to dynamically modify sets as our program runs:

set_1 = {1, 2, 3}
set_2 = {3, 4, 5}
set_1 &= set_2
# {3}
set_2 -= set_1
# {4, 5}

Sets also support comprehensions with the same syntax as lists:

sentence = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
unique_consonants = {c.lower() for c in sentence if c not in "aeiou ,."}
# {'g', 'p', 'b', 'l', 'r', 'd', 'm', 'q', 'c', 't', 's', 'n'}

For more set features, see the Python set documentation.


Conclusion

Sets are not the most common data structure in Python, but they can be useful when you need to create and compare unique collections of data. Remember that sets are unique, unordered, and mutable.

Resources

python-p3-sets's People

Contributors

lizbur10 avatar professor-ben avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.