Chapter 8 Flashcards

1
Q

What is a set and what can you put in it?

A

A set is a mutable container of UNORDERED elements (no indices) that are unique (no duplicates).

Created using the set() function.

Holds sequence-type iterable objects (i.e. lists, tuples, strings)

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

How do you make a set literal?
How do you make an empty set?

A

set{}
set()

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

In a set, the index operator…

A

…is not valid, because a set contains unique unordered elements with no set position.

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

A set is often used to…

A

…reduce a list of items that potentially contains duplicates into a collection of unique values

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

How can you remove duplicates from a list?

A

Pass the list into a set() to make it into a set of unique values
Format:
ex_list = []
unique_set = set(ex_list)

#copies ex_list to unique_set w/o duplicates

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

Are sets mutable or immutable?

A

Mutable

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

How do you add elements to a set?
How do you remove elements?
What does the .pop function do?

A

set.add(value)
set.remove(value)
set.pop() removes a random element

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

How would you add the elements of set_2 to set_1?
How would you clear ALL values in a set?

A

set1.update(set_2)
set.clear()

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

This function returns a set containing the common elements shared between set and all provided sets

A

set.intersection(set_a, set_b, set_c, …)

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

This function returns a set containing all the unique elements in all given sets.

A

set.union(set_a, set_b, set_c, …)

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

Function that returns a set containing only the values in set that are not found in the given argument sets.

A

set.difference(set_a, set_b, set_c, …)

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

Function that returns a set containing only values found in exactly one of either set_a or set_b

A

set_a.symetric_difference(set_b)

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

What is a dictionary?
What is the object type?
What is its mutability?

A

A dictionary is a container used to associate/map keys (terms placed in a dict) with values.

Represented by object type dict

Dictionaries are mutable.

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

What is a key?
What types can be used for a key?

A

A key is a term that can be located in a dictionary, (such as the word “cat” in the English dictionary), and is associated with a value through the dict.

A key can consist of any immutable type (string, tuple, etc.), the value can be of any type.

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

What is the syntax for creating a dictionary?

A

ex_dict =
{
“Example Key 1” : 77
“Example Key 2” : 86
}

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

What will cause a KeyError when calling an item in a dictionary?
How do you avoid this?

A

Dictionary elements cannot be indexed, and thus accessing entries by indexing will result in a KeyError.

We avoid this by putting the key itself in brackets where an index would normally be located when accessing.

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

Are elements in a dictionary ordered or unordered?

A

Dictionary elements are ordered, despite the fact that they cannot be indexed.

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

How do you add or modify an entry in a dictionary?

A

adds key “ex_key” and assigns value “ex_val”

ex_dict[ex_key] = ex_val

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

How do you delete an entry from a dictionary?

A

deletes key “ex_key” from dictionary “dict”

del ex_dict[ex_key]

20
Q

What does the sort() function do?
What rules does it follow?

A

sort() re-arranges the elements of a list from lowest value to highest.

Numbers are sorted by value, strings are sorted according to ASCII/Unicode values, lists are compared element by element, etc.

21
Q

What does the sorted() function do?
How does this differ from the sort() function?

A

sorted() sorts the elements the same as list.sort(), but returns the elements as a new list rather than rearranging the elements of the existing list.

22
Q

What is the purpose of the optional key argument in the list.sort() and sorted() functions?

A

The key argument specifies a function to be applied to each element prior to comparing their values.

For example, “key=str.lower” as the key argument will convert all elements to lower case before comparing their values.

23
Q

When using list.sort() or sorted(), how would you reverse the resulting list of elements?

A

Use the optional argument: reverse=True

24
Q

What are the 2 primary ways to create a dict?

A

1). Wrap curly braces { } around key-value pairs (whether literals or variables).
i.e.: {‘Jose’: ‘A+’, ‘Gino’: ‘C-‘}
2). Use the dict function
i.e.: dict(Bob=’97’, John=’83’)

25
How do you test for the existence of a key in a dictionary?
***ex_key in ex_dict***
26
What is a hash? Why does this matter in the context of dictionaries?
A **hash** is a transformation of a key into a unique value that allows the interpreter to perform fast lookup. When using a loop to iterate through a dictionary, the order in which elements are evaluated is according to their **hash** value, not necessarily the order they're placed in the dictionary.
27
What is a ***view object***?
A ***view object*** provides *read-only* access to dictionary keys and values (in real time). A program can iterate over a ***view object*** to access key-value pairs in a dictionary one at a time.
28
This function returns a view object that yields a tuple of key-value pairs from a dict.
***dict.items()***
29
This function returns a view object that yields keys from a dict.
***dict.keys()***
30
This function returns a view object that yields values from a dict.
***dict.values()***
31
What is a nested dictionary? How many layers of nesting can you have?
When a dictionary contains another dictionary as one of its values. There is no limit.
32
What is a ***data structure***?
A ***data structure*** is a method of organizing data in a logical and coherent fashion.
33
What is ***NumPy***?
***NumPy*** is a package that provides tools for mathematical computations, frequently used in data science and statistical analysis.
34
What is an array? How does it differ from other data types we have learned about?
It is an object that is conceptually similar to a list, with the added benefit of more mathematical support and performing mathematical operations faster.
35
What is ***ndarray***
***ndarray*** is the numpy array data type, where the nd stands for 'n'-dimentional, and 'n' can be any number of dimensions.
36
How do dimensions work in arrays? (i.e. how does the structure of an array change with each additional dimension?)
An "N"-dimensional array has **"N" levels of nested containers**. At each level, every container must have the **same number of elements**. The **shape** of an array is a *tuple* of the *lengths* of each of its *dimensions*. The size of an array is the total number of elements.
37
In a 2D array, the 0-axis runs along...
...the array's **rows**
38
True or False: arrays are mutable and their shapes can be changed.
**False** While arrays are mutable, their **shapes cannot be changed** after creation.
39
This function returns an ***ndarray*** based on a given object, like a list.
***array(object)***
40
These functions each return an ***ndarray*** of a specified *shape* filled with zeros, ones, or a specified value.
***zeros(array_shape)*** ***one(array_shape)*** ***full(arr_shape, value)***
41
This function returns the element located at indices [row_index, col_index].
***array[row_index, col_index]***
42
This function returns a new ndarray with a row or column deleted from the given *ndarray*.
***delete(ndarray, index, axis)*** Deletes row or col indicated by the index. If axis = 0, delete row. If axis = 1, delete column.
43
This function sorts an *ndarray* in place in ascending order along an axis. If axis=None, the array is flattened into a 1D array, then sorted. If no argument is passed, sorting occurs along the last axis.
***ndarray.sort(axis)***
44
This function returns a flattened (1D) version of the given *ndarray*.
***ndarray.ravel()***
45
This function returns a new *ndarray* containing the elements of the given *ndarray* with a new shape
***ndarray.reshape(new_shape)***
46