Untitled Deck Flashcards
snake_case is used for
variables and functions
core types
can be checked with method type()
extend()
can concatenate two lists;
list1.extend(list2)
identity operators
check for types;
is / is not
dictionaries
unordered and mutable
tuples
ordered but immutable, allowing duplicates. Should be used for values that will not change.
count()
count occurrences of something;
item.count(x)
index()
returns position of the first instance of that value;
item.index(x)
floor division
rounds down if the result is a decimal
list comprehensions
listname = [(process) for x in range(y)];
outputs the result of the process over the range
list slicing
list[starting index, ending index + 1];
excludes last number
dictionary access / assignment
my_dict[‘key’] = value
continue
continue skips the current iteration and moves to the next if condition is/n’t met
break
exits the loop if the condition is/n’t met with no more iterations
class
a definition for a variable to act a certain way and comply with new functions
object
an instance of a class, following the rules of that class with unique data;
object = myClass(a1, a2…)
__init__
a constructor; automatically called when an object of a class is created.
initializes (confirms + stores) the unique attributes
inheritance
a subclass inherits properties and methods from an existing class; promotes code reuse;
class Animal: …
class Dog(Animal): …
both have function speak(self)
super()
within a subclass, super() calls methods from the superclass without adding them to the subclass.
often used in the __init__ method for the subclass to initialize attributes inherited from the superclass
encapsulation
the bundling of attributes and methods into a single unit and restricting direct access to come of the object’s components;
coupling attributes into a class to protect the integrity of the data
polymorphism
the ability of an object to take on many forms;
methods can be overwritten in subclasses with super().func()
refactor
third step of TDD: clean up code after ensuring the tests pass
atomic operations
performed as a single, uninterruptible step;
arithmetic, variable assignment, branching statements.jumps,
all dictionary operations are
of O(1) time complexity
adding and removing items from sets
O(1)
list indexing, adding to and popping from lists are
O(1) time complexity
list operations at specific indexes
O(n-i) time complexity
searching for x in a list
O(n) time complexity
list splicing
O(b - a)
adding lists
O(n1 + n2)
sorting a list
O(n log n)
order of time complexities:
O(1), O(log n), O(n), O(n log n), O(n^2), O(n^x), O(2^n), O(n!)
methods for reducing time complexity
eliminate unnecessary repeated work, use sets / dictionaries for faster lookups, remove slow parts of the algorithm
a loop runs while n > 1, and n // 2 after every iteration
O(log n)
nested loops iterating over the same variable
O(n^x)
nested loops iterating over different variables
O(n * m)
max and min
O(n), if unsorted
push(item)
add to top of stack
pop()
removes from top of stack
peek()
views next removable item without removing
enqueue(item)
add to back of queue
dequeue()
removes from front of queue
implementing stack
list wrapper;
python list, append + pop
implementing queue
track front with head index; dequeue increments head; cleanup when head > len/2
.capitalize()
capitalizes first letter of a string
if a subclass has new variables
init separately under def__init__;
inherit other variables with super().__init__(var)
if a subclass has a similar function
inherit base functionality with super.function(), adding extra conditionals before or after to match
unittest formula:
import unittest
class TestClassName(unittest.TestCase):
def test_func(p1, p2…):
# define values + run process
self.assertEqual(expected result of process)
unittest.main()
__str__
returns a readable string representation of an object;
.join()
joins iterables by a separator:
“separator”.join(obj)