What of vanila python datatypes are sequences?
String, List, Tupel, Range
Stack vs. Queue, use cases
FIFO, LIFO
stack: undo&redo functions, browser history(when navigating back, last visited page is accessed first)
queue: message queues, network data packets
what is a deque?
Double Ended Queue
allows efficient append and pop operations at the ends of the container
O(1) vs List O(n)
Which operations does a deque perform less efficiently than a regular list?
Accessing elements, cos List uses elements whilst deque has to use next method multiple times
What are iterables and iterators in python?
How do we create them?
Is an iterable an iterator and vica vera?
Iterable: Object, that is capable of returning its elements one by one and implements __iter__ method
Iterator: Object, that represents a stream of data and implements an __iter__ and __next__ method
create them by creating a class and defining dunder methods mentioned above
Iterator -> Iterable but Iterable -/-> Iterator
What can be used as a context manager and why do we use them?
A class with __enter__ and __exit__ methods can be used as a context manager
To establish a connection to something (such as database or file) and want to be sure that this connection get closed properly in all cases
Magic method? When to use them?
methods, that start and end with a double underscore (also known as dunder)
Implementing such methods allows us to use standard python functions and operators with our custom classes
Use when you want to define the behaviour of objects in response to certain actions, such as when to define the string representation of an object (e.g. __str__ -> print())
How can you mark a function as private?
You can’t, there’s no such thing as fully private function or variable in python
double underscore at the start helps a bit (__method() prevents accidental overriding)
what does the zip function do?
combines multiple iterables (list, tuple ,..) into tuples
zipped = zip([1,2,3], [a,b,c]) => ((1, a), (2,b), (3,c))
what does functool.wraps do?
helps us keep original names and docstrings of wrapped functions
What are generator expressions in python?
how are they different from list comprehensions?
Expressions that return a generator object, contains a yield keyword
difference: uses lazy evaluation
what are closures and why do they exist?
A function that remembers how it was when the function was defined
e.g.
closure = function(“Hello world”) # would print Hello world
closure() # prints Hello world
Allows to hide/protect data
what is a dataclass and what are its advantages?
dataclass/class/named tuples - pros and cons
dataclass: A dataclass is a decorator(@dataclass) that automatically generates special methods such as __init__ and __repr__
pros: less boilerplate code
cons: mutable by default
class:
pros: full control
cons: more boilerplate
named tuples: (e.g. namedtuple(“Person”, [“name”, “age”, “city”]))
pros: lightweight and fast
cons: less functionality
What is a strongly typed and dynamic language?
What does it mean?
strongly typed language doesn’t allow implicit type conversions.
Dynamic language means type checking happens at runtime.
Like python
Naming schemes of variables, functions, and classes in python.
variables and functions: lowercase and underscore
classes: CamelCase
3 built in datatypes in python
int, float, str
What is a module in python?
a module is a python file containing code that can be imported
Where does python look for modules/packages?
system path, python installation directory, working directory
what does finally clause mean in context of exception?
The finally block will always be executed even if there’s an error or return
This can be useful to close objects and clean up resources
Can you define a function like this:
def my_pow(x=10, power)
pass
No, the default values in parameters have to appear after the non-default values
What is the difference between keyword arguments and positional arguments?
keyword arguments(city=”unknown”) allow us to pass in any order when calling a function
positional arguments first, then keyword arguments (e.g. function(name, age, city=”unknown” => function(“Alice”, city=”Munich”, 25) doesn’t work)
How do you create a function that accepts arbitrary numbers of arguments?
def average(*args)
value of args is treated as a tuple with positional arguments
does not have to be args necessarily
How can you unpack collections and pass their values as arguments to a function?
def my_date(year, month, day):
my_values = [2021, 12, 31]
my_date(*my_values)
How do you sort the values of a list by their third character?
strings = sorted(strings, key = lambda x: x[2])
difference between append and extend for list?
append: add an element
extend: add every elements of another list
What does x, y, z do when accessing a list [x:y:z]. Give all eight possibilities and explain what they do.
[x:y:z] x: start index, y: end index, z: step(how many to skip)
[x:y]: every element from x to y (excluding y)
[:y:z]: starts from the beginning
[x::z] til the end with the step z
[x:] til the end
[:y] til excluding y
[x] x-th index
[::z] take every element with step z
Briefly explain the function of namedtuple, counter, and OrderedDict
- namedtuple: to group variables together
- counter: to count collections items
- OrderedDict: remembers the insertion order
How can you specify the type of the variable at the same time as the value?
variable: int = 3
How can you specify the return data type of a function?
def function() -> int:
what are decorators?
wrapper functions (called with @ before definining function)
compare list, tuple, set, dict with 2 characteristics
list: mutable, ordered
tuple: heterogeneous, immutable
set: no duplicates, unordered
dict: unordered, key-value pairs
list, tuple are sequences
2 different ways to format strings
.format()
fstrings
Last changed2 months ago