MIT 6.00 Week 2 Flashcards
What guarantees that a loop will end?
A decrementing function
What characteristics are required of a decrementing function?
1) It maps a set of program variables to an integer.
2) It starts with a non-negative value.
3) It terminates when the value < 0.
4) It decreases each iteration.
What is “brute force” problem solving?
Exhaustive enumeration
Determining the range of values that an answer could be, and checking each value individually, in order, until you find the correct answer.
What is the syntax of a for loop in Python?
for variable in (something):
what is the syntax for the range function?
range(start, stop, increment)
start is inclusive
stop is non inclusive
What do we do when an exact answer cannot be ensured?
Use approximation.
Determine what answer will be close enough that we are satisfied, and search for an answer within that range.
What is a faster type of search than exhaustive enumeration, and how does it work?
Bisection Search:
1) Determine a range within the answer can be found
2) Test the middle of the range. If it is too high, make it the new upper bound. If it is too low, make it the new lower bound.
3) Test the answer that is the middle of the new range.
4) Repeat until the answer, or an acceptable approximation, is found.
What advantages does the use of functions provide?
1) Decomposition
2) Abstraction
What do we mean by decomposition?
Structure is created. The modules created by functions are self-contained and reusable.
What doe we mean by abstraction?
Details are suppressed when they are unimportant or distracting.
How do we create a function?
def functionName(parameter, parameter,...): Function Body
How do we get a value back from a function?
the return keyword
What happens if you do not use return in a function?
It will return the none data type
What is the term for calling on a function?
Invoking
What is the written description and explanation of a function called?
The specification
What should be included in specification?
The purpose/effect of the function, and the parameters it takes.
What is the term for the parameter found in the definition of a function?
The formal parameter
or
parameter
What is the term for a value passed into a function?
The actual parameter
or
argument
What is scope?
The matching of names to objects.
What keyword will check for a true/false value, and terminate the program is the value is false?
assert
Planning for bad inputs is:
Defensive programming
When are scopes created?
When a function is run, not when it is defined.
Another term for scope is:
A stack frame
The list of scopes and their relative distance to the main scope:
The stack
When something is removed from the stack, we:
pop the stack
What is the relationship of variables in one scope to the variables of another scope?
They are discreet.
This is more complicated than it is currently being presented.
What happens if a string is used in a for loop?
The variable will be assigned each character in the string, in order, and run through the loop.
How do you find a character in a certain position within a string?
s[#:#]
where “s” is the name of the variable the string is in, the first # is the position of the character you want to start with, and the second # is the end character.
The first number is inclusive, starting at 0. The second number is exclusive.
How do you find a certain character in a string?
variableName.find(‘a’)
‘a’ is the character you are looking for. The result is the position of the character in the string, starting with 0.
How do you find a list of everything you can do with a data type, without descriptions?
dir(data_type)
How do you find a definition of a keyword from within IDLE?
help(data_type.keyword)
how do you find a list of everything you can do with a data type, with descriptions?
help(data_type)
Define a tuple:
a non-scalar data type that can hold many things
what is the literal syntax of a tuple?
(element, element)
How can the individual parts of a tuple be accessed?
By using indices
what are two ways to print an individual element of a tuple?
print tuple_name[x]
Where x is the index position you want to print, starting with 0
print tuple_name[-x]
Where -x is the index position you want to print, reading from right to left, starting with -1 as the last element in the tuple.
What kind of data structure only houses one kind of data type?
A homogenous data type
What kind of data structure houses multiple different data types?
A heterogenous data structure.
Tuples can hold tuples
tuple_name1(tuple_name2(x, y), z)
What happens if you print the tuple itself?
You will print a literal representation of the tuple, as you see it.
if the tuple is (1, 2, ‘a’), the program will print:
(1, 2, ‘a’)
A data type whose elements cannot be changed is an:
immutable data type
Are tuples an immutable data type?
yes
What are the defaults when you leave blanks while choosing sections of an index? i.e. [:#] or [#:]
The default start is 0
the default end is the last element of the index
What happens when you add a tuple to a tuple?
The elements of the second tuple are added, in order, to the end of the first tuple.
What is a common mistake when writing a tuple of one element?
Forgetting the ‘,’
For example (‘a’,) is a tuple, whereas (‘a’) is not. The second is simply regarded as more important for order of operations.
What keyword kicks you out of a loop?
break
What is a function, roughly?
A named, pre-written piece of code that takes inputs and gives outputs.
What is another term for the specification?
The doc string
What is another term for invoking a function?
Calling the function