1/64
Looks like no tags are added yet.
List-like structures
A data structure that holds multiple individual values gathered together under one variable name, accessed via indices
Index
A number used to access a particular element from a list-like data structure
String
A data structure that holds a list, or a string, of characters
Lists
A data structure that hold multiple individual values gathered together under one variable name, accessed via indices
Dictionaries
A data structure comprised of key-value pairs, where a key is entered into the dictionary to get out a value
Passing by value
a function receives a copy of a variable’s values rather than accessing the original variable itself
python passes basic data types like integers and strings by value
Passing by reference
Giving a function direct access to a variable’s original memory location(its address) rather than making a duplicate copy of its value
python passes more advanced data types like lists by references
Mutable variable
A variable whose value can change after it has been declared
lists
when changed, the new value is stored at the SAME memory address
Immutable variable
A variable whose value cannot change after it has been declared
integers, strings,etc
when changed, the new value is stored at a different memory address
memory address
tells the computer where to find a particular value for a particular variable name
the “id”(function in python), returns the place in memory that the variable points to
Methods
Functions that are contained within data types
Instead of calling directly, it has to be first said what variable is being referred to and then call the method inside that variable.
Unicode
A computing industry standard that sets hexadecimal codes correspond to what characters so that text appears consists across platforms
Hexadecimal
A short-hand expression of the ones and zeroes that comprise computer data, comprised of 16 characters, 0 through 9 and A through F
f string format
name = “Maya”
print(f"Hello, {name}!”)
Expressions in f-string
price = 20
print(f"Total with tax: ${price *1.08}”)
New line
mystringwithnewline = “12345\n67890”
print(mystringwithnewline)
12345
67890
mystring
String declaration
‘“12345”’
“‘12345’”
‘‘‘“‘12345’”’’’
“‘‘‘12345’’’”
string slicing syntax
s[start:stop:step]
omitting components of string syntax
start omitted = s[:4], starts at index 0 and stops at index 4
stop omitted = s[2:], starts at index 2 and stops at the end
step omitted = default step 1(every character
both omitted = returns a full copy of string
negative numbers in string slicing
counts backward from the end of the string where -1 is the last character, -2 is second to last etc.
starts at the letter that u get to when u count backward and stops BEFORE the letter that you count forward to
s = Python
s[-4:-1]
starts at index -4 ‘t’, stops before -1 “n”
step in string slicing
s[::2] grabs every 2nd character of the whole string(when start and step omitted)
s[1::2] grabs every 2nd character from index 1 to end(when stop omitted)
reversing a string using slicing
s[::-1]
goes through whole string(start and stop omitted) and step counts backward by 1
negative step implication
when theres a negative step, start must be larger than stop
out of bound slicing
slicing handles out of range numbers by automating the strings boundaries
.find() method
str.find(“sub”, start, end)
sub = what ur looking for
start = at what index u wanna start looking for sub at
end = at what index u wanna stop looking for sub at
returns -1 when not found
.capitalize()
returns string with first letter capitalized
.lower()
returns all lowercase version of my string
.upper()
returns all uppercase version of my string
.title()
capitalizes any character found immediately after a space
.strip()
strips out any white space before or after the string
.replace(“string”, “string”)
finds all instances of the first argument in the string and replaces it with the second argument
.split()
returns a list of the individual words inside that string
print(“character”.join(myList))
takes each item from that list of strings and concatenates using the string calling the join method as the string to go between each pair of strings being concatenated
Homogeneity
A property of lists determining whether they can accept multiple types of variables.
A homogenous list can only accept one type of variable
a heterogenous list can accept multiple types
Tuples
an immutable form of a list-like structure
myTuple = (1, 2, 3)
set a variable equal to a comma separate series of values or variables
IMMUTABLE
you can put tuples inside if tuples
.sort()
sorts the values of a list for some internal logic like for integers it goes in ascending order
.append()
adds an item to a list
.extend
end current list by another list
.insert(location, value)
location - index of item u wanna replace
value - item u wanna replace it with
.remove(item that u wanna remove)
removes item u want removed
.pop()
removes the last item of a list and also returns the item it removes
del
to delete items by LOCATION in the list instead of by specific item or value
.index(item)
to find the index of a certain item or value
.count(item)
to count how many times a certain item appears in a list
.reverse()
puts list in descending order
stack
“Last in first out” - the last item added(top most item)is the first item removed(only ever able to remove the top most item)
if we have a new item to add to our list the only place we can add it is to the top of the of the stack
We can only access the topmost data
Queue
“First in first out”
we can only access the LEAST recently added item on the list and can only access it by removing it from the list
We queue up our data and the oldest data added is the next data processed
Linked List
A list-like structure where the location of each item in the list is contained in the previous item in the list
data in the memory location of a value that would ITSELF point to another memory location rather than the list next spot pointing to a different memory location
Dictionary
A data structure comprise of key-value pairs where a key is entered into the dictionary to get out a value
ex: myDictionary = { “sprockets”: 5, “widgets”: 11, “cogs”:3, “gizmos”:15}
cannot have multiple copies of the same key
Dictionary key
A value that, when passed into a dictionary, returns a corresponding value like a word and its definition
dictionary is a compilation of multiple key value pairs that u can pass around together
a function can generally only return one value but a dictionary would let us return multiple values from a function
Dictionary preserves the ability to give names to those values by way of keys
Defining new keys in dictionaries
pretend the keys already exist and assign them a new value → variable for dictionary[“key of choice”] = value
del variable for dictionary[“key of choice”] → deletes key and its value from the dictionary
adding a key thats already in the dictionary will just change the value of the key
if u want to check if the key is already in there because u want both values reflected in the list, make a conditional to check. If there is already that key, make a different key for the value u want to add
key error
trying to access a key from a dictionary when it doesnt exist
Traversing dictionaries
.values(): A dictionary method type that returns a list of all the values of that dictionary
.key(): A method of a dictionary type that returns a list of all the keys in that dictionary
.items(): A method of a dictionary type that returns a list of (key, value) tuples in that dictionary
Object
a custom data structure that organizes and encapsulates variables and methods into a single data type
Instance
A single set of values of a particular class. Classes may be comprised of multiple variables; an instance is a set of values for these variables
referred to as class
when function is called inside of a class, its called a method
init and self
called when we first create a new instance of this class
self tells python to define the variables after its dot for the instance, any other code inside this class would be able to see those variables as long as it accessed them with self as well
every method inside class should have self as its first parameter
Encapsulation
The ability to combine variables and methods into class definitions. it helps avoid modification or misuse of data by other functions or programs
Constructor
A common type of method in writing classes that specifies some code to run whenever a new instance of the class is created
Destructor
specifies how the instance of a class is to be destroyed
getter
returns the value of a variable contained within the class
setter
sets a variable contained within the class to a new value
.get() vs []
ex: stock[“pen”] will raise a KeyError if “pen” doesnt exist in the dictionary
stock.get(“box”, 10) will not crash; it returns the value if its in the dictionary even if the key is missing
check default arguments:
b = a for lists
When you assign a list to a new variable using b = a does not make a copy
In-place methods(mutators) dot notation
Most in place methods return None if u write b = a.sort(), b becomes not
Non mutating functions and methods
dont touch your origninal variable, instead they return a brand new object so u need to assign a variable if u want to keep it
sorted(a)
reversed(a)
list(reverest(a)
s.upper()/s.lower
s.replace(old,new)