Python Flashcards

1
Q

What does this operator do %?

A

returns a remainder

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

What does this operator do //?

A

math.floor(a / b)

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

What happens when a subclass inherits from a base class?

A

The subclass inherits all the methods, properties, and attributes of the base class.

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

What are some of the features/advantages of python

A

Supports multiple inheritance.

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

What is multiple inheritance?

A

Multiple inheritance is the ability to derive a class from multiple base classes at the same time. Multiple inheritance has a bad reputation to the extent that most modern programming languages don’t support it. Instead, modern programming languages support the concept of interfaces. In those languages, you inherit from a single base class and then implement multiple interfaces, so your class can be re-used in different situations.

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

what is pythons MRO?**Hint multiple inheritance

A

Method resolution order. The MRO shows the order of classes in which Python is going to look for a matching attribute or method. it can be accessed on the class attribute .__mro__ we can use this to troubleshoot multiple inheritance problems.

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

What are some disadvantages of multiple inheritance?

A

1) We can end up creating tightly coupled relationships that become too dependent on each other. 2) Create complex relationships between classes that become hard to follow and debug. 3) It can create redundancy if two classes inherit from the same base class. The opposite approach would be to do a composition design. loosely coupled, flexible design.

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

What is __str__( ) method in python?

A

It provides a verbose, readable string representation of your class. invoked when print( ). is called with an object. it’s considered unofficial documentation str() is used for creating output for end-user while repr() is mainly used for debugging and development. repr’s goal is to be developer-friendly and ideally shows how to recreate the object and str’s meant to be user friendly and readable

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

What is Liskovs substitution principle in relation to python?**HINT THIS HAS TO DO WITH INHERITANCE.

A

Liskov’s substitution principle is the most important guideline to determine if inheritance is the appropriate design solution. Inheritance should only be used to model an “is a” relationship. Liskov’s substitution principle says that an object of type Derived, which inherits from Base, can replace an object of type Base without altering the desirable properties of a program.

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

what is the __dict__ attribute in python?

A

Creating a class implicitly inherents from object . __dict__ is one of the members of the object class and is basically. a mapping of all the attributes in an object to their value.

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

What does dictionary comprehension syntax look like?

A

{i : chr(65+i) for i in range(4)}

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

what’s the difference between item( ) and iteritem( ) on a python dictionary?

A

The difference only matters In python 2. in python 3 items( ) Return an iterator over the dictionary’s (key, value) pairs. we use this to iterate over the dictionary

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

When should you use inheritance? When should you use them over composition?

A

1) Use inheritance over composition in Python to model a clear “IS A” relationship. 2) Use inheritance over composition in Python to leverage BOTH the interface and implementation of the base class. 3) Use inheritance over composition in Python to provide mixin features to several unrelated classes when there is only one implementation of that feature.

If you’re able to justify an inheritance between two classes both ways they probably should not derive from each other.

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

describe isinstance( ) in python?

A

Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If the object is not an object of the given type, the function always returns False.

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

What is the __repr__( ) method in python?

A

__repr__() compute the “official” string representation of an object (a representation that has all information about the object). Used for debugging and should be wrapped in backquotes.

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

When should you use composition over inheritance?

A

1) Use composition over inheritance in Python to model a has a relationship that leverages the implementation of the component class. 2) Use composition over inheritance in Python to create components that can be reused by multiple classes in your Python applications. 3) Use composition over inheritance in Python to implement groups of behaviors and policies that can be applied interchangeably to other classes to customize their behavior.

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

Describe a lambda function.

A

A lambda function is used to describe an anonymous function in python. An anonymous function is a function with no name. Although syntactically they look different, lambda functions behave in the same way as regular functions that are declared using the def keyword.

18
Q

Describe characteristics of lambda functions

A

1) A lambda function can take any number of arguments, but they contain only a single expression. An expression is a piece of code executed by the lambda function, which may or may not return any value.
2) Syntactically, lambda functions are restricted to only a single expression.
3) Structure - lambda argument(s): expression
4) The lambda operator cannot have any statements and it returns a function object that we can assign to any variable.
5) useful as temporary functions for higher-order functions (functions that take other functions as arguments)

19
Q

what is a set?

A

An unordered list with no duplicates.

20
Q

Describe generator functions in python.

A

generators are functions that contain a yield keyword. The yield keyword returns a value and the function state is saved until it’s evaluated again. It causes a pause in execution.

21
Q

What is a generator?

A

Generators are a special type of iterator in python that is lazily evaluated. It’s particularly useful for iterating over large lists and sequences because the entire sequence isn’t stored in memory.

22
Q

What is a decorator?

A

Decorators add additional functionality or modify the behaviour of your functions without changing its definition.

23
Q

How do decorators work?

A

It wraps and processes your function, then returns the wrapped function.

24
Q

Describe some of the changes between Python 2 and 3.

HINT 4

A

1) print( ).
2) Unicode support natively.
3) Exception handling syntax is slightly different.
4) The division operater ( / ) performs floating-point division in python 3. the operator ( // ) floor division is what ( / ) was in python 2

25
Q

What is super( )

A

At a high level super gives you access to a parent classes attributes and methods. All methods that are called with super() need to have a call to their superclass’s version of that method.

26
Q

What is pass by reference?

A

pass-by-reference, the function, and the caller both use the exact same variable and object.

27
Q

What is pass by value?

A

pass-by-value, the function receives a copy of the argument objects passed to it by the caller, stored in a new location in memory. The copies of variables and objects in the context of the caller are completely isolated.

28
Q

Python is pass by object reference instead of pass by reference or pass by value? what does that mean?

A

Python is different. As we know, in Python, “Object references are passed by value”. A function receives a reference to (and will access) the same object in memory as used by the caller. However, it does not receive the box that the caller is storing this object in; as in pass-by-value, the function provides its own box and creates a new variable for itself. But the key is that they really are different names and different boxes.

29
Q

Describe how we create a generator expression.

A

A generator expression is similar to a list comprehension except it uses parenthesis ( ) instead of square brackets [ ]

30
Q

What are some of the ways to define a dictionary? Hint 3*

A

1) x = dict((k, v), (k1, v1)..)
2) y = dict(k=v, k1=v1, k2=v2)
3) z = {k:v, k1:v1, k2:v2}

31
Q

Describe ways to access dictionary values? ***HINTS python question, 3 ways.

A

1) dict[‘key’]
2) dict.get(‘key’, default=None)
3) dict.setdefault(‘key’, value) where key is assigned value if it doesn’t exist.

32
Q

describe different ways to set values in a dictionary. HINT 3

A

1) dict[‘key’] = value
2) from the collections module import defaultdict. defaultdict(default_value, initial_dict=None). defaultdict is similar to a dictionary except if a key is accessed before assigned a value, it binds the key to the default_value. the default_value can be a function.
3) dict.setdefault(‘key’, value) where key is assigned value if it doesn’t exist.

33
Q

Describe different ways to iterate over a dictionary in python.

A

1) for key in dict(…)
2) if key in dict(…)
3) for k, v in dict(…).items( )

34
Q

describe the Counter class from the collections module.

A

dict subclass for counting hashable objects. Elements are counted from an iterable or initialized from another mapping.

most_common( ) and elements( ) are the two most widely used methods from this class.

35
Q

Describe the deque object

A

list-like container with fast appends and pops on either end. inserts in regular lists is O(n)

36
Q

What are some useful itertools methods?

A

1) repeat(object, times=[optional]) repeats the object infinite times, simply call next( ) to retrieve the next objects.
2) permutations(iterable, length) - returns a list of permutations.
3) combinations(iterable, length)
4) cycle(iterable)

37
Q

What are some useful functool methods

A

1) reduce(func, iterable) Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
2) cached_property (python 3.8) Transform a method of a class into a property whose value is computed once and then cached as a normal attribute for the life of the instance.
3) lru_cache Decorator to wrap a function with a memoizing callable that saves up to the maxsize most recent calls

38
Q

What is a regular expression?

A

A regex is a special sequence of characters that defines a pattern for complex string-matching functionality.

39
Q

Describe 3 ways you can merge dictionaries in python.

A

1) using the | operator in python 3.9 >
2) using the spread operator **
3) using the built dict method update( )

40
Q

Describe a data class in python

A

A data class is a class that comes with a lot of basic functionality built in. It is declared using the @dataclass decorator from the module dataclasses. For instance, you can instantiate, print, and compare data class instances straight out of the box