1/30
Looks like no tags are added yet.
Lists vs Tuples vs Sets
Lists: ordered mutable data structures
Tuples: ordered immutable data structures
Sets: unordered mutable data structures containing only unique objects
shallow copy vs deep copy
shallow copy: creates a new outer object while nested inner object may still reference the same point in memory as original
deep copy: recursively creates a new outer and inner object
*args and **kwargs
*args: function takes any number of positional arguments which are stored in a tuple
**kwargs: function takes keyword arguments which are stored in a dictionary where the keys are the keys, and the words are the values
Generator
generates a result one at a time using YIELD
yield vs return
yield: ouputs a result and pauses the function
return: outputs a result and terminates the function
instance method vs class method vs static method
instance method: receives self, is a method that can access class and instance attributes
class method: receives cls, method created within a class that is bound to the class, uses @classmethod decorator
static method: method used for utility functionality related to a class, uses @staticmethod decorator
append vs extend
append: adds an item to the end of a list
extend: adds the elements of an iterable to the end of an existing list
remove() vs pop() vs del
remove(): removes first instance of an item from an iterable using the item as an argument
pop(): removes an item from an iterable by index and returns the item, default -1
del: removes list items by index or slice without returning
sort() vs sorted()
sort(): sorts a list in place, changing the original list and returns None
sorted(): creates and returns a new sorted list, not changing original list
break vs continue vs pass
break: breaks out of the current loop
continue: skips the current iteration and moves to the next
pass: placeholder for functions and loops
is vs ==
is: checks if two objects share the same memory address
==: checks if two objects are equivalent to one another
Decorator
used to modify/change a function’s behavior without changing source code.
e.g. logging, caching
SyntaxError vs TypeError vs ValueError
SyntaxError: not following python’s grammatical rules
TypeError: wrong datatype for operation
ValueError: right datatype but invalid value
stack vs queue
stack: data structure that controls how objects are added/remove, uses LIFO
queue: data structure that controls how objects are added/remove, uses FIFO
heap
heaps are tree-based data structures that are used to implement priority queues
minheap: root contains smallest item, every parent is smaller or equal to children
maxheap: root tracks largest item, every parent is larger or equal to children
linear search vs binary search
linear search: iterates through an iterable one at a time — O(n)
binary search: elminates half of the iterable when iterating, iterable must be sorted — O(logn)
recursion vs iteration
recursion: function that repeatedly calls itself until a base case is met
iteration: repetition using a for or while loop for memory efficiency
map() vs filter()
map(): changes elements into iterables
filter(): keeps elements that meet a condition
lambda
anonymous function that automatically returns a value from a single expression