Quiz 3- Chapter’s 9-11 Flashcards

(108 cards)

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
names = { ‘Jeremy’, ‘Kate’,’Peg’} variable = {item:len(item) for item in names} variable What will the output produce?
It will display the length of each name for each item in the list {‘Jeremy’:6,’Kate’,:4,’Peg’:3}
26
dict1 = {‘A’:1,’B ‘:2,’C’:3} dict2 = {k:v for k,v in dict1.items()} dict 2 What will this do?
produce a copy of the first dictionary {‘A’:1,’B ‘:2,’C’:3}
27
Can you use an if clause in dictionary comprehension statements?
Yes, to select only certain elements of the input sequence.
28
set:
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
29
set function:
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
30
len function for a set:
returns the number of elements in a set
31
Are sets mutable or immutable?
mutable
32
add method (for a set)
adds an element to a set
33
update method (for a set)
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)
34
remove and discard method both do what? (for a set)
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
35
what error does remove method raise when specified item is not found in a set?
KeyError exception
36
What error does discard method raise when the specified item is not found in the set?
IT DOES NOT RAISE AN EXCEPTION
37
clear method (for a set):
clears all the elements of the set.
38
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 for loop. Once. format: for item in set:
39
What operator can test whether a value is in set or not?
in, not in
40
Union of 2 sets:
a set that contains all the elements of both sets
41
How do you find the union of 2 sets:
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
42
Intersection of 2 sets:
a set that contains only the elements found in both sets
43
How do you find the intersection of 2 sets?
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
44
Difference of 2 sets:
a set that contains elements that appear in the first set but do not appear in the second set
45
How do you find the difference of 2 sets:
Use the difference method: Format: set1.difference(set2) or Use the - operator: format: set1 - set2
46
Symmetric difference of 2 sets:
a set that contains the elements that are not shared by the 2 sets
47
How to find the symmetric difference of 2 sets:
use the symmetric_difference method: format: set1.symmetric_difference(set2) or use the ^ operator: format: set1 ^ set2
48
When is set A a subset of set B?
if all the elements in set A are included in set B
49
To determine whether set A is a subset of set B:
use the issubset method: format: setA.issubset(setB) or use the <= operator: format: setA <= setB
50
set A is a superset of set B if
it contains all the elements of set B
51
To determine whether set A is a superset of set B:
use the issuperset method: format: setA.issuperset(setB) use the >= operator format: setA >= setB
52
set comprehension:
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
serializing objects:
convert the object to a stream of bytes that can easily be stored in a file
54
pickling:
serializing an object
55
Procedural programming
writing programs made of functions that perform specific tasks. data items commonly passed from one procedure to the next
56
Is python an object-oriented programming language?
Yes, almost everything in python is an object
57
object-oriented programming
focused on creating objects
58
object:
entity that contains data and procedures
59
data is known as
data attributes
60
procedures are known as
methods
61
What do methods do?
perform operations on the data attributes
62
encapsulation:
combining data and code into a single object
63
data hiding (object?)
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
object reusability:
the same object can be used in different programs
65
data attributes:
define the state of an object: ex: clack would have second, minute, and hour attributes
66
public methods:
allow external code to manipulate the object
67
private methods:
used for object’s inner workings
68
class:
code that specifies the data attributes and methods of a particular type of object (blueprint, cookie cutter)
69
instance
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
class Cookie(): does what
creates a class named Cookie
71
Class definitions:
set of statements that define a class’s methods and data attributes format: class Class_Name: class names often begin with uppercase letter
72
self parameter:
required in every method in the class_references the specific object that the method is working on
73
initializer method:
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
What will this do? car = class_name()
create an instance of a class
75
to call a class what notation do you use
dot notation: my_instance.method()
76
what does this do self.__current_minute ?
makes sure that the object’s data attributes are private by placing two underscores in front of the attribute
77
accessing attributes is done through?
dot notation ex: self.name
78
you call method using
dot notation ex: my_cat.play()
79
The self parameter is a reference to
the current instance of the class
80
self (in classes) is used to
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
Pure or Accessor methods:
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
Modifiers or Mutator methods
a function which modifies the objects it gets as parameters where the changes are visible to the caller ex: set_model
83
Mutator methods are sometimes called____ and accessor methods are sometimes called ____
Mutator: “setters” accessor: “getters”
84
modifier:
a function that modifies the object it gets as parameter. Changes are visible to the caller (function)
85
Object properties can be modified and deleted?
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
Object oriented programming languages usually require what kind of functions?
pure functions: programs are faster and less error-prone
87
Can class definitions be empty?
No, but you can use the pass method to avoid the error message (same as functions)
88
Instance dictionary :
an interval value used by classes
89
object’s state:
the values of the object’s attributes at any given moment
90
__str__(self) helps you do what
control what information gets printed for a class.
91
UML stands for what
unified modeling language
92
UML definition:
standard diagram for graphically depicting object-oriented systems top: name of class middle: attributes bottom: methods
93
Inheritance:
when one class inherits from another. It takes on the attributes and methods of the 1st class
94
parent class:
the original class
95
child class:
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
“Is a” relationship:
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
Inheritance is used to create what kind of relationship between classes
“is a” relationship
98
Superclass (base or parent class)
a general class
99
subclass( derived or child class)
a specialized class that is an extended version of the superclass but can have its own new attributes and methods added.
100
Using __init__ for child will initialize any attributes that were defined in the parent __int__() which makes:
all or selected attributes available in the child class
101
polymorphism:
an objects ability to take different forms
102
what are some essential ingredients of polymorphic behavior?
- 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
does polymorphism provide great flexibility when designing programs?
yes
104
AttributeError exception:
raised when a method receives an object which is not an instance of the right class
105
isinstance function
determines whether an object is an instance of a class format: isinstance(object, class)
106
__init__ is short for, and does what
“initialization”, and it is a special method that gets invoked when an object is instantiated
107
__str__() does what
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
polymorphism:
functions that work with several types and encourages code reuse (AKA the intent of object oriented programming CODE REUSE)