Foreground
Foreground
D0Z - motion / graphic / dev๐Ÿงบ

Brain ๐Ÿ”’

โœ•

brain

CAUTION

Some files are private or not ready yet, if a link gives you a 404 that's why :)
This is a work in progress, new notes will be added daily

List of note Indexes


                .... |\| .|||.    |\/|  .|\\. .|.
              .\\||/|.  .\|..||  .\|..|||...||.....
            .|   .....||.|\\\\\\    ... |\\\/. .\\. .||
        .\\|   .|\\|..|\\ .\\\\  .\\|. .\\\\\|  |\| |\\|
        |\\  |\\|..  /\\\. |\|\\   \\\\|.   .|\\. .\\\\|
        .\\. |\\\\\\\|.. .\\|      .\\||\\/|. |\\|     .|/\|
      \. .\\\||/\|....   \\| |\/   |\\\  |\\\| .\\|  \\| ....
    |\/  |\\| |\\\\\\\| |\|.\\\   \\\\. |\\\\| |\\  \\.  ||..
      ..|||\|  .. .\\\\. |\\\\\\  .\\|....|\\\. |\\\\\\  |/. |/
    \\\|..   .\\\ |\\. .\\\| \\|   |||\\\.  ...|\\/||.  .\\\
  |\|  |\/.  /\\\\\| .\\\\. \\.   .../|.   .|\\\|.   |\\\|  |\\
    .  |||\\|.  ..||| .\\|.  |\|  \\|..../\\| |\\|  .\\\\\   ..
    \\|  .||\|.|..      |\\\|./\  \\||\. ||...    .\\\\/.  |\/||
    ..|/||.   |\\\\\|.     .|/\|  ||.        .||\\\     .|||. |\
  .\/|  \\\. .|\\\|||\\\||..       .|\\\\\\\|\\\\.  /\\\\||. |
    .|| |\\\\|  .......|\\\\\\.  .\\\. ..|||....  .\\\\|.. ...
    \\||....|\\\\\\\\||\\\\\\\/   |\|....|||\\\\\\\/....|\\\|
      ||.|\||\\| .....|\\\\..\\|    .|\\\. ||........ .|\|...
          . |\  /\\/\\\\\\. |\.  .\\\\\\.  ./\\\\/  |\\\||.
        .|\.|\| .|\|... ..              ./\\|..|\\|  ..
            .|\\|  .\\\\.\\\\\\\   \||...\\\|..|\\\|.||
                |\. ..||....||     ... ..||\\\. .|.
                        ...            .......

โ†‘ Python


Python - Sets

An unordered and unindexed collection of unique values. In opposition to lists and tuples a set cannot contain duplicate values. The values of a set are immutable but you can add or remove values of the set.

my_set = {1, 2, 3, 4, 'hello'}

[!TIP] Difference with lists:

  • Lists: ordered values where the order matter, values can have duplicates and can be accessed using an index
  • sets: do not keep the order or allow duplicates of values and you cannot use indexes.

Adding values

Using add(<value>), if the value already exists it won't be added

my_set = {1, 2, 3, 4, 'hello'}
my_set.add(5)
my_set.add(3) # Nothing happens

Removing values

  • remove(<value>): if the value does not exist raise an error
  • discard(<value>): if the value does not exist do nothing
  • pop(<value>): removes the last element and returns it (but keep in mind order is not guaranteed so you don't know what you're removing)
my_set = {1, 2, 3, 4, 'hello'}
my_set.remove(1)
my_set.remove(5) # Raise an error: KeyError: 5
my_set.discard(5) # Nothing happens
my_set.pop()

Find values

# check if a value exists in a set
my_set = {1, 2, 3, 4, 'hello'}
3 in my_set # True
5 in my_set # False

Logical operations on sets

# Sets used for the examples
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

Intersection

Returns shared values between two sets

intersection = set1 & set2  # {3, 4}

Difference

Returns values that are in the first set but not the second

difference = set1 - set2  # {1, 2}

Union

Returns a set containing all the values of both sets without duplicates

union = set1 | set2  # {1, 2, 3, 4, 5, 6}

Subset

Check if all the values of a set are in another set

subset = {1, 2} 
is_subset = subset.issubset(set1) # True

Superset

Check if all the values of another set are in the current set

superset = {1, 2} 
is_superset = set1.superset(superset) # True

Other operations

  • copy(): Copy a set
  • clear(): Empty all values of a set
  • symmetric_difference(): Returns the values unique to each set
  • update(): Add values from another set to the current set
set1 = {1, 2, 3}
set2 = {3, 4, 5}

sym_diff = set1.symmetric_difference(set2)  # {1, 2, 4, 5}

set_copy = set1.copy()

set1.clear()
ยฉ 2024 d0z.eu | 0.1.0-beta.39