Python Set Methods Flashcards

1
Q

Adds an element to the set

A

add()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Removes all the elements from the set

A

clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Returns a copy of the set

A

copy()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Returns a set containing the difference between two or more sets

A

difference()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Removes the items in this set that are also included in another, specified set

A

difference_update()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Remove the specified item

A

discard()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Returns a set, that is the intersection of two or more sets

A

intersection()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Removes the items in this set that are not present in other, specified set(s)

A

intersection_update()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Returns whether two sets have a intersection or not

A

isdisjoint()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Returns whether another set contains this set or not

A

issubset()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Returns whether this set contains another set or not

A

issuperset()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Removes an element from the set

A

pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Removes the specified element

A

remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Returns a set with the symmetric differences of two sets

A

symmetric_difference()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

inserts the symmetric differences from this set and another

A

symmetric_difference_update()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Return a set containing the union of sets

A

union()

17
Q

Update the set with another set, or any other iterable

A

update()

18
Q

What is a python set?

A
  • Sets are used to store multiple distinct items in a single variable.
  • A set is a collection which is unordered, unchangeable*, and unindexed.
  • Set items are unchangeable, but you can remove items and add new items.
  • Sets are written with curly brackets.
  • Set items are unordered, unchangeable, and do not allow duplicate values.
  • Since sets are unordered, so you cannot be sure in which order the items will appear.
  • Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
  • Sets cannot have two items with the same value.
  • Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
  • To determine how many items a set has, use the len() function.
  • Set items can be of any data type:
19
Q

How are sets created?

A
  • Sets are written with curly brackets
    myset = {“apple”, “banana”, “cherry”}
  • It is also possible to use the set() constructor to make a set.
    thisset = set((“apple”, “banana”, “cherry”)) # note the double round-brackets