Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 2: Expressions

59 problems · 0 figures

Concepts in Practice 2.1.1 §2.1 source ↗

Python is a _____ language.
  1. a.
    high-level
  2. b.
    low-level

Answer

A. high-level
Most programming languages are high-level and meant to be understood by humans.

Concepts in Practice 2.1.2 §2.1 source ↗

Which of the following is the most basic line of code the interpreter can run?
  1. a.
    print(1 + 1)
  2. b.
    1 + 1
  3. c.
    1

Answer

C. 1
The most basic line of Python code is a single expression, such as the integer 1 .

Concepts in Practice 2.1.3 §2.1 source ↗

What result does the shell display after running the line name = input()?
  1. a.
    the name that was input
  2. b.
    nothing (except for >>>)

Answer

B. nothing (except for >>>)
After the input is stored in memory, the shell displays a prompt for the next line.

Concepts in Practice 2.1.4 §2.1 source ↗

Which arrow keys were needed to edit the typo?
  1. a.
    only the up arrow key
  2. b.
    the up and down arrows
  3. c.
    the up and left arrows

Answer

C. the up and left arrows
The up arrow displays the previous line, and the left arrow moves within that line.

Concepts in Practice 2.1.5 §2.1 source ↗

What keys would the user press to go back two lines?
  1. a.
    press the up arrow twice
  2. b.
    press the down arrow twice
  3. c.
    press the left arrow twice

Answer

A. press the up arrow twice
Pressing the up arrow twice moves to the line before the previous line.

Concepts in Practice 2.2.1 §2.2 source ↗

Consider the example above.

What is book_rating's data type on line 7?
  1. a.
    float
  2. b.
    integer
Video · Example: Book ratings in the book ↗

Answer

A. float
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 would book_rating's data type be if update = 1.0 instead of 0.5?
  1. a.
    float
  2. b.
    integer
Video · Example: Book ratings in the book ↗

Answer

A. float
Combining a float and an integer in a calculation produces a float. 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 of x after the following code executes?
x = 42.0
x = x * 1
  1. a.
    float
  2. b.
    integer
Video · Example: Book ratings in the book ↗

Answer

A. float
A calculation with a float and an integer produces a float. 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?
  1. a.
    float()
  2. b.
    input()
  3. c.
    int()
Video · Example: Ordering pizza in the book ↗

Answer

A. 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?
  1. a.
    Use float() instead of int().
  2. b.
    Add 1 to the result of int().
  3. c.
    Add str() around int().
Video · Example: Ordering pizza in the book ↗

Answer

B. Add 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
  1. a.
    4
  2. b.
    5

Answer

A. 4
y = 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)
  1. a.
    4.5
  2. b.
    "4.5"

Answer

B. "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)
  1. a.
    4.0
  2. b.
    4.5

Answer

A. 4.0
float(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
  1. a.
    2
  2. b.
    2.0

Answer

B. 2.0
Multiplying an integer and a float produces a float.

Concepts in Practice 2.3.2 §2.3 source ↗

2 * 9
  1. a.
    18
  2. b.
    18.0

Answer

A. 18
Multiplying two integers produces an integer.

Concepts in Practice 2.3.3 §2.3 source ↗

20 / 2
  1. a.
    10
  2. b.
    10.0

Answer

B. 10.0
Dividing two integers produces a float, even when the result is a whole number.

Concepts in Practice 2.3.4 §2.3 source ↗

7 / 2
  1. a.
    3.0
  2. b.
    3.5

Answer

B. 3.5
Dividing two integers produces a float and does not round.

Concepts in Practice 2.3.5 §2.3 source ↗

12.0 / 4
  1. a.
    3
  2. b.
    3.0

Answer

B. 3.0
Dividing a float by an integer produces a float, even when the result is a whole number.

Concepts in Practice 2.3.6 §2.3 source ↗

8 - 1.0
  1. a.
    7.0
  2. b.
    7

Answer

A. 7.0
Subtraction of a float from an integer produces a float.

Concepts in Practice 2.3.7 §2.3 source ↗

5 - 0.25
  1. a.
    4.5
  2. b.
    4.75

Answer

B. 4.75
Subtraction of a float from an integer produces a float and does not round.

Concepts in Practice 2.3.8 §2.3 source ↗

int('34') + 56
  1. a.
    '3456'
  2. b.
    90
  3. c.
    '90'

Answer

B. 90
34 + 56 produces 90 , an integer.

Concepts in Practice 2.3.9 §2.3 source ↗

str(12) + ' red roses'
  1. a.
    '12 red roses'
  2. b.
    '12'
  3. c.
    Error

Answer

A. '12 red roses'
'12' + ' red roses' performs string concatenation.

Concepts in Practice 2.3.10 §2.3 source ↗

'50' * 3
  1. a.
    '150'
  2. b.
    150
  3. c.
    '505050'

Answer

C. '505050'
Multiplying a string by an integer is a useful shortcut.

Concepts in Practice 2.3.11 §2.3 source ↗

str(5.2) + 7
  1. a.
    12.2
  2. b.
    '12.2'
  3. c.
    Error

Answer

C. Error
Adding '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')
  1. a.
    180
  2. b.
    180.0
  3. c.
    '180'

Answer

B. 180.0
Adding a float and an integer produces a float.

Concepts in Practice 2.3.13 §2.3 source ↗

str(3.14) + '159'
  1. a.
    162.14
  2. b.
    '3.14159'
  3. c.
    Error

Answer

B. '3.14159'
'3.14' and '159' are concatenated.

Concepts in Practice 2.3.14 §2.3 source ↗

2.0 * 'this'
  1. a.
    'this'
  2. b.
    'thisthis'
  3. c.
    Error

Answer

C. Error
String repetition only works with strings and integers.

Concepts in Practice 2.4.1 §2.4 source ↗

For each situation, which error occurs?

The statement result = 2.0 * (10.0 ** 500) assigns the variable result with too large of a value.
  1. a.
    round-off
  2. b.
    overflow

Answer

B. overflow
An overflow error occurs because the value is larger than the upper limit of the floating-point range.

Concepts in Practice 2.4.2 §2.4 source ↗

For each situation, which error occurs?

0.123456789012345678901234567890 * 0.1 produces 0.012345678901234568430878013601.
  1. a.
    round-off
  2. b.
    overflow

Answer

A. round-off
The digits starting from the eighteenth place after the decimal are inaccurate due to approximation, so a round-off error occurs.

Concepts in Practice 2.4.3 §2.4 source ↗

What is the output of round(6.6)?
  1. a.
    6
  2. b.
    6.6
  3. c.
    7

Answer

C. 7
round(6.6) = 7 as 7 is the closest whole number to 6.6.

Concepts in Practice 2.4.4 §2.4 source ↗

What is the output of round(3.5, 2)?
  1. a.
    3
  2. b.
    3.5
  3. c.
    3.50

Answer

C. 3.50
round(3.5, 2) = 3.50 as the input is rounded to two decimal places.

Concepts in Practice 2.4.5 §2.4 source ↗

What is the output of round(12)?
  1. a.
    12.0
  2. b.
    12
  3. c.
    12.00

Answer

B. 12
When round() 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 ↗

What is the output of round(0.1, 1)?
  1. a.
    0.1
  2. b.
    0.10
  3. c.
    0.1000000000000000055511151231257827021181583404541015625

Answer

A. 0.1
The round(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
  1. a.
    2
  2. b.
    2.6
  3. c.
    3

Answer

B. 2.6
13 divided by 5 is 2 and 3/5. The / operator computes the result as a float.

Concepts in Practice 2.5.2 §2.5 source ↗

What is the value of each expression?

13 % 5
  1. a.
    2
  2. b.
    2.6
  3. c.
    3

Answer

C. 3
5 goes into 13 two times, remainder 3. The % operator computes the remainder.

Concepts in Practice 2.5.3 §2.5 source ↗

What is the value of each expression?

1 // 4
  1. a.
    0
  2. b.
    0.25
  3. c.
    1

Answer

A. 0
4 goes into 1 zero times, remainder 1. The // operator computes the quotient.

Concepts in Practice 2.5.4 §2.5 source ↗

What is the value of each expression?

2 % 0
  1. a.
    0
  2. b.
    2
  3. c.
    Error

Answer

C. Error
Dividing by zero is undefined in math. Python raises an error when dividing by zero.

Concepts in Practice 2.5.5 §2.5 source ↗

What is the modulus for converting minutes to hours?
  1. a.
    40
  2. b.
    60
  3. c.
    280

Answer

B. 60
The modulus is the amount to divide by. To convert hours to minutes, a program divides by 60.

Concepts in Practice 2.5.6 §2.5 source ↗

A program has the line pounds = ounces // 16. What is likely the next line of code?
  1. a.
    ounces = ounces % 16
  2. b.
    pounds = ounces % 16
  3. c.
    ounces = ounces - pounds * 16

Answer

A. ounces = ounces % 16
Programs that use floor division often use modulo on the next line.

Concepts in Practice 2.6.1 §2.6 source ↗

In the above example, when evaluating math, why did the interpreter raise a NameError?
  1. a.
    The math module was not available.
  2. b.
    The variable pie was spelled incorrectly.
  3. c.
    The math module was not imported.
Video · Importing math in a Python shell in the book ↗

Answer

C. The math module was not imported.
The import statement defines the math variable needed to access the module.

Concepts in Practice 2.6.2 §2.6 source ↗

Which of these functions is builtin and does not need to be imported?
  1. a.
    log()
  2. b.
    round()
  3. c.
    sqrt()

Answer

B. round()
The round() function, for rounding floating-point numbers, is built-in.

Concepts in Practice 2.6.3 §2.6 source ↗

Which expression results in an error?
  1. a.
    math.abs(1)
  2. b.
    math.log(1)
  3. c.
    math.sqrt(1)

Answer

A. 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 ↗

What is the value of math.tau/2?
  1. a.
    approximately 2.718
  2. b.
    approximately 3.142
  3. c.
    approximately 6.283

Answer

B. approximately 3.142
Tau is equal to 2π, so tau divided by 2 is π.

Concepts in Practice 2.6.5 §2.6 source ↗

What is the value of math.sqrt(100)?
  1. a.
    the float 10.0
  2. b.
    the integer 10
  3. c.
    ValueError: math domain error

Answer

A. the float 10.0
The math.sqrt() function returns a float, even when given an integer.

Concepts in Practice 2.6.6 §2.6 source ↗

What is πr2 in Python syntax?
  1. a.
    pi * r**2
  2. b.
    math.pi * r**2
  3. c.
    math.pi * r*2

Answer

B. math.pi * r**2
This expression is a concise way to write πr2.

Concepts in Practice 2.6.7 §2.6 source ↗

Which expression returns the integer 27?
  1. a.
    3 ** 3
  2. b.
    3.0 ** 3
  3. c.
    math.pow(3, 3)

Answer

A. 3 ** 3
When given integers, the ** operator returns an integer result.

Concepts in Practice 2.7.1 §2.7 source ↗

Which statement is formatted properly?
  1. a.
    name = input("What is your name? ")
  2. b.
    name = input ("What is your name? ")
  3. c.
    name = input( "What is your name? " )

Answer

A. name = input("What is your name? ")
The code has one space before and after the assignment operator (=).

Concepts in Practice 2.7.2 §2.7 source ↗

Which statement is formatted properly?
  1. a.
    name=name+"!"
  2. b.
    name = name+"!"
  3. c.
    name = name + "!"

Answer

C. name = name + "!"
Each operator (=, +) has one space both before and after.

Concepts in Practice 2.7.3 §2.7 source ↗

Which statement is formatted properly?
  1. a.
    print("Hello",name)
  2. b.
    print("Hello", name)
  3. c.
    print("Hello " , name)

Answer

B. print("Hello", name)
The comma is followed by a space.

Concepts in Practice 2.7.4 §2.7 source ↗

Which expression is formatted properly?
  1. a.
    b**2 - 4*a*c
  2. b.
    b ** 2 - 4 * a * c
  3. c.
    b**2 - 4*a * c

Answer

A. b**2 - 4*a*c
The expressions b**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 ↗

Which line prints the word "grandmother"?
  1. a.
    print(grandmother)
  2. b.
    print("grand" "mother")
  3. c.
    print("grand", "mother")

Answer

B. print("grand" "mother")
The two consecutive strings are automatically combined into one string before printing.

Concepts in Practice 2.7.6 §2.7 source ↗

What string is equivalent to "Today is" "a holiday"?
  1. a.
    'Today isa holiday'
  2. b.
    'Today is a holiday'
  3. c.
    'Today is" "a holiday'

Answer

A. 'Today isa holiday'
The original strings do not have a space between the words "is" and "a" .

Concepts in Practice 2.7.7 §2.7 source ↗

If name is "Ada", what does print("Hello," name) output?
  1. a.
    Hello,Ada
  2. b.
    Hello, Ada
  3. c.
    SyntaxError

Answer

C. SyntaxError
The literal "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 ↗

Which character is used for explicit line joining?
  1. a.
    /
  2. b.
    \
  3. c.
    |

Answer

B. \
The backslash, when used at the end of a line, indicates a multi-line statement.

Concepts in Practice 2.7.9 §2.7 source ↗

What is the best way to print a very long string?
  1. a.
    Break up the string into multiple smaller strings.
    print("..." # first part of string
    "..." # next part of string
    "...")
  2. b.
    Print the string using multiple print statements.
    print("...") # first part of string
    print("...") # next part of string
    print("...")
  3. c.
    Assign the string to a variable and print the variable.
    text = "..." # the entire string
    print(text)

Answer

A. Break up the string into multiple smaller strings.
print("..." # first part of string
"..." # next part of string
"...")
Using one print statement with parentheses joins the lines implicitly and does not increase the number of statements.

Concepts in Practice 2.7.10 §2.7 source ↗

Which example consists of two statements?
  1. a.
    print("Happy "
    "New Year")
  2. b.
    saying = ("Happy "
    "New Year")
  3. c.
    saying= "Happy "
    "New Year"

Answer

C.
saying= "Happy "
"New Year"
The first line assigns "Happy " to the variable saying. The second line has no effect on the program.

Concepts in Practice 2.8.1 §2.8 source ↗

Which of the following fields use Python to support their work?
  1. a.
    geography
  2. b.
    health care
  3. c.
    political science
  4. d.
    all of the above

Answer

D. all of the above
Python is one of the most-used programming languages in the world, especially in fields outside of CS and IT.

Concepts in Practice 2.8.2 §2.8 source ↗

Which of the following Python libraries creates charts and plots?
  1. a.
    Matplotlib
  2. b.
    SciPy
  3. c.
    TensorFlow

Answer

A. Matplotlib
Matplotlib is a popular library for creating charts, plots, and other visualizations.

Concepts in Practice 2.8.3 §2.8 source ↗

Which of the following applications were built with Python?
  1. a.
    Facebook
  2. b.
    Instagram
  3. c.
    WhatsApp

Answer

B. Instagram
As of 2023, Instagram is the largest deployment of the Python Django web framework.

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.