Python Set methods Flashcards

(26 cards)

1
Q

What does the add() method do in a set?

A

Adds a single element to the set.

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

True or false: The remove() method raises an error if the element is not found.

A

TRUE

Use discard() to avoid errors when removing elements.

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

What is the purpose of the clear() method?

A

Removes all elements from the set.

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

Fill in the blank: update() merges another set or iterable into the set, adding _______.

A

elements

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

What does the discard() method do?

A

Removes an element from the set without raising an error.

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

Define pop() in the context of sets.

A

Removes and returns an arbitrary element from the set.

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

What does the intersection() method return?

A

A new set containing elements common to both sets.

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

True or false: union() creates a set with unique elements from both sets.

A

TRUE

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

What is the function of the difference() method?

A

Returns a set of elements in one set but not in another.

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

Fill in the blank: symmetric_difference() returns elements that are in either set, but not _______.

A

both

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

What does the issubset() method check?

A

Determines if one set is a subset of another.

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

What is the purpose of the issuperset() method?

A

Checks if one set contains all elements of another set.

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

True or false: Sets can contain duplicate elements.

A

FALSE

Sets automatically remove duplicates.

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

What does the copy() method do?

A

Creates a shallow copy of the set.

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

Fill in the blank: The frozenset() function creates an immutable _______.

A

set

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

What is the output of len() on a set?

A

Returns the number of unique elements in the set.

17
Q

Define set comprehension.

A

A concise way to create sets using an expression and a loop.

18
Q

What does the difference_update() method do?

A

Removes elements found in another set from the current set.

19
Q

True or false: Sets are ordered collections.

A

FALSE

Sets do not maintain any order of elements.

20
Q

What does the pop() method return if the set is empty?

A

Raises a KeyError.

21
Q

Fill in the blank: The intersection_update() method modifies the set to keep only _______.

A

common elements

22
Q

What is the result of set1 | set2?

A

The union of set1 and set2.

23
Q

What does set1 & set2 represent?

A

The intersection of set1 and set2.

24
Q

Define set difference.

A

The elements in one set that are not in another set.

25
What does the **symmetric_difference_update()** method do?
Updates the set to contain elements in either set, but not both.
26
Fill in the blank: The **isdisjoint()** method checks if two sets have _______.
no elements in common