Quiz 3- Chapter’s 9-11 Flashcards

1
Q

define a dictionary

A

an object that stores a collection of data.

elements are ordered, changeable and DO NOT allow duplicates

KEY VALUE PAIRS are elements

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

Is a key immutable or mutable

A

Key’s are immutable

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

are dictionaries mutable?

A

yes

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

How do you retrieve a specific value from a dictionary?

A

You use the key associated with it

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

Dictionaries are ordered. What does that mean

A

that the items have a defined order and that order will not change. Unordered means that the items do not have a defined order and you cannot refer to an item by using an index

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

dictionary format:

A

{key1:val1,key2:val2,key3:val3}

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

What will this do?

variable = {}

What built-in function will accomplish the same thing?

A

It will create an empty dictionary.

dict() will accomplish the same thing

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

what will this do?

cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}

print(cars)

A

it will display the dictionary.

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

What will this do?

dictionary[key] = value

A

Add the new element to the dictionary. If the key already exists it will change the value associated with it

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

dictionary[key]

What will this do?

A

retrieve the value from the dictionary. If key is in the dictionary, otherwise KeyError exception is raised.

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

What’s a way to test and see if a key is in a dictionary?

A

using the in and not in operators.

if ‘’model’’ in cars:
print(‘’Yes, ‘model’ is a key’’)

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

cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}

x = cars.values()
print(x)

What is the output?

A

dict_values([‘Ford’,’Mustang’,1964])

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

How do you retrieve elements from a dictionary?

cars = {‘’brand’’:’’Ford’’,’’model’’:’’Mustang’’,’’year’’:1964}

A

x = cars.items()
print(x)

it will display the key value pair

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

del dictionary[key]

Will do what?

A

delete the key-value pair in the dictionary. If key is not in dictionary, KeyError exception is raised.

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

What does the len function do?

A

Used to obtain the number of elements in a dictionary.

Key must be immutable but values can be any type of object.

One dictionary can include keys of several different immutable tuples.

Values stored in a single dictionary can be of different types

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

clear method

A

deletes all the elements in a dictionary, leaving it empty

format: dictionary.clear()

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

What does the get method do?

A

gets a value associated with a specified key from dictionary

format: dictionary.get(key,default)

default is returned if key is not found

alternative to [] operator

CANNOT RAISE KEYERROR EXCEPTION

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

items method:

A

returns all the dictionaries keys and values

format: dictionary.items()

returned as dictionary view

each element in dictionary view is a tuple which contains a key and its value
USE A FOR LOOP to iterate over the tuples in the sequence

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

keys method:

A

returns all the dictionary keys as a sequence

format: dictionary.keys()

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

pop method:

A

returns value with specified key and removes that key-value pair from the dictionary

format: dictionary.pop(key,default)

default is returned if key is not found

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

popitem method:

A

returns, as a tuple, the key value pair that was last added to the dictionary. The method also removes the pair from the dictionary.

format: dictionary.popitem()

KEY VALUE PAIR RETURNED AS TUPLE

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

values method:

A

returns all the dictionary values as a sequence

format: dictionary.values()

use for loop to iterate over values

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

dictionary comprehension:

A

an expression that reads a sequence of input elements and uses those input elements to produce a dictionary

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

what is this an example of:

squares = {item:item**2 for item in numbers}

A

dictionary comprehension.

The results expressions is item:item**2

the iteration expression is for item in numbers

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

names = { ‘Jeremy’, ‘Kate’,’Peg’}

variable = {item:len(item) for item in names}

variable

What will the output produce?

A

It will display the length of each name for each item in the list

{‘Jeremy’:6,’Kate’,:4,’Peg’:3}

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

dict1 = {‘A’:1,’B‘:2,’C’:3}
dict2 = {k:v for k,v in dict1.items()}
dict 2

What will this do?

A

produce a copy of the first dictionary

{‘A’:1,’B‘:2,’C’:3}

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

Can you use an if clause in dictionary comprehension statements?

A

Yes, to select only certain elements of the input sequence.

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

set:

A

an object that stores a collection of data in same ways as mathematical set

-all items must be unique (no duplicates)
-set is unordered
- elements can be of different data types

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

set function:

A

used to create a set

for empty set: call set()

for non empty set: call set(argument) where argument is an object that contains iterable elements. (ex: list, string, tuple, can all be argument) but if the argument is a string each character becomes a set element. For set of strings, pass them to the function as a list.

if argument contains duplicates, only one of the duplicates will appear in the list

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

len function for a set:

A

returns the number of elements in a set

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

Are sets mutable or immutable?

A

mutable

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

add method (for a set)

A

adds an element to a set

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

update method (for a set)

A

updates a group of elements to a set. (arguments must be a sequence containing iterable elements and each of the elements is added to the set)

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

remove and discard method both do what? (for a set)

A

remove the specified item from the set.

The item that should be removed is passed to both methods as an argument.

Behave differently when THE SPECIFIED ITEM IS NOT FOUND IN THE SET

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

what error does remove method raise when specified item is not found in a set?

A

KeyError exception

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

What error does discard method raise when the specified item is not found in the set?

A

IT DOES NOT RAISE AN EXCEPTION

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

clear method (for a set):

A

clears all the elements of the set.

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

What kind of loop can be used to iterate over each element in a set?

how many times does the loop iterate for each item in the set?

A

A for loop. Once.

format: for item in set:

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

What operator can test whether a value is in set or not?

A

in, not in

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

Union of 2 sets:

A

a set that contains all the elements of both sets

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

How do you find the union of 2 sets:

A

use the union methodL
format: set1.union(set2)

or

use the | operator
format: set1 | set2

BOTH RETURN A NEW SET WHICH CONTAINS THE UNION OF BOTH SETS

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

Intersection of 2 sets:

A

a set that contains only the elements found in both sets

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

How do you find the intersection of 2 sets?

A

use the intersection method:
format: set1.intersection(set2)

or

use the & operator
format: set1 & set2

BOTH TECHNIQUES RETURN A NEW SET WHICH CONTAINS THE INTERSECTION OF BOTH SETS

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

Difference of 2 sets:

A

a set that contains elements that appear in the first set but do not appear in the second set

45
Q

How do you find the difference of 2 sets:

A

Use the difference method:
Format: set1.difference(set2)

or

Use the - operator:
format: set1 - set2

46
Q

Symmetric difference of 2 sets:

A

a set that contains the elements that are not shared by the 2 sets

47
Q

How to find the symmetric difference of 2 sets:

A

use the symmetric_difference method:
format: set1.symmetric_difference(set2)

or

use the ^ operator:
format: set1 ^ set2

48
Q

When is set A a subset of set B?

A

if all the elements in set A are included in set B

49
Q

To determine whether set A is a subset of set B:

A

use the issubset method:
format: setA.issubset(setB)

or

use the <= operator:
format: setA <= setB

50
Q

set A is a superset of set B if

A

it contains all the elements of set B

51
Q

To determine whether set A is a superset of set B:

A

use the issuperset method:
format: setA.issuperset(setB)

use the >= operator
format: setA >= setB

52
Q

set comprehension:

A

a concise expression that creates a new set by iterating over the elements of a sequence.

Set comprehensions are written like comprehensions except they are enclosed in curly brackets {}

53
Q

serializing objects:

A

convert the object to a stream of bytes that can easily be stored in a file

54
Q

pickling:

A

serializing an object

55
Q

Procedural programming

A

writing programs made of functions that perform specific tasks.

data items commonly passed from one procedure to the next

56
Q

Is python an object-oriented programming language?

A

Yes, almost everything in python is an object

57
Q

object-oriented programming

A

focused on creating objects

58
Q

object:

A

entity that contains data and procedures

59
Q

data is known as

A

data attributes

60
Q

procedures are known as

A

methods

61
Q

What do methods do?

A

perform operations on the data attributes

62
Q

encapsulation:

A

combining data and code into a single object

63
Q

data hiding (object?)

A

data attributes are hidden from code outside the object

outside code does NOt need to know the internal structure of the object,

PROTECTS AGAINST ACCIDENTAL CORRUPTION

64
Q

object reusability:

A

the same object can be used in different programs

65
Q

data attributes:

A

define the state of an object:

ex: clack would have second, minute, and hour attributes

66
Q

public methods:

A

allow external code to manipulate the object

67
Q

private methods:

A

used for object’s inner workings

68
Q

class:

A

code that specifies the data attributes and methods of a particular type of object (blueprint, cookie cutter)

69
Q

instance

A

an object created from a class

there can be many instances of 1 class

class: set of instructions for how to make an instance

70
Q

class Cookie():

does what

A

creates a class named Cookie

71
Q

Class definitions:

A

set of statements that define a class’s methods and data attributes

format: class Class_Name:

class names often begin with uppercase letter

72
Q

self parameter:

A

required in every method in the class_references the specific object that the method is working on

73
Q

initializer method:

A

automatically executed when an instance of the class is created:

format: def__init__(self):

usually first method in class definition
DOES NOT HAVE TO BE CALLED SELF

74
Q

What will this do?

car = class_name()

A

create an instance of a class

75
Q

to call a class what notation do you use

A

dot notation:

my_instance.method()

76
Q

what does this do

self.__current_minute

?

A

makes sure that the object’s data attributes are private by placing two underscores in front of the attribute

77
Q

accessing attributes is done through?

A

dot notation
ex: self.name

78
Q

you call method using

A

dot notation
ex: my_cat.play()

79
Q

The self parameter is a reference to

A

the current instance of the class

80
Q

self (in classes) is used to

A

access variables that belongs to the class.

the parameter does not have to be named self but it must be the first parameter of any function in teh class

81
Q

Pure or Accessor methods:

A

a function which creates an object, initializes its attributes, and returns a reference to the new object

ex: get_model is an accessor method

82
Q

Modifiers or Mutator methods

A

a function which modifies the objects it gets as parameters where the changes are visible to the caller

ex: set_model

83
Q

Mutator methods are sometimes called____ and accessor methods are sometimes called ____

A

Mutator: “setters”
accessor: “getters”

84
Q

modifier:

A

a function that modifies the object it gets as parameter.

Changes are visible to the caller (function)

85
Q

Object properties can be modified and deleted?

A

yes:
p1 = Person(“john”,36)

p1.age = 40

print(p1.age)

40

del p1.age
print(p1.age). gives you an AttributeError (object person has no attribute age)

86
Q

Object oriented programming languages usually require what kind of functions?

A

pure functions:

programs are faster and less error-prone

87
Q

Can class definitions be empty?

A

No, but you can use the pass method to avoid the error message (same as functions)

88
Q

Instance dictionary :

A

an interval value used by classes

89
Q

object’s state:

A

the values of the object’s attributes at any given moment

90
Q

__str__(self) helps you do what

A

control what information gets printed for a class.

91
Q

UML stands for what

A

unified modeling language

92
Q

UML definition:

A

standard diagram for graphically depicting object-oriented systems

top: name of class
middle: attributes
bottom: methods

93
Q

Inheritance:

A

when one class inherits from another. It takes on the attributes and methods of the 1st class

94
Q

parent class:

A

the original class

95
Q

child class:

A

the new class which inherits any or all of the attributes and methods of its parent class AND is also free to define new methods and attributes

96
Q

“Is a” relationship:

A

exists when 1 object is a specialized version of another object

-the specialized object has all the characteristics of the general object plus unique characteristics

ex: rectangle is a shape, daisy is a flower

97
Q

Inheritance is used to create what kind of relationship between classes

A

“is a” relationship

98
Q

Superclass (base or parent class)

A

a general class

99
Q

subclass( derived or child class)

A

a specialized class that is an extended version of the superclass but can have its own new attributes and methods added.

100
Q

Using __init__ for child will initialize any attributes that were defined in the parent __int__() which makes:

A

all or selected attributes available in the child class

101
Q

polymorphism:

A

an objects ability to take different forms

102
Q

what are some essential ingredients of polymorphic behavior?

A
  • ability to define a method in a superclass and override it in a subclass
    (subclass defines method with same name)
  • ability to call the correct version of overridden method depending on the type of object that called for it
103
Q

does polymorphism provide great flexibility when designing programs?

A

yes

104
Q

AttributeError exception:

A

raised when a method receives an object which is not an instance of the right class

105
Q

isinstance function

A

determines whether an object is an instance of a class

format: isinstance(object, class)

106
Q

__init__ is short for, and does what

A

“initialization”, and it is a special method that gets invoked when an object is instantiated

107
Q

__str__() does what

A

it is a special method that is supposed to return a string representation of an object

when you print an object python invokes the str method implicitly.

The __str__ method is useful for debugging

108
Q

polymorphism:

A

functions that work with several types and encourages code reuse (AKA the intent of object oriented programming CODE REUSE)