Sets and Type Convertions Flashcards

(32 cards)

1
Q

What is the syntax for creating a set?

A

my_set = {1, 2, 3}.

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

How to create an empty set?

A

Use set(): my_set = set() (not {}, which creates a dictionary).

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

What is the key property of sets?

A

Sets store unique elements (no duplicates).

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

How to add an element to a set?

A

Use .add(item) (e.g., my_set.add(4){1,2,3,4}).

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

How to add multiple elements to a set?

A

Use .update(): my_set.update([4,5]){1,2,3,4,5}.

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

How to remove an element from a set?

A

Use .remove(item) (raises error if item is missing) or .discard(item) (no error).

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

How to remove and return an arbitrary element from a set?

A

Use .pop() (e.g., my_set.pop() → removes and returns an element).

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

How to clear all elements from a set?

A

Use .clear(): my_set.clear()set().

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

How to check if an element exists in a set?

A

Use in: 2 in {1,2,3}True.

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

What is the union of two sets?

A

Use .union() or |: {1,2}.union({2,3}){1,2,3}.

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

What is the intersection of two sets?

A

Use .intersection() or &: {1,2}.intersection({2,3}){2}.

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

What is the difference between two sets?

A

Use .difference() or -: {1,2}.difference({2,3}){1}.

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

What is the symmetric difference between two sets?

A

Use .symmetric_difference() or ^: {1,2}.symmetric_difference({2,3}){1,3}.

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

How to check if one set is a subset of another?

A

Use .issubset(): {1,2}.issubset({1,2,3})True.

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

How to check if one set is a superset of another?

A

Use .issuperset(): {1,2,3}.issuperset({1,2})True.

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

How to create a set from a list?

A

Use set(): set([1,2,2]){1,2}.

17
Q

question

18
Q

How to convert a string to an integer?

A

Use int(): int('5')5.

19
Q

How to convert a string to a float?

A

Use float(): float('3.14')3.14.

20
Q

How to convert a number to a string?

A

Use str(): str(10)'10'.

21
Q

How to convert a list to a tuple?

A

Use tuple(): tuple([1,2,3])(1,2,3).

22
Q

How to convert a tuple to a list?

A

Use list(): list((1,2,3))[1,2,3].

23
Q

How to convert a list to a set?

A

Use set(): set([1,2,2]){1,2}.

24
Q

How to convert a set to a list?

A

Use list(): list({1,2})[1,2].

25
How to convert a dictionary to a list of keys?
Use `list()`: `list({'a':1})` → `['a']`.
26
How to convert a dictionary to a list of values?
Use `list()` with `.values()`: `list({'a':1}.values())` → `[1]`.
27
How to convert a dictionary to a list of key-value pairs?
Use `list()` with `.items()`: `list({'a':1}.items())` → `[('a',1)]`.
28
How to convert a string to a list of characters?
Use `list()`: `list('hello')` → `['h','e','l','l','o']`.
29
How to convert a list of strings to a single string?
Use `join()`: `''.join(['h','e','l','l','o'])` → `'hello'`.
30
How to convert a number to a boolean?
Use `bool()`: `bool(1)` → `True`, `bool(0)` → `False`.
31
How to convert a string to a boolean?
Non-empty strings are `True`; empty strings are `False` (e.g., `bool('text')` → `True`).
32
How to convert a list to a boolean?
Non-empty lists are `True`; empty lists are `False` (e.g., `bool([1,2])` → `True`).