Introduction to Computing Unit 3
Byte
8 bits
Object (instance)
abstract data type created by a developer that can include multiple attributes and methods and may even contain other objects can be passed as an argument in the usual way changing the state of an object is done by making an assignment to one of its attributes Python compares objects based on memory addresses unless dunder comparison methods are created
Method
action that is defined inside a class definition and is invoked on instances of that class self must be used in order to modify the class def method(self, other):
Mode
action that is done after opening a Python file "r" is for read by default (file needs to exist beforehand) "w" is for write (file can either exist beforehand or be made on command) "a" is for append (file can either exist beforehand or be made on command)
Searching algorithm
algorithm designed to check and search for an element in a given data structure
Sorting algorithm
algorithm designed to rearrange the elements stored in a data structure according to a comparison operator between those elements
__str__ method
allows an object to be printed as a string that would otherwise return "<__main__.Name object at memory_address>" without it and can only be called individually to work can only return strings convention is to return attributes or class name used for returning individual data def __str__(self): return
requests module
allows user to reach an APIʼs endpoint run pip install requests on macOS terminal to install requests module run python -m pip install requests on Windows command prompt to install requests module
O(n)
as the input size increases, the time the program takes to run increases at the same rate or linearly
O(log(n))
as the input size increases, the time the program takes to run will increase logarithmically O(log(n)) functions run a number of times approximately equal to log(n) if the number of iterations isn't O(1), but it's significantly less than O(n), it's most likely O(log(n))
__init__ (initializer) method
automatically called whenever a new instance of the class is made by a constructor and is set to their initial state initializes attributes extra parameters can be added to generalize the class constructor as default values parameters that are defined either within the parentheses or as an attribute will stay as that value def __init__(self, other): self.attribute = parameter
Print statement after recursive call for a string
base case is passed first then the statement is printed from right to left
Bit
binary digit that is a single unit of information in a computer typically represented as a 0 or 1
Hexadecimal to binary conversion
break up the hexadecimal into individual digits convert each of these digits to 4 binary bits put the bits together to get the binary number
Recursive call
calling function within a function but with a different parameter
Recursive step
changes the parameter within a recursive call to reach the base case
Closing file
closes a file in Python required for file to be read or have modifications saved not closing a file will result in data corruption always close the file before return statement file.close()
class
compound data type that is a template for the objects that are instances of it when using a defined class, call the class with Name(self,other) class Name:
Hexadecimal to decimal conversion
convert any of the hexadecimal digits to decimal digits write out each place value multiplied the power of 16 and the powers increase from left to right, beginning with 0 add the products up
variable.strip()
copies string where white space is removed from the beginning and end of the string and returns the modified string
Deep copy
copying the contents of an object as well as any embedded objects, and any objects embedded in them
Shallow copy
copying the contents of an object, including any references to embedded objects
file.write(value)
creates a new file if it does not exist already and the default parameter is "r " overwrites previous data if the file already exists and mode is "w" adds onto previous data if the file already exists and mode is "a" value is the string that is written into the file and uses newline character to separate lines convert all non-string data types to string when writing to a file
File
data on non-volatile storage media that is stored in named locations on the media
O(c1 f(n) + c2), where c1 and c2 are constants = O(f(n))
drop all constants, the effect of constants will be negligible as the input size approaches infinity
Shallow equality
equality of references that point to the same object
Deep equality
equality of values that point to objects that have the same value
Text file
file that holds text without any formatting and can be opened in numerous applications and each line separated by a newline character
Decimal to hexadecimal conversion
floor division to divide the number by 16 and then find the remainder and keep dividing until 0 is reached translate the remainders into the corresponding hexadecimals final hexadecimal's order goes from least to greatest
Binary to hexadecimal conversion
group the bits into group of 4, starting with the bits on the right-hand side treat each group as its own binary number and then convert it to decimal convert each individual group to hexadecimal put all the digits together
Constructor
instantiates an object of type Name every class automatically provides a constructor function which is named the same as the class constructor x = Name()
__ne__ method
invoked when the != is used and returns a boolean
__lt__ method
invoked when the < is used and returns a boolean
__le__ method
invoked when the <= is used and returns a boolean
__eq__ method
invoked when the == is used and returns a boolean
__gt__ method
invoked when the > is used and returns a boolean
__ge__ method
invoked when the >= is used and returns a boolean
Iterative sorting algorithm
iterate over the data structure that is meant to be sorted
Dunder (magic) method
method that is invoked internally from the class on a certain action instead of being directly invoked by the programmer
ModuleNotFoundError
module is not installed on the computer
copy module
module that copies a object within a class copy.copy(x)
pprint module
module that prints API data cleanly and legibly pprint(variable)
Converting a loop to recursion
move the function name under a condition that will call itself at with a modified parameter as the recursive call loop is the base case change function is the recursive step
Big-O notation
notation system used to classify algorithms based on time and space complexity or efficiency and focuses on the best, average, or worst case performance of a program only take the portion of the Big O that has the largest degree (power) and bigger powers contribute more when two loops have the same indentation, add the Big-O when two loops are nested, multiply the Big-O all constants are removed keep the worst inefficient term
Decimal number system
numbers based off base 10 x₁₀
Hexadecimal number system
numbers based off base 16 x₁₆
Binary number system
numbers based off base 2 x₂
Open file
opens a file in Python use relative address of the file based on where the current document resides in the computer places cursor back at the beginning of the file file = open("filename","mode")
Endpoint
point of communication that users will make requests to and is part of an URL depending on which endpoint targeted and the type of request made, we will get a specific response or post some change to the API
Recursion
process of calling the function that is currently executing whenever a recursive call is reached, a new function call is made start over from the beginning of the function with a new set of parameters once the program starts returning, go backwards through the recursive calls and continue each one, one by one until those functions return parameter and function should have unique names with no overlapping words
Instantiation
process of creating an object or an instance of a class creates space in memory for the new object and binds a name for the object with the object's data in memory
Status code 200
processing of the request that was sent by the client was successful or ok returned when accessing an URL is successful
Status code 404
processing of the request that was sent by the client was unsuccessful or not found returned when accessing an URL is unsuccessful
O(1)
program always takes the same amount of time, regardless of the input size includes any code that does not repeat itself most efficient
O(n²)
program gets slower as a square of the amount of data elements of a loop within a loop least efficient
Space complexity
program has a better space complexity than another program if it utilizes less memory given the same input deals with how much memory a program utilizes
Time complexity
program has a better time complexity than another program if it takes less time to run (faster) given the same input deals with how long a program takes to run
O(n(log(n)))
program that runs with nested loop that runs logarithmically and linearly
Attribute
properties that make up an instance and shared between all instances modifying a class attribute affects every instance of the class default attributes should be in the parameter of the method default parameters must come last in the parentheses attributes that are defined in the code block are always that value unless changed by another function
Divide and conquer sorting algorithm
recursively break down a set of elements until the set is simple enough to be sorted by itself (reach a base case)
Selection sort
repeatedly select the maximum in a list, and placing it in the correct position until all items have been placed consists of selecting the maximum element of the sublist and swap it with the last element in the sublist since the last element is sorted, the sublist now consists of all the elements except the last one and this sorting algorithm can also be implemented by selecting the minimum and swapping it with the first element in the subarray minimizes swaps, so it would be the best to use if for some reason writing to memory was expensive best, average, and worst: O(n²) each element n is compared n times
self
represent an instance or object of the given class that is automatically set to reference the newly created object and needs to be initialized add self only in method definitions or when referring to the variable within a function, NOT when calling the method outside of the class always bound to new objects and not needed to be put in as parameter can be named anything as it is a naming convention and not a keyword self has to be the first parameter for every method
.json()
retrieves and decodes the JSON object into a Python object that is usually a dictionary or list does not work on html code as it will throw an exception but status code will still be 200
file.readlines(value)
returns a list whose elements are strings of each line including the "\n" continues from the last file reading method unless it has been closed if called again, the program will only return the lines after the first call reading from the cursor to the end of the file value is the number bytes (single characters) to read
requests.get("endpoint url")
returns a request object and takes in the URL as a string of the API that is receiving data and displays a status code when printed
file.readline(value)
returns a string of one line which includes newline at the end when there are no more lines to be read from the file, an empty string is returned continues from the last file reading method unless it has been closed every time this is called, the cursor moves to the beginning of the next line if called again, the program will only return the lines after the first call value is the number bytes (single characters) to read
file.read(value)
returns all the contents of the file as a string including all newline characters continues from the last file reading method unless it has been closed if called again, the program will only return the lines after the first call reading from the cursor to the end of the file value is the number bytes (single characters) to read
Linear search
search all the elements until we find the element we want or when there are no more elements (meaning the element we want is not there) and this method is not efficient best: O(1) element we want is the first element we searched average and worst: O(n) we need to search all elements
Binary search
search list must be sorted beforehand and is done by repeatedly dividing the search interval in half if the element we are searching has a value less than the element at the middle of the interval, narrow the interval to the lower half, otherwise, to the upper half best: O(1) element we want is the first element we searched average and worst: O(log n)
Application programming interface (API)
set of rules and specifications that provide a layer of abstraction between the client and server sides of software allows developers to request information or functionality without having to worry about the implementation or abstraction
Base case (terminating condition)
simplest solution to the recursive problem that is being solved explicitly defined value of the function for a certain value of the parameter or value returned for empty variables
Merge sort
sort simple sublists of numbers and merge them together unsorted lists do not merge until sorted if the list is only one element, return it else, divide the list into two halves until it cannot be divided (reaches the base case) then, merge the smaller lists into a new list in sorted order requires additional space for the merging process best, average, and worst: O(n(log(n)))
variable.split(str)
splits a single string into a list of individual words between spaces and removing all the whitespace between them and returns the list delimiter can be passed in as a string and separates each item within the list
Javascript object notation (JSON)
standardized format for sharing information across different platforms, languages, and APIs
Print statement before recursive call for a string
statement is printed from left to right then base case is passed
Object oriented programming
style of programming that involves representing items, things, and people as objects rather than basing the logic around actions
Bubble sort
swap pairs of numbers until the greatest number bubbles to the top and start with the first and second element, compare and sort them in ascending order compare and sort the second and third element then the third and fourth and so on once the largest number is at the end of the list, first iteration is finished repeat the previous steps until no more swaps are required large values are sorted first and it only takes one iteration to detect that a collection is already sorted best: O(n) when list is already sorted and there are no swaps average and worst: O(n²) each element n is compared n times
Dot notation
syntax for calling a method in another class by specifying the class can be used as part of any expression variable.method_name()
CSV file
text file with a header and one record per line with the field of each piece of data separated by commas and each line separated by a newline character use .split(",") and .strip() when reading a file
API key
unique identifier used to authenticate a user, developer, or calling program to an API always concatenate "?key=" before the API key
O(f(n) + O(g(n)) = O(max(f(n), g(n)))
when multiple terms are being summed, time complexity will be the largest of them
__repr__ method
works very similar to __str__ in that they both print a representation of the object in a user-friendly way but allows programmer to control output of print function used for returning data structures
Decimal to binary conversion
write down what each place value is worth starting from right to left start with the biggest number that is less than or equal to the target number and subtract that number from the target number repeat this process until 0 is reached
Binary to decimal conversion
write out what each place value in a binary number is worth from right to left multiply each place by the value of the bit at that place add the products up