Sets Flashcards

1
Q

What is a set

A

a set is an unordered collection of unique elements. Sets are very useful for storing and manipulating collections of data, especially when you want to eliminate duplicates or check for membership.

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

Create a set:

A

Sets are written with curly brackets.

thisset = {“apple”, “banana”, “cherry”}
print(thisset)

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

Set intersection

A

Return a set that contains the items that exist in both set x, and set y:

x = {“apple”, “banana”, “cherry”}
y = {“google”, “microsoft”, “apple”}

z = x.intersection(y)

print(z)

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

Are sets ordered

A

Note: 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.

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

Are sets changeable

A

Set items are unchangeable,

meaning that we cannot change the items after the set has been created.

Once a set is created, you cannot change its items, but you can remove items and add new items.

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

Do sets have duplicate values

A

Set items are unordered, unchangeable, and do not allow duplicate values.

Duplicate values will be ignored:

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

Get the Length of a Set

A

use the len() function.

thisset = {“apple”, “banana”, “cherry”}

print(len(thisset))

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

Set Items - Data Types

A

Set items can be of any data type:

Example
String, int and boolean data types:

set1 = {“apple”, “banana”, “cherry”}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}

A set can contain different data types:

Example
A set with strings, integers and boolean values:

set1 = {“abc”, 34, True, 40, “male”}

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

How to you turn data into a set:

A

It is also possible to use the set() constructor to make a set.

thisset = set((“apple”, “banana”, “cherry”)) # note the double round-brackets
print(thisset)

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