Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 2: Expressions
59 problems · 0 figuresConcepts in Practice 2.1.1 §2.1 source ↗
- a.high-level
- b.low-level
Answer
Concepts in Practice 2.1.2 §2.1 source ↗
- a.
print(1 + 1) - b.
1 + 1 - c.
1
Answer
1 1 .Concepts in Practice 2.1.3 §2.1 source ↗
name = input()?- a.the name that was input
- b.nothing (except for >>>)
Answer
Concepts in Practice 2.1.4 §2.1 source ↗
- a.only the up arrow key
- b.the up and down arrows
- c.the up and left arrows
Answer
Concepts in Practice 2.1.5 §2.1 source ↗
- a.press the up arrow twice
- b.press the down arrow twice
- c.press the left arrow twice
Answer
Concepts in Practice 2.2.1 §2.2 source ↗
Consider the example above.
What isbook_rating's data type on line 7?- a.float
- b.integer
Answer
book_rating was assigned with 3.5 on line 5 and converted from an integer to a float by the interpreter. book_rating isn't modified after line 5.Concepts in Practice 2.2.2 §2.2 source ↗
Consider the example above.
What wouldbook_rating's data type be if update = 1.0 instead of 0.5?- a.float
- b.integer
Answer
book_rating would be assigned with 4.0 .Concepts in Practice 2.2.3 §2.2 source ↗
Consider the example above.
What is the data type ofx after the following code executes? x = 42.0 x = x * 1
- a.float
- b.integer
Answer
x is a float, 42.0 , after the code executes.Concepts in Practice 2.2.4 §2.2 source ↗
Consider the example above.
Which function converts the input number of slices to a data type that can be used in the calculation?- a.
float() - b.
input() - c.
int()
Answer
float()float() converts the input number of slices from a string to a float for the pizza order calculation.Concepts in Practice 2.2.5 §2.2 source ↗
Consider the example above.
How could line 3 be changed to improve the program overall?- a.Use
float()instead ofint(). - b.Add
1to the result ofint(). - c.Add
str()aroundint().
Answer
1 to the result of int().int() removes any fractional part. Many calculations will produce a fractional part. Ex: 46 people with 3.0 slices per person will need 138 slices or 17.25 pizzas. int(17.25) is 17 , so adding 1 produces 18 pizzas, a better estimate to ensure enough pizza is ordered.Concepts in Practice 2.2.6 §2.2 source ↗
Given x = 4.5 and y = int(x), what is the value of each expression?
y- a.
4 - b.
5
Answer
4y = int(4.5) = 4. The fractional part, 0.5, is removed.Concepts in Practice 2.2.7 §2.2 source ↗
Given x = 4.5 and y = int(x), what is the value of each expression?
str(x)- a.
4.5 - b.
"4.5"
Answer
"4.5"str(4.5) produces "4.5" .Concepts in Practice 2.2.8 §2.2 source ↗
Given x = 4.5 and y = int(x), what is the value of each expression?
float(y)- a.
4.0 - b.
4.5
Answer
4.0float(y) = float(4) = 4.0.The fractional part, 0.5, was removed during the integer conversion, int(x).Concepts in Practice 2.3.1 §2.3 source ↗
8 * 0.25- a.
2 - b.
2.0
Answer
2.0Concepts in Practice 2.3.2 §2.3 source ↗
2 * 9- a.
18 - b.
18.0
Answer
18Concepts in Practice 2.3.3 §2.3 source ↗
20 / 2- a.
10 - b.
10.0
Answer
10.0Concepts in Practice 2.3.4 §2.3 source ↗
7 / 2- a.
3.0 - b.
3.5
Answer
3.5Concepts in Practice 2.3.5 §2.3 source ↗
12.0 / 4- a.
3 - b.
3.0
Answer
3.0Concepts in Practice 2.3.6 §2.3 source ↗
8 - 1.0- a.
7.0 - b.
7
Answer
7.0Concepts in Practice 2.3.7 §2.3 source ↗
5 - 0.25- a.
4.5 - b.
4.75
Answer
4.75Concepts in Practice 2.3.8 §2.3 source ↗
int('34') + 56- a.
'3456' - b.
90 - c.
'90'
Answer
9034 + 56 produces 90 , an integer.Concepts in Practice 2.3.9 §2.3 source ↗
str(12) + ' red roses'- a.
'12 red roses' - b.
'12' - c.
Error
Answer
'12 red roses''12' + ' red roses' performs string concatenation.Concepts in Practice 2.3.10 §2.3 source ↗
'50' * 3- a.
'150' - b.
150 - c.
'505050'
Answer
'505050'Concepts in Practice 2.3.11 §2.3 source ↗
str(5.2) + 7- a.
12.2 - b.
'12.2' - c.
Error
Answer
Error '5.2' , a string, and 7 , an integer, is not an accepted operation and leads to an error.Concepts in Practice 2.3.12 §2.3 source ↗
80.0 + int('100')- a.
180 - b.
180.0 - c.
'180'
Answer
180.0Concepts in Practice 2.3.13 §2.3 source ↗
str(3.14) + '159'- a.
162.14 - b.
'3.14159' - c.
Error
Answer
'3.14159' '3.14' and '159' are concatenated.Concepts in Practice 2.3.14 §2.3 source ↗
2.0 * 'this'- a.
'this' - b.
'thisthis' - c.
Error
Answer
ErrorConcepts in Practice 2.4.1 §2.4 source ↗
For each situation, which error occurs?
The statementresult = 2.0 * (10.0 ** 500) assigns the variable result with too large of a value.- a.round-off
- b.overflow
Answer
Concepts in Practice 2.4.2 §2.4 source ↗
For each situation, which error occurs?
0.123456789012345678901234567890 * 0.1 produces 0.012345678901234568430878013601.- a.round-off
- b.overflow
Answer
Concepts in Practice 2.4.3 §2.4 source ↗
round(6.6)?- a.
6 - b.
6.6 - c.
7
Answer
7round(6.6) = 7 as 7 is the closest whole number to 6.6.Concepts in Practice 2.4.4 §2.4 source ↗
round(3.5, 2)?- a.
3 - b.
3.5 - c.
3.50
Answer
3.50round(3.5, 2) = 3.50 as the input is rounded to two decimal places.Concepts in Practice 2.4.5 §2.4 source ↗
round(12)?- a.
12.0 - b.
12 - c.
12.00
Answer
12round() is applied to an integer value without specifying the number of decimal places, there will be no change to the value.Concepts in Practice 2.4.6 §2.4 source ↗
round(0.1, 1)?- a.
0.1 - b.
0.10 - c.
0.1000000000000000055511151231257827021181583404541015625
Answer
0.1round(0.1, 1) rounds the value 0.1 to one decimal place, which is 0.1 .Concepts in Practice 2.5.1 §2.5 source ↗
What is the value of each expression?
13 / 5- a.
2 - b.
2.6 - c.
3
Answer
2.6Concepts in Practice 2.5.2 §2.5 source ↗
What is the value of each expression?
13 % 5- a.
2 - b.
2.6 - c.
3
Answer
3Concepts in Practice 2.5.3 §2.5 source ↗
What is the value of each expression?
1 // 4- a.
0 - b.
0.25 - c.
1
Answer
0Concepts in Practice 2.5.4 §2.5 source ↗
What is the value of each expression?
2 % 0- a.
0 - b.
2 - c.
Error
Answer
ErrorConcepts in Practice 2.5.5 §2.5 source ↗
- a.40
- b.60
- c.280
Answer
Concepts in Practice 2.5.6 §2.5 source ↗
pounds = ounces // 16. What is likely the next line of code?- a.
ounces = ounces % 16 - b.
pounds = ounces % 16 - c.
ounces = ounces - pounds * 16
Answer
ounces = ounces % 16Concepts in Practice 2.6.1 §2.6 source ↗
NameError?- a.The math module was not available.
- b.The variable pie was spelled incorrectly.
- c.The math module was not imported.
Answer
Concepts in Practice 2.6.2 §2.6 source ↗
- a.
log() - b.
round() - c.
sqrt()
Answer
round()round() function, for rounding floating-point numbers, is built-in.Concepts in Practice 2.6.3 §2.6 source ↗
- a.
math.abs(1) - b.
math.log(1) - c.
math.sqrt(1)
Answer
math.abs(1)abs() is a built-in function. The math module does not have an abs() function.Concepts in Practice 2.6.4 §2.6 source ↗
math.tau/2?- a.approximately
2.718 - b.approximately
3.142 - c.approximately
6.283
Answer
3.142Concepts in Practice 2.6.5 §2.6 source ↗
math.sqrt(100)?- a.the float
10.0 - b.the integer
10 - c.
ValueError: math domain error
Answer
10.0math.sqrt() function returns a float, even when given an integer.Concepts in Practice 2.6.6 §2.6 source ↗
- a.
pi * r**2 - b.
math.pi * r**2 - c.
math.pi * r*2
Answer
math.pi * r**2Concepts in Practice 2.6.7 §2.6 source ↗
- a.
3 ** 3 - b.
3.0 ** 3 - c.
math.pow(3, 3)
Answer
3 ** 3Concepts in Practice 2.7.1 §2.7 source ↗
- a.
name = input("What is your name? ") - b.
name = input ("What is your name? ") - c.
name = input( "What is your name? " )
Answer
name = input("What is your name? ")Concepts in Practice 2.7.2 §2.7 source ↗
- a.
name=name+"!" - b.
name = name+"!" - c.
name = name + "!"
Answer
name = name + "!"Concepts in Practice 2.7.3 §2.7 source ↗
- a.
print("Hello",name) - b.
print("Hello", name) - c.
print("Hello " , name)
Answer
print("Hello", name)Concepts in Practice 2.7.4 §2.7 source ↗
- a.
b**2 - 4*a*c - b.
b ** 2 - 4 * a * c - c.
b**2 - 4*a * c
Answer
b**2 - 4*a*cb**2 and 4*a*c are evaluated first. Adding space around the - operator makes the order of operations more readable.Concepts in Practice 2.7.5 §2.7 source ↗
"grandmother"?- a.
print(grandmother) - b.
print("grand" "mother") - c.
print("grand", "mother")
Answer
print("grand" "mother")Concepts in Practice 2.7.6 §2.7 source ↗
"Today is" "a holiday"?- a.
'Today isa holiday' - b.
'Today is a holiday' - c.
'Today is" "a holiday'
Answer
'Today isa holiday' "is" and "a" .Concepts in Practice 2.7.7 §2.7 source ↗
"Ada", what does print("Hello," name) output?- a.
Hello,Ada - b.
Hello, Ada - c.
SyntaxError
Answer
SyntaxError "Hello," and the variable name cannot be concatenated without using a + operator. Alternatively, the two values could be separated by a comma in the print function.Concepts in Practice 2.7.8 §2.7 source ↗
- a.
/ - b.
\ - c.
|
Answer
\Concepts in Practice 2.7.9 §2.7 source ↗
- a.Break up the string into multiple smaller strings.
print("..." # first part of string "..." # next part of string "...") - b.Print the string using multiple print statements.
print("...") # first part of string print("...") # next part of string print("...") - c.Assign the string to a variable and print the variable.
text = "..." # the entire string print(text)
Answer
print("..." # first part of string
"..." # next part of string
"...")Concepts in Practice 2.7.10 §2.7 source ↗
- a.
print("Happy " "New Year") - b.
saying = ("Happy " "New Year") - c.
saying= "Happy " "New Year"
Answer
saying= "Happy " "New Year"
"Happy " to the variable saying. The second line has no effect on the program.Concepts in Practice 2.8.1 §2.8 source ↗
- a.geography
- b.health care
- c.political science
- d.all of the above
Answer
Concepts in Practice 2.8.2 §2.8 source ↗
- a.Matplotlib
- b.SciPy
- c.TensorFlow
Answer
Concepts in Practice 2.8.3 §2.8 source ↗
- a.Facebook
- b.Instagram
- c.WhatsApp
Answer
Loading chapter 3…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.