03 Data Structures Flashcards

1
Q

What kinds of data structures did we learn of?

A
  • sequence types
  • unordered collections
  • mapping types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are sequence types of data structures?

A

It’s an ordered list of values where the position/index of the values is used to access them.
ie: list, tuple, string, range

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

What are unordered collections?

A

unordered set of unique elements.
ie: set

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

What are mapping types of data structures?

A

It’s a group of key-value-pairs where unique keys are used to access their values.
ie: dictionary

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

What is the difference between a list and a tuple?

A

A list is mutable (can be changed), a tuple is immutable (can’t be changed).

(everytime a tuple is changed a new tuple with the same name is created and the old one gets deleted.)

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

True or False: in Python, a list only contains a group of values of the same type.

A

False. It can contain items of variable data types.

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

True or False: The order of items in a list is preserved.

A

True

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

How is a list created?

A

Using square brackets containing comma-separated values.

ie: my_list = [0, 5.5, “string”, 8]

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

True or False: in Python, a list contains a group of values of various type.

A

True

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

How are tuples created?

A

Via a number of values separated by commas.
ie: my_tuple = 0, 5.5, “string”, 8
or: my_tuple = (0, 5.5, “string”, 8)

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

How is an empty set created?

A

By using the method set().
ie: my_set = set()

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

How is a set with at least one element created?

A

By using curly braces and comma separated values.
ie: my_set = {0, 5.5, “string”, 8}

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

What are the values in set3?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 & set2

A

{3}

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

What are the values in set3?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 | set2

A

{1, 2, 3, 4, 5}q

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

What are the values in set3?

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 - set2

A

{1, 2}

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

Which of the following statement/s is/are correct?

  1. Dictionaries are immutable and unordered.
  2. Dictionaries are mutable and unordered.
  3. Dictionaries are mutable and ordered.
  4. Dictionaries are immutable and ordered.
A

3 is correct.

17
Q

In Python dictionaries, what can be used as keys?

A

any hashable object

18
Q

In Python dictionaries, what can be used as values?

A

any object

19
Q

How are dictionaries created?

A

my_dict = {key1: value1, key2: value2}
or
my_dict = dict(key1=value1, key2=value2)

20
Q

What’s the output of these statements?

my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
my_dict[1] = “soup”
print(my_dict)

A

Error in line 2

When creating a dictionary using the dict() function, the keys are automatically converted to strings. So getting the value of key 1 isn’t possible. Only the value of key “1” can be accessed.

21
Q

What’s the output of these statements?

my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
my_dict[“1”] = “soup”
print(my_dict)

A

{“1”: “soup”, “2”: “main”, “3”: “dessert”}

Because when creating a dictionary using the dict() function, the keys are automatically converted to strings.
With the second statement the value of key “1” is changed from “starter” to “soup”.

22
Q

What’s the output of these statements?

my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
my_dict = {1: “starter”}
print(my_dict)

A

{1: “starter”}

In line 1 the dictionary is created and in line 2 it is overwritten. This time the creation is done with curly braces so the keys stay the same data type, in this case an integer.

23
Q

What’s the output of these statements?

my_dict = {1=”starter”, 2=”main”, 3=”dessert”}
my_dict[“8”] = “cheese”
print(my_dict)

A

{1: “starter”, 2: “main”, 3: “dessert”, “8”: “cheese”}

In line 1 the creation is done with curly braces so the keys stay the same data type, in this case an integer.
Then, in line 2, a new entry is added but this time the key is a string.

24
Q

What’s the output of these statements?

my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
soup = my_dict[“1”]
print(my_dict)

A

{“1”: “starter”, “2”: “main”, “3”: “dessert”}

In line 1 the creation is done using the dict() function so the keys are automatically converted to keys.
In line 2 the value of key “1” is saved to a variable “soup”.
The dictionary is still the same as when it was created, it hasn’t changed.

25
Q

What is List Comprehension?

A

A compact way to loop over an inerable, optionally execute actions on each element and store the results in a list. It can also optionally include conditions to filter the elements for which the actions should be executed.

26
Q

What is the syntax for a list comprehension that includes actions and a condition?

A

new_list = [code-to-be-executed for element in iterable if condition]

27
Q

What is slicing, what is its syntax and what are its default values?

A

It’s a way to select a range of items in a sequence (ie in a list, in a string, in a set,…).

Syntax:
sequence[start:end:step]

Default values:
start: 0 (inclusive)
end: length of sequence (exclusive)
step: 1

28
Q

What’s the output of these statements?

my_list = [1, 2, 3, 4, 5]

  1. print(my_list[2:4:1])
  2. print(my_list[1:4])
  3. print(my_list[:4:3])
  4. print(my_list[0::2])
  5. print(my_list[::-1])
A
  1. [3, 4, 5]
  2. [2, 3, 4] (step default value is 1)
  3. [3] (start default value is 0)
  4. [2, 4] (end default value is last item)
  5. [5, 4, 3, 2, 1] (negativ step = reverse order)
29
Q

What does unpacking of a sequence mean?

A

Assigning the values of a sequence to a series of variables.