Types Flashcards

string, list, tuple, set, dictionary basics (12 cards)

1
Q

how to define a string

A

str = ‘hello’ or str =”hello”

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

indexing

A

alphabet[0] = a alphabet[-1] = z

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

how to define a list

A

my_list = [‘value1’, 2, my_var]

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

how to define a tuple

A

my_tuple = (‘item1’, ‘item2’)

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

how to define a named tuple

A

my_named_tuple = namedtuple(‘TupleName’, [‘item1’, ‘item2’])

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

what is a set

A

An unordered collection of elements.
Elements do not have positions or indices.
All elements must be unique (no duplicates).
Mutable: elements can be added or removed.
Elements must be hashable (e.g., numbers, strings, tuples of immutables).
Notation: curly braces or the set() constructor, e.g., {1, 2, 3} or set([1, 2, 3]).

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

what is a string

A

An ordered sequence of characters (text).
Elements do have positions (indexable).
Immutable: cannot change in place; operations create new strings.
Allows duplicates (e.g., repeated characters).
Notation: quotes, e.g., “hello” or ‘hello’.

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

what is a tuple

A

An ordered collection of elements.
Elements do have positions (indexable, sliceable).
Immutable: contents cannot be changed after creation.
Allows duplicates and mixed types.
Notation: parentheses, e.g., (1, “a”, 1.5); single-item tuple needs a trailing comma, e.g., (42,).

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

what is a list

A

An ordered collection of elements.
Elements do have positions (indexable, sliceable).
Mutable: can add, remove, or modify elements in place.
Allows duplicates and mixed types.
Notation: square brackets, e.g., [1, “a”, 1.5].

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

what is a dictionary

A

A mapping of keys to values (associative array).
Keys are unique; values may repeat.
Preserves insertion order (Python 3.7+), but access is by key, not by position.
Mutable: can add, remove, or modify key–value pairs.
Keys must be hashable (e.g., strings, numbers, tuples of immutables).
Notation: curly braces with key: value pairs, e.g., {“a”: 1, “b”: 2}.

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

how to create a set

A

a set is created with the set function
set([1,2,3])
it is often used as a way to remove duplicates from a list

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

how to create a dictionary

A

my_dict = {‘key1’: ‘value1’, ‘key2’:’value2’}

players = {
‘Lionel Messi’: 10,
‘Cristiano Ronaldo’: 7
}

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