Answers
What is one similarity between print() and input()? - answer * Both generate a string
output
* Both are built-in or standard library functions
When ever the Python interpreter needs to evaluate a name (of a variable, function, ...),
it searched for the name in order:
1)
2)
3)
4) - answer 1) local
2) Global
3)Module Import
4 ) Built in
Explain the difference between 'import math' and 'from math import sqrt' or 'from math
import*' - answer 'Import math' and 'From math import*' import all the
functions/objects of the module while 'from module import sqrt' imports the specific
function/object. "From math import*' and 'From math import sqrt' allow the object to be
referred to without including the module from which it was imported, which might lead to
pollution of the name space.
What is the difference between print() and return? - answer * print(): prints the
specified message to the screen
* return : is used to end the execution of the function call and "returns" the result (value
of the expression following the return keyword) to the caller
* print example
*return example
Which operator has the lowest precedence? Why?
count %= count - offset + 2 - answer answer: (%=)
explanation: Since the modulo operator is chained with the assignment operator(which
has the lowest precedence), the %= will execute last.
Do the keys in a dictionary best model a list or a set? - answer + 5 pts [List or Set]
Set
+ 5 pts [Uniqueness] Keys must be unique
+ 5 pts [Order] Keys and sets are unordered.
+ 5 pts [Mutability] Keys are immutable, and
elements of a set have to be immutable
, Give two examples of functionality/applications that you can do with a dictionary you
can't do with a list. - answer + 2.5 pts Relationships between keys and values
+ 2.5 pts Addressing values by key rather than index
Given a value for a dictionary, is there a built-in way to lookup the corresponding key?
Explain your answer. - answer + 4 pts There is NOT a built-in way.
+ 4 pts Keys are unique while values are not.
+ 4 pts Many keys might have the same associated value.
Why does python provide so many different ways to structure & store data? What would
happen if Python didn't provide data structure? - answer +4 pts Answered first
question: many ways to associate/store data
+4 pts Programs would have an unmanageable number of variables
Give two examples of functionality/applications that you can do with a dictionary you
can't do with a list. - answer + 2.5 pts Relationships between keys and values
+ 2.5 pts Addressing values by key rather than index
+ 2.5 pts Keys are unique
+ 2.5 pts Dictionaries are unordered
When can you change the field of a namedtuple and not change the original object's id?
Write a program to demonstrate. - answer + 4 pts Answer: when the field being
changed is a mutable object
+ 4 pts Example: nt = namedtuple('nt','immutable mutable')
e = nt('s',[0])
e.mutable.append(1)
Why is it good advise to use parentheses with Boolean expressions? - answer
*overrides operator precedence
* for readability
*you can use an example with the "and" and "or" operators to increase the precedence
of the or operator
What is the difference between a function that returns None and a value returning
method? - answer *a value returning method has a return statement, a function that
returns None doesn't exist
*result of a value returning method can be used in expressions(more commonly)
Arguement - answer the value that is sent to the function when its called
Parameter - answer a variable listed inside the parenthesis in a function's definition
Why is it good advice to use parentheses with Boolean expressions? Give an example
where it is necessary. - answer + 4 pts override operator precedence
+ 4 pts For readability of a very long and unreadable boolean expression