Python Book
πŸ‡ΊπŸ‡¦ Stand with UkraineπŸŽ“Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. 3. Containers

Sets

Set is unordered mutable collection of unique elements

Set is just like dictionary without values. Sets do not support indexing, slicing, or other sequence-like behavior.

Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Elements can be only hashable objects - just like keys of dictionary (all immutable objects and instances of classes).

Ways to create a set:

  • set()

  • {1, 2, 3}

  • set([1, 2, 3, 4])

  • set("abcdeabcde")

πŸͺ„ Code:

some_list = [1, 2, 3, 4]
some_tuple = tuple(some_list)
set_a = set(some_list)
set_b = set(some_tuple)
print(set_a)
print(set_b)

πŸ“Ÿ Output:

{1, 2, 3, 4}
{1, 2, 3, 4}

Main methods

πŸͺ„ Code:

[method for method in dir({1}) if not method.startswith("__")]

πŸ“Ÿ Output:

['add',
 'clear',
 'copy',
 'difference',
 'difference_update',
 'discard',
 'intersection',
 'intersection_update',
 'isdisjoint',
 'issubset',
 'issuperset',
 'pop',
 'remove',
 'symmetric_difference',
 'symmetric_difference_update',
 'union',
 'update']

A lot of different methods for checking various mathematical set properties

Set methods

Method(s)
Description

len()

Return int - length of the set

x in set_a

Return True/False - is object xis part of the set set_a?

set_a | set_b

Return new set - a union (all from both)

set_a & set_b

Return new set - a intersection (common to both)

set_a - set_b

Return new set - a difference (those in set_a that are not in set_b)

set_a < set_b

Return True/False - a issubset (set_a is proper subset of set_b)

set_a > set_b

Return True/False - a issubset (set_b is proper subset of set_a)

PreviousDictionariesNextConditions

Last updated 2 years ago

Was this helpful?