cs exam II

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/231

encourage image

There's no tags or description

Looks like no tags are added yet.

232 Terms

1
New cards

a unique type of data structure that stores information makes it unique from lists and tuples

dictionary

2
New cards

dictionaries are sets of informatin where the

a) order does not matter

b) order does matter

a) order does not matter

3
New cards

in dictionaries:

data is stored in pairs that conist of a ______ and its accompanying

key and value

4
New cards

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

5
New cards

how do you create a dictionary?

dictionaries in python are denoted by curly braces - {}

6
New cards

how are keys connected to their values?

using a colon “:”

7
New cards

how are key-value pairs separated in a dictionary?

separated with commas

8
New cards

like lists, dictionaries are

a) immutable data structures

b) mutable data structures

b) mutable data structures

9
New cards

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

10
New cards

are there any special parameters dictating what you can use for the values of a dictionary?

no

11
New cards

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

12
New cards

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

13
New cards

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

14
New cards

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

15
New cards

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

16
New cards

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

17
New cards

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)

18
New cards

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

19
New cards

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”

20
New cards

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

21
New cards

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)

22
New cards

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])

23
New cards

what does this return?

desserts.keys()

returns an iterable of the dictionary’s keys

24
New cards

what is this code doing?

for key in desserts.keys():

####print(key)

returns an iterable of the dictionary’s keys

25
New cards

what does this return?

desserts.values()

returns an iterable of the dictionary’s valuse

26
New cards

what does this code do?

for value in desserts.values():

####print(value)

returns an iterable of the dictionary’s values

27
New cards

what does this return?

desserts.items()

returns a iterable of the tuples in the format [(key, value)]

28
New cards

what does this do?

for key, value in desserts.items():

#####print(key, value)

returns an iterable of tuples in the format [{key, value)]

29
New cards

true or false:

students sometimes tr to delete key-value pairs while looping through the dictionary

true

30
New cards

what is the best method to delete information while iterating?

makes a separate list and then delete keys in that list

31
New cards

_____ 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

32
New cards

true or false:
occasionally, we also need to add information to another (output) file, rather than returning a value

true

33
New cards

File I/O is all about?

accessing files

obtaining useful information

manipulating it to serve our needs

34
New cards

files must first be _______ before we can access their information

open

35
New cards

how do you open a file in python?

use the built-in open() function for this

example:

open(filename, “r”)

36
New cards

how many parameters does open() take?

two parameters: the file you want to open and the mode to use (a string)


37
New cards

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”)

38
New cards

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

39
New cards

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

40
New cards

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

41
New cards

whenever we open a file, we have to?

close it

42
New cards

how to close a file?

f.close()

43
New cards

in (‘w”) mode, we can add to a file called outfile using?

outfile.write(string)

44
New cards

in (“a”) mode, we also use _________ to add information to a file

.write()

45
New cards

in read mode, ______ returns the full contents of the file as a giant string, “\n” characters included for proper formatting

.read()

46
New cards

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()

47
New cards

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()

48
New cards

the practical use of ________ is when your information contains a header

.read()

.readline()

.readlines()

.readline()

49
New cards

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()

50
New cards

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

51
New cards

a list in python is a type of?

data structure

52
New cards

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

53
New cards

true or false:

with use brackets [] to denote a list, and empty brackets are how we intialize a new one

true

54
New cards

how to make a new list?

myList = []

55
New cards

______ are simply a special type of list—-a list of characters

strings

56
New cards

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

57
New cards

what do strings and lists have in common?

  1. they are ordered. the way that the data is arranged matters, and changing the order changes their overall value

  2. their member can be accessed by an index

  3. length: the len() function can be used on both strings and lists


58
New cards

the len() function can be used on both ______ and ______

strings and lists

59
New cards

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

60
New cards

built-in methods that can be used on list variables, such as:

list.index()

list.count()

61
New cards

while strings contain str type data, lists can?

hold anything whether that is integers, booleans, and even other lists

62
New cards

mutability means directly modifying the values of a list.

with strings, modification means?

reassigning the variable

a_string = “Hello”

a_string = a_string.upper()

63
New cards

when you change information directly in a list, do you have to reassign a varaible?

no;

a_list = [1, “Hello”, True]

a_list.reverse()

64
New cards

what does max(): do?

returns the highest value in the list

65
New cards

what does min(): do?

returns the lowest value in the list

66
New cards

what does sum(): do?

returns the sum of the elements in the list

67
New cards

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 “.”

68
New cards

what are some examples of dot operators in lists?

list.index()

list.remove()

list.count()

list.sort()

list.append()

list.reverse()

69
New cards

when using a list method, remember that you are

a) re-assigning the modified list

b) NOT reassigning the modified list

b

70
New cards

the _____ method adds a specific piece of data to the righthand side of the list

append

71
New cards

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)

72
New cards

the remove method will delete a specified value from a list. use in the format:

list.remove(element)

73
New cards

the count method will return the number of occurences of a value that is searched for. returns ___ if the value is not found

0

74
New cards

the reverse method is equivalent to?

a) string[:-1]

b) string[::-1]

c) string[-1::]

b) string[::-1]

aka also a_list.remove()

75
New cards

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

76
New cards

approaches to organizing abstract data types, such that the data can be accessed efficiently

data structure

77
New cards

a data structure that holds a list, or a string, of characters

string

78
New cards

a data structure that holds multi[ple individual values gathered together under one variable name, accessed via indices. similar to arrays and tuples

lists

79
New cards

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

80
New cards

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

81
New cards

strings are

a) mutable

b) immutable

b) immutable

this is why we have to reassign a variable when we want to change the string

82
New cards

true or false

strings are both a data type and a data structure

true

83
New cards

true or false:
python is not case sensitive—so “a” and “A” are still the samr

FALSE; they are entirely different characters

84
New cards

what does the newline character do (“\n”)

starts a new line within the string

85
New cards

what does the tab character (“\t”) do?

adds a tab indentation within the string

86
New cards

we access specific characters in a string by their index-their numerical position within the data structure. what are used for indexing?

brackets []

87
New cards

python is a zero-index language, meaning that the first element in a string has an index of?

0 not 1

88
New cards

python supports negative indexing, which begins at?

[-1] with the last character in the string and continues down the negative number line

89
New cards

to slice and retrieve substings, we use brackets [] in a very similar way to the?

range() function

90
New cards

in python, we can use the ___ operator to determine whether or not a specified substring is contained with a string

“in”

91
New cards

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

92
New cards

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

93
New cards

the _____ method has built-in parameters, which are the boundaries in which to search for a specific occurence

find

94
New cards

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

95
New cards

the _____ method is useful for combining string elements into a final string, using a specified character to join the elements together

.join()

96
New cards

.lower()

returns a string with all lowercase letters

97
New cards

.upper()

returns a string with all uppercase letters

98
New cards

.index()

returns the position of a specified character and errors if the character is not found

99
New cards

.replcae()

replaces a specified substring with another substring

100
New cards

.strip()

removes occurneces of a specified character from the beginning and end of a string