return a
return not a
return a or not b
return a and not b
>>> x = 0
>>> 3 / x
Runtime error
>>> x != 0 and 3 / (1-1) > 3
False
>>> def f(a, b):
return a and b
>>> f(x != 0, 3 / (1-1) > 3)
Runtime error
>>> 3 < 5 or 5 => 3
Syntax error
>>> x = 1
>>> 'x\'\"y'
'x'\"y'
>>> print "x\\y\'z"
x\y'z
def xor(a, b):
return (a or b) and not (a and b)
def is_even(x):
return x % 2 == 0
def min3(x, y, z):
"Returns the minimum of x, y, and z."
# Use the built-in min function, but only with two arguments
return min(x, min(y, z))
def min3(x, y, z):
"Returns the minimum of x, y, and z."
# Use the built-in min function, but only with two arguments
if x < y:
return min(x, z)
else:
return min(y, z)
def min3(x, y, z):
"Returns the minimum of x, y, and z."
# Do not use any built-in functions
if x < y:
if x < z:
return x
else:
return z
elif y < z:
return y
else:
return z
def message(score, goal):
if score < 0.9 * goal:
print "You loose"
elif score < goal:
print "Close, but not prize"
elif score < goal + 100:
print "You win"
else:
print "You're awesome!"
A Python variable that is only assigned once (when it is declared) is
a useless variable, because it does not change
called a constant
always given an all-caps name
none of the above
A function that both prints information and returns a value is
impossible in Python
possible but generally bad style
possible and good style
possible only if a boolean value is returned
Which of the following are Python comparison operator?
=<
~=
!=
Which of the following expressions are true (have a true value)?
1
-1
0
None
""
False
'False'
1 == 2 or 1 == 3
1 == 2 or 3
not True and False or 0
3 < 5 or 5 >= 3
not not False and False
3 < 4 and 4 < 5
not (1 or 2 < x and False)
1 <= 3 or 1/0 < 3
is equivalent to the following in the truth of the value returned:
if a: return True else: return False
is equivalent to the following in the truth of the value returned:
if a: return False else: return True
is equivalent to the following in the truth of the value returned:
if a: return True elif b: return False else: return True
is equivalent to the following in the truth of the value returned:
if a:
if b:
return False
else:
return True
else:
return False
Replace each ellipsis (...) in the following transcript with the value that would be printed at that point. If an error message would be printed at a given point, just indicate if it is a runtime or syntax error.
Write a function named xor that takes two truth values and returns a value that is true only when one, but not both, of its arguments are true. For full credit, your solution should be as simple as possible, making good use of the boolean operators. (Note that the arguments are not necessarily boolean values, so it does not work to test them for equality.)
Write a function named is_even that takes an integer and returns a boolean value indicating if the integer is an even number (that is, if the remainder is zero when it is divided by two).
The following problems involve playing the "replace the dots" game described on the Practice and Review problems index page.
Write a function named message that takes two numbers, a score and a goal. It prints the message "You lose" when the score is less than 90% of the goal, the message "Close, but no prize" when the score is less than, but within 10% of, the goal, the message "You win" if the score is over the goal, but by less than 100 points, and the message "You're awesome!" when the score is 100 points or more over the goal. Avoid using unnecessarily complicated logic in your solution.