algo Flashcards
python: To declare a variable without assigning it a value, type
my_variable = None
algo: For a linked list, the main 2 attributes you need are
data and next
python: To make the most basic class, type
class MyClass: pass
python: Every file that you create is a
library
python: To import one specific class from an external file, type
from library_name import ClassName
python: To turn two lists into a dictionary, type
dict(zip(list1, list2))
python: To slice the last 8 characters from a string, type
my_string[-8:]
python: To round a number to the nearest whole number, type
round(1.3)
python: To make a one line if else statement, type
“true value” if 10==10 else “false_value”
python: To make a node of a linked list that takes in its data upon instantiation, type
class Node: data = None next = None def \_\_init\_\_(self, data): self.data = data
python: To call a class method on the class itself, type
ClassName.method_name(class_instance)
python: To import a class called MyClass from a file called my_file.py in the current directory, type
from my_file import MyClass
python: All class methods must have a
self argument
python: To call a class variable from inside a class method, type
self.variable_name
python: To call a class method from inside another class method, type
self.method_name()
python: To set a default value for a function, type
def my_function(param1, param2 = True): return param1
note: Params with default value must all come at the end
python: To pass in unlimited named parameters into a function, type
def my_function(**kwargs): return ...
my_function(param1 = “string”, param2 = “string”):
Note: All the params will be accessible in a dict called kwargs.
Note: The **kwargs arguments must come at the end.
python: To create a class where the __init__ function sets all the keyword arguments as attributes, type
class MyClass:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
algo: Usually, your algorithm with use a
while True loop instead of a for loop, will use break, pass for logic inside. Afterwards try to replace while True: with like while pointer > 4:
python: Attributes in a class that are declared as attribute_name = “string” but not explicitly set using self.attribute_name in the __init__ method or any other method are
universal and all the instances can at once have their attribute values changed using MyClass.attribute_name = “string”
python: To inherit a class, type
class MyClass(ParentClass): pass
algo: For efficiency, avoid
nested loops
algo: The purpose of big 0 notation is
to assess how much time an algorithm will take to run and how much space it will take in memory, as the input gets arbitrarily larger
algo: The main big 0 notations are
0(n) = Linear - one loop 0(1) = Static - no loops 0(n^2) = Quadratic - One nested loop