Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
93 Cards in this Set
- Front
- Back
How would you write this Python function? |
|
|
In Python, is this statement TRUE or FALSE? 5 is "five" |
FALSE |
|
What boolean would be returned if I put "w1 == w2"? |
True |
|
What boolean would be returned if I put "w1 is w2"? |
False |
|
How do most modern computers store and manipulate data as? |
As binary digits represented by bits (strings of 0s and 1s... or high and low voltage or dipoles or etc...) |
|
How do you define a class API? |
Choose a class name and write a brief description in the class docstring Write some examples of client code that uses your class Decide what services your class should provide as public methods Decode which attributes your class hsould provide without calling a method, list them in class dostring |
|
How would you implement a class? |
First, usually contain special methods such as __init__, __eq__ and __str__ Second, include body of other methods Finally, do some testing |
|
In class methods like __str__(self), what are some alternatives to using self? |
Python allows point, me, yo or ego as alternatives to self |
|
What is the first parameter of class methods? |
The first parameter in a method definition is a reference to the class isntance and method belongs to |
|
What does the special method __repr__ do? |
Returns a canonical string representation of the object Unambiguous |
|
What happens if you do not implement a special method __str__ in a class? |
Python will automatically use __repr__ in its place |
|
How do you write single line comments in Python? |
Using the "#" |
|
For the following class, write an __init__ for it: |
|
|
Write an __eq__ for the following class: |
|
|
Write an __str__ for the following class: |
|
|
Finish the distance_to_origin method: |
|
|
Finish the distance method: |
|
|
Finish the move method: |
|
|
Write an __ad__ to the following class: |
|
|
What would happen if other.x does not have an "x" attribute? |
It will produce an error |
|
Are attributes in Python, by default, public or private? |
Public |
|
What does it mean when a Python attribute is public? |
Meaning it is completely possible for anyone to have access to it |
|
What is Python's property function? |
property() Re-directs access to an attribute so that is must use a method Takes four arguments |
|
What attributes does the Python function property() take? |
Method to get value Method to set value Method to delete attribute/value Some documentation |
|
In Python, what is composition? |
Uses user-defined types in Python code anywhere you would use built-in types that include using user-defined types inside new user-defined types In other words, if we need feature of type A in user-defined type B, simply include an instance of A in our class B |
|
Why would you want to use composition in Python? |
Build increasing complex types using already-built types thus reducing amount of complexity have to deal with at any given time |
|
In Python, what is inheritance? |
Allows user to inherit methods and attributes and modify or extend some or even completely override them |
|
In Python, what is the difference between inheritance and composition? |
Composition: include an instance of exsting class in new class and use its features Inheritance: New class that inherit all methods and attributes of parent class |
|
Write an __init__ for the folowing class: |
|
|
Write a __str__ for the following class: |
|
|
Write an __eq__ for the following class: |
|
|
For the following class, why would the following code not be a correct choice for the __eq__ method? return self.num * other.denom == other.n |
Though it would work sometimes, it might not always work the way we want because of rounding error from limtations of representing numbers on the computer |
|
Finish the to_float function |
|
|
Write an __add__ for the following class: |
|
|
What is the special class __mul__? |
Return a * b, for a and b numbers (multiplication) |
|
Write an __mul__ for the following class: |
|
|
In Python, what is the special class __lt__? |
Less than |
|
Write a __lt__ for the following class: |
|
|
How would you denote something as private in Python? |
Using a leading underscore like _this in the method's name |
|
What does the leading underscore indicate in a Python method? |
"Private" (not actually private but by convention) |
|
Create a private method for the following class that changes the denominator |
|
|
What is raise Exceptions in Python? |
Brings fourth a message when an error occurs |
|
How do you import a class from another Python file? |
from [directory].[file_name] import [class] |
|
Create an __init__ for the following class: |
|
|
Finish this private method for this class |
|
|
Lets say a _set_perimeter was already created (making a self._perimeter), create a _get_perimeter for the following class |
|
|
Finish the _set_area method to create self._area |
|
|
Lets say a _set_area was already created (making a self._area), create a _get_area for the following class |
|
|
When creating a class, how do you inherit methods and attributes from another class? |
Put the name of the Class you wish to inherit in the class name For example, [class New_Class(to_inherit_class)] |
|
What does the Python function input([prompt] do? |
Read a string from standard input The trailing newline is stripped The prompt string,if given, is printed without a trailing newline before reading |
|
What does the Python function len(x) do? |
Returns an int Return the length of the list, tuple, dict, or string x |
|
What does the Python function open(name[, mode]) do? |
Returns file open for reading, writing, or appending Opens a file Legal modes are r (read), w (write), and a (append) |
|
What does the Python function range([start], stop, [step]) do? |
Returns list-like-object of int Return the integers starting with start and ending with stop - 1 with step specifyingthe amount to increment (or decrement) If start is not specified, the list starts at 0 If step is not specified,the values are incremented by 1 |
|
The following code is for creating a new list with the squares of all elements of L = list(range(100)) |
|
|
What is the general pattern for comprehensions? |
[expression for name in iterable if condition] |
|
What is an Abstract data types (ADTs)? |
An ADT specifies the intended meaning of data it stores and operations it provides on that data It is NOT about how to store and manipulate data in programming |
|
What is an ADT interface? |
Tells the clients what operations the ADT is capable of |
|
Why would changing an ADT after publishing it can be dangerous? |
1) You have to inform clients of change 2) It can cause clients to have to re-write their code |
|
What are the common types of ADTs? |
Sequence of items (can be added, removed, accessed, etc...) Collection of items accessed by their associated keys Specalized list where we only have access to most recently added item |
|
What is a stack class? |
Stack is another example of an ADT It stores items, provides operaiton to remove/pop top item, provides operation to add/push a new item ontop of the stack and provides a way to tell whether a stack is empty |
|
Give a verbal description of what a stack class does? |
So if you has function A calls function B, so function B must be removed first before function A can be removed from that stack With this, you cannot access the rest |
|
Why would you use unit testing (unittest) in your ADT? |
To make sure that your particular implementation remains consistent with your ADT's interface |
|
How would you use unit testing (unittest) in your ADT's interface? |
Import module unittest Subclass unittest.Testcase for your tests and begin each method that carries out a test with the string test Compose tests before and during implementation |
|
What is test driven philosophy? |
You write tests before you write the code so before you write a segment of a code, you can run tests during |
|
Let's say you are creating a new class (Square) but you are inherting from another (Shape). In your __init__, you want to keep the old corners property but create a new color and name, how would you do that? |
|
|
Stack classes need some way to determine if they are empty, write a code to determine if a stack is empty or not |
|
|
Create shorter/comprehensive code for the following: |
|
|
Create shorter/comprehensive code for the following: |
|
|
What is the setUp function/method? |
Creates (or set-ups) an empty version of a class being tested |
|
What is a tearDown function/method? |
Creates a fresh stack So each start of the test can begin with a clean, empty version that is independent of another test |
|
Why would you want a tearDown function/method be set to None during a test? |
Starts a new test with a clean, empty variable that is independent of another test When set to None, it can prevent multiple instances of the same variable when testing (like stack) |
|
When doing unit testing (unittest), how does the self.assertEqual or assert work? |
If an error occurs, the assert message will be displayed as an AssertionError message |
|
What does raise NotImplementedError do? |
If the subclass of the class this raise belongs to does not have the method that this raise belongs to, an error will occur asking for that method to exist |
|
Finish the following function: |
|
|
What does Python's isinstance() do? |
Return whether an object is an instance of a class or of a subclass thereof Parameters: obj, class or tuple, / |
|
What is depth in Python? |
How many list or sublists a input has Example: ([1, 2, 3]) has depth of 1 while ([1, [2, [3, 3, 3,], 2]]) has a depth of 3 |
|
What is a flat list or flat depth? |
No sublists (depth of 1) |
|
What is a base case for recursion? |
Ifinstance of data structure cannot be broken down into smaller substructures ofthe same sort and solution derived by solving those smaller problems, processthe data structure directly without recursion |
|
What is generative recursion? |
Recursive code that generates amller isntances of input data structure that correspond to smaller subproblems |
|
Why is this generative recursion? |
Even if you constructed integers as a recursive data structure, it is very unlikely that the structure of m or n would always have substructure m&n This is another case where the developer thought hard about the problem and generated the data for the recursive step: n and m%n |
|
Complete the following function: |
|
|
Complete the following function: |
|
|
Complete the following function: |
|
|
What helper methods does this function call? |
sum (already sums ints in a list) and isinstance |
|
What is the point of tracing through a function? |
To convience yourself that it works and you know what it is doing |
|
Trace sum_list(27) |
isinstance is False, go straight to else block and return 27 |
|
Trace sum_list([4, 1, 8]) |
|
|
Trace sum_list([4]) |
|
|
Trace sum_list([]) |
|
|
Trace sum_list([4, [1, 2, 3], 8]) |
|
|
Trace sum_list([[1, 2, 3], [4, 5], 8]) |
23 |
|
Trace sum_list([1, [2, 2], [2, [3, 3, 3], 2]]) |
|
|
Trace sum_list([1, [2, 2], [2, [3, [4, 4], 3, 3], 2]]) |
|