Midterm Review: Python Problem Sets 3
-
Is there a difference between these two lines of code? Would they result in an runtime error?
x = (a = 5)x = a = 5 -
What is the value of
aafter the second assignment?>>> print(a := 10) >>> a = 20 >>> print(a := a + 5) -
What is the output of this code? Explain the difference between the two approaches. Any errors?
# Approach 1 print(a = 7) # Approach 2 print(a := 7) -
Identify and fix the error in this code. Can you fix this by adding just one more character?
while input_str = input("Enter text: "): print(input_str) -
Explain the difference between the
//operator andintfor division truncation.>>> 2.9 // 1 >>> int(2.9 / 1) >>> -2.9 // 1 >>> int(-2.9 / 1) -
What will be the type and value of each result?
a = 5 / 2 b = 5 // 2 c = int(5 / 2) -
Explain the output of these expressions:
print(bool('False')) print(bool('')) print(bool(0.0)) print(bool([])) -
What will the output be?
print("{} {} {}".format("seven", "eight", "nine")) -
What will the output be?
print("{0} is the first, {1} is the second, and {0} is the first again".format("one", "two")) -
What will the output be?
print("{0}, {val1}, and {1}.".format("the penguin borrowed a ladder", "the kangaroo came by for a smoke", val1="the otter mugged me")) -
What will the output be?
print("{1} {0} {val1}".format("the penguin borrowed a ladder", "the kangaroo came by for a smoke", val1="the otter mugged me"))