03 Data Structures Flashcards
What kinds of data structures did we learn of?
- sequence types
- unordered collections
- mapping types
What are sequence types of data structures?
It’s an ordered list of values where the position/index of the values is used to access them.
ie: list, tuple, string, range
What are unordered collections?
unordered set of unique elements.
ie: set
What are mapping types of data structures?
It’s a group of key-value-pairs where unique keys are used to access their values.
ie: dictionary
What is the difference between a list and a tuple?
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.)
True or False: in Python, a list only contains a group of values of the same type.
False. It can contain items of variable data types.
True or False: The order of items in a list is preserved.
True
How is a list created?
Using square brackets containing comma-separated values.
ie: my_list = [0, 5.5, “string”, 8]
True or False: in Python, a list contains a group of values of various type.
True
How are tuples created?
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 is an empty set created?
By using the method set().
ie: my_set = set()
How is a set with at least one element created?
By using curly braces and comma separated values.
ie: my_set = {0, 5.5, “string”, 8}
What are the values in set3?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 & set2
{3}
What are the values in set3?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 | set2
{1, 2, 3, 4, 5}q
What are the values in set3?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1 - set2
{1, 2}
Which of the following statement/s is/are correct?
- Dictionaries are immutable and unordered.
- Dictionaries are mutable and unordered.
- Dictionaries are mutable and ordered.
- Dictionaries are immutable and ordered.
3 is correct.
In Python dictionaries, what can be used as keys?
any hashable object
In Python dictionaries, what can be used as values?
any object
How are dictionaries created?
my_dict = {key1: value1, key2: value2}
or
my_dict = dict(key1=value1, key2=value2)
What’s the output of these statements?
my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
my_dict[“1”] = “soup”
print(my_dict)
{“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”.
What’s the output of these statements?
my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
my_dict = {1: “starter”}
print(my_dict)
{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.
What’s the output of these statements?
my_dict = {1=”starter”, 2=”main”, 3=”dessert”}
my_dict[“8”] = “cheese”
print(my_dict)
{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.
What’s the output of these statements?
my_dict = dict(1=”starter”, 2=”main”, 3=”dessert”)
soup = my_dict[“1”]
print(my_dict)
{“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.
What is List Comprehension?
A compact way to loop over an iterable, 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.
What is the syntax for a list comprehension that includes actions and a condition?
new_list = [code-to-be-executed for element in iterable if condition]
What is slicing, what is its syntax and what are its default values?
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
What’s the output of these statements?
my_list = [1, 2, 3, 4, 5]
- print(my_list[2:4:1])
- print(my_list[1:4])
- print(my_list[:4:3])
- print(my_list[0::2])
- print(my_list[::-1])
- [3, 4]
- [2, 3, 4] (step default value is 1)
- [1, 4] (start default value is 0)
- [1, 3, 5] (end default value is last item)
- [5, 4, 3, 2, 1] (negativ step = reverse order)
What does unpacking of a sequence mean?
Assigning the values of a sequence to a series of variables.