1/231
Looks like no tags are added yet.
a unique type of data structure that stores information makes it unique from lists and tuples
dictionary
dictionaries are sets of informatin where the
a) order does not matter
b) order does matter
a) order does not matter
in dictionaries:
data is stored in pairs that conist of a ______ and its accompanying
key and value
instead of using an index such as [0] to access information like in a list, we can access specific values in dictionaries how?
by using a key while traversing through them
how do you create a dictionary?
dictionaries in python are denoted by curly braces - {}
how are keys connected to their values?
using a colon “:”
how are key-value pairs separated in a dictionary?
separated with commas
like lists, dictionaries are
a) immutable data structures
b) mutable data structures
b) mutable data structures
what does a mutable data structure mean?
means that the information can be directly modified
-for example, if you wanted to change the value associated with the key “Cookie,” yo could change it simply by accessing it through the key
are there any special parameters dictating what you can use for the values of a dictionary?
no
are there any special parameters dictating what you can use for keys of a dictionary?
yes; keys can only be immutable data types: strings, integers, booleans, tuples, floats
true or false:
just like a word in a dictionary can have multiple definitions that are different parts of speech, keys can have values that are different data types
true
this is an example of what with a dictionary:
desserts = {“Cookies”: 10, “Cakes”:5, “Ice Cream”: 3}
desserts[“Candy”] = 25
a) adding a new pair
b) replacing the value of an existing pair
c) deleting an existing pair
a) adding a new pair
this is an example of what with a dictionary:
desserts = {“Cookies”: 10, “Cakes”:5, “Ice Cream”: 3}
desserts[“Cakes”] = 4
a) adding a new pair
b) replacing the value of an existing pair
c) deleting an existing pair
b) replacing the value of an existing pair
this is an example of what with a dictionary:
desserts = {“Cookies”: 10, “Cakes”:5, “Ice Cream”: 3}
del desserts[“Ice Cream”]
a) adding a new pair
b) replacing the value of an existing pair
c) deleting an existing pair
c) deleting an existing pair
the way you change a dictionary’s values
a) does not depend on the data type of that value
b) depends on the data type of that value
b) depends on the data type of that value
desserts = {“Cookies”: [10], “Cakes”: 5, “Ice Cream”: “Hello”, “Candy”:(25)}
if i wanted to add 12 to the “Cookies” value list, how would i do that?
desserts[“Cookies”].append(12)
desserts = {“Cookies”: [10], “Cakes”: 5, “Ice Cream”: “Hello”, “Candy”:(25)}
if i wanted to add 5 to the value of cakes, how would i do that?
desserts[“Cakes”] += 5
desserts = {“Cookies”: [10], “Cakes”: 5, “Ice Cream”: “Hello”, “Candy”:(25)}
If i wanted to add the word ‘world’ to the end of Ice Cream’s value, how would i do that?
desserts[“Ice Cream"] += “World”
desserts = {“Cookies”: [10], “Cakes”: 5, “Ice Cream”: “Hello”, “Candy”:(25)}
what if i wanted to add three to the value of candy?
desserts[“Candy”] += 3
how would i iterate through this dictionary? if i wanted to print out the keys?
desserts = {“Cookies”: 10, “Cakes”: 5, “Ice Cream”:3 }
for key in desserts:
####print(key)
how would i iterate through this dictionary? if i wanted to print out the values?
desserts = {“Cookies”: 10, “Cakes”: 5, “Ice Cream”:3 }
for key in desserts:
#####print(desserts[key])
what does this return?
desserts.keys()
returns an iterable of the dictionary’s keys
what is this code doing?
for key in desserts.keys():
####print(key)
returns an iterable of the dictionary’s keys
what does this return?
desserts.values()
returns an iterable of the dictionary’s valuse
what does this code do?
for value in desserts.values():
####print(value)
returns an iterable of the dictionary’s values
what does this return?
desserts.items()
returns a iterable of the tuples in the format [(key, value)]
what does this do?
for key, value in desserts.items():
#####print(key, value)
returns an iterable of tuples in the format [{key, value)]
true or false:
students sometimes tr to delete key-value pairs while looping through the dictionary
true
what is the best method to delete information while iterating?
makes a separate list and then delete keys in that list
_____ are simply a way to store data directly to a computer’s hard drive. In this class, all you need to understand is that files contain information separately from our program, so we use special code to retrieve that information
files
true or false:
occasionally, we also need to add information to another (output) file, rather than returning a value
true
File I/O is all about?
accessing files
obtaining useful information
manipulating it to serve our needs
files must first be _______ before we can access their information
open
how do you open a file in python?
use the built-in open() function for this
example:
open(filename, “r”)
how many parameters does open() take?
two parameters: the file you want to open and the mode to use (a string)
true or false:
you do not always need to assign the open function to a varaible
false:
this is because open() returns a file object for us to manipulate (no object knowledge needed for this). we want to store that object so we can use it
f = open(filename, “r”)
we use this when we want to obtain the information we don’t care about using the actual file in any way
a) “r” mode
b) “a” mode
c) “w” mode
a) “r” mode
we use this mode when we want the information in the file, and we plan to add onto the same file
a) “r” mode
b) “a” mode
c) “w” mode
b) “a” mode
we mostly use this mode when we open an output file to add information to. if we open (“?”) on a file that isn’t blank, Python overrides the data with our new data
a) “r” mode
b) “a” mode
c) “w” mode
c) “w” mode
whenever we open a file, we have to?
close it
how to close a file?
f.close()
in (‘w”) mode, we can add to a file called outfile using?
outfile.write(string)
in (“a”) mode, we also use _________ to add information to a file
.write()
in read mode, ______ returns the full contents of the file as a giant string, “\n” characters included for proper formatting
.read()
what is a note for when you open a file and read its information?
store the read information in a separate variable:
data = f.read()
in read mode, _______ returns the first line in the formatted string. each consecutive time it is used, it returns the next line down (has a memory).
.readline()
the practical use of ________ is when your information contains a header
.read()
.readline()
.readlines()
.readline()
this method is by far the most useful, and returns a list, where each element is a separate line of the file (as a string). we like .readlines because we can use a for-loop to iterate over each line of the file
a) .read()
b) .readline()
c) .readlines()
c) .readlines()
how to iterate over a file using .readlines():
def file_reader(filename):
###f = open(filename, “r”)
###data = f.readlines()
###f.close()
###for line in data:
######do something
a list in python is a type of?
data structure
which data structure is this describing?
this mean its purpose is simply to to store information in a way that is useful to us
list
true or false:
with use brackets [] to denote a list, and empty brackets are how we intialize a new one
true
how to make a new list?
myList = []
______ are simply a special type of list—-a list of characters
strings
true or false:
strings are a container for data, but the data in a string can only be strings of length one or substrings (basically, information of the str type)
true
what do strings and lists have in common?
they are ordered. the way that the data is arranged matters, and changing the order changes their overall value
their member can be accessed by an index
length: the len() function can be used on both strings and lists
the len() function can be used on both ______ and ______
strings and lists
are strings iterable objects?
yes like lists they are—basically meaning that they can be looped over
-also because of the len() function, we can use the range for-loop on lists as well
built-in methods that can be used on list variables, such as:
list.index()
list.count()
while strings contain str type data, lists can?
hold anything whether that is integers, booleans, and even other lists
mutability means directly modifying the values of a list.
with strings, modification means?
reassigning the variable
a_string = “Hello”
a_string = a_string.upper()
when you change information directly in a list, do you have to reassign a varaible?
no;
a_list = [1, “Hello”, True]
a_list.reverse()
what does max(): do?
returns the highest value in the list
what does min(): do?
returns the lowest value in the list
what does sum(): do?
returns the sum of the elements in the list
as seen with strings, lists have an assortment of built-in functions that are used for manipulation. they are declared by using the _____ operator
the dot operator “.”
what are some examples of dot operators in lists?
list.index()
list.remove()
list.count()
list.sort()
list.append()
list.reverse()
when using a list method, remember that you are
a) re-assigning the modified list
b) NOT reassigning the modified list
b
the _____ method adds a specific piece of data to the righthand side of the list
append
to append many elements in a row in a list, such as the characters of a string, use a?
for-each loop"
for letter in string:
###a_list.append(letter)
the remove method will delete a specified value from a list. use in the format:
list.remove(element)
the count method will return the number of occurences of a value that is searched for. returns ___ if the value is not found
0
the reverse method is equivalent to?
a) string[:-1]
b) string[::-1]
c) string[-1::]
b) string[::-1]
aka also a_list.remove()
the sort method will iterate over the elements in a list and sort them in a _______ order, both for numbers and strings
a) ascending
b) descending
a) ascending
approaches to organizing abstract data types, such that the data can be accessed efficiently
data structure
a data structure that holds a list, or a string, of characters
string
a data structure that holds multi[ple individual values gathered together under one variable name, accessed via indices. similar to arrays and tuples
lists
the complementary processes of saving data to a file and loading data from a file, generally such that the state of the memory of the program is the same after saving and loading have occurred
file input and output
a data structure comprised of key-value pairs, where a key is entered into the dictionary to get out a value. similar to or synonymous with Maps, Associative Arrays, HashMaps, and Hashtables
dictionaries
strings are
a) mutable
b) immutable
b) immutable
this is why we have to reassign a variable when we want to change the string
true or false
strings are both a data type and a data structure
true
true or false:
python is not case sensitive—so “a” and “A” are still the samr
FALSE; they are entirely different characters
what does the newline character do (“\n”)
starts a new line within the string
what does the tab character (“\t”) do?
adds a tab indentation within the string
we access specific characters in a string by their index-their numerical position within the data structure. what are used for indexing?
brackets []
python is a zero-index language, meaning that the first element in a string has an index of?
0 not 1
python supports negative indexing, which begins at?
[-1] with the last character in the string and continues down the negative number line
to slice and retrieve substings, we use brackets [] in a very similar way to the?
range() function
in python, we can use the ___ operator to determine whether or not a specified substring is contained with a string
“in”
because strings are immutable data structures, using methods on them
a) won’t change their actual value
b) will change their actual value
a) won’t change their actual value
the (.find()) find method returns the _____ index of a specified character or substring (if present in the string). will return -1 if no occurence was found
a) first
b) second
c) all
a) first
the _____ method has built-in parameters, which are the boundaries in which to search for a specific occurence
find
the _____ method takes a string and divides the content into separate elements of a list, depending on a given character to split by (space by default)
split
the _____ method is useful for combining string elements into a final string, using a specified character to join the elements together
.join()
.lower()
returns a string with all lowercase letters
.upper()
returns a string with all uppercase letters
.index()
returns the position of a specified character and errors if the character is not found
.replcae()
replaces a specified substring with another substring
.strip()
removes occurneces of a specified character from the beginning and end of a string