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

Chapter 1: Statements

64 problems · 0 figures

Concepts in Practice 1.1.1 §1.1 source ↗

How many types of programs were described in the animation?
  1. a.
    3
  2. b.
    4
  3. c.
    5
Video · Online music streaming in the book ↗

Answer

B. 4
Each image in the animation represents a different type of computer program.

Concepts in Practice 1.1.2 §1.1 source ↗

What type of program will this book explain how to write?
  1. a.
    a tool that summarizes an individual's music preferences
  2. b.
    a mobile app for managing and sharing playlists of songs
  3. c.
    a website that shows the top artists for the past five years

Answer

A. a tool that summarizes an individual's music preferences
This type of analysis requires basic knowledge of programming.

Concepts in Practice 1.1.3 §1.1 source ↗

Which of the following devices is an example of a computer?
  1. a.
    wired headphones that plug into a smartphone
  2. b.
    remote control that pauses or skips the current song
  3. c.
    wi-fi speaker that streams music from Amazon

Answer

C. wi-fi speaker that streams music from Amazon
Like mobile apps, wi-fi speakers run small programs that connect to servers.

Concepts in Practice 1.1.4 §1.1 source ↗

Reading this book requires a strong background in mathematics.
  1. a.
    true
  2. b.
    false

Answer

B. false
This book assumes readers understand the basics of algebra, but nothing too difficult.

Concepts in Practice 1.1.5 §1.1 source ↗

In general, Python programs are _____ than Java programs.
  1. a.
    faster
  2. b.
    longer
  3. c.
    shorter

Answer

C. shorter
Python's syntax is more concise than Java's.

Concepts in Practice 1.1.6 §1.1 source ↗

In the example programs above, what syntax required by Java is not required by Python?
  1. a.
    semicolons
  2. b.
    parentheses
  3. c.
    quote marks
Video · Counting lines in a file in the book ↗

Answer

A. semicolons
Python does not require a semicolon at the end of each line. Braces are also not required.

Concepts in Practice 1.2.1 §1.2 source ↗

Which line of code prints Hello world! as one line of output?
  1. a.
    print(Hello world!)
  2. b.
    print("Hello", "world", "!")
  3. c.
    print("Hello world!")

Answer

C. print("Hello world!")
The hello world program is just one line of code.

Concepts in Practice 1.2.2 §1.2 source ↗

Which lines of code prints Hello world! as one line of output?
  1. a.
    print("Hello")
    print(" world!")
  2. b.
    print("Hello")
    print(" world!", end="")
  3. c.
    print("Hello", end="")
    print(" world!")

Answer

C.
print("Hello", end="")
print(" world!")
The end="" option removes the newline character after hello, producing Hello world! on the same line.

Concepts in Practice 1.2.3 §1.2 source ↗

What output is produced by the following statement?
print("555", "0123", sep="-")
  1. a.
    555 0123
  2. b.
    5550123-
  3. c.
    555-0123

Answer

C. 555-0123
The default space character that separates multiple items has been replaced with the dash character.

Concepts in Practice 1.2.4 §1.2 source ↗

Which line of code correctly obtains and stores user input?
  1. a.
    input()
  2. b.
    today_is = input
  3. c.
    today_is = input()

Answer

C. today_is = input()
The user input is stored in memory and can be referenced using today_is.

Concepts in Practice 1.2.5 §1.2 source ↗

Someone named Sophia enters their name when prompted with
print("Please enter your name: ")
name = input()
What is displayed by print("You entered:", name)?
  1. a.
    You entered: name
  2. b.
    You entered: Sophia
  3. c.
    You entered:, Sophia

Answer

B. You entered: Sophia
The print() function outputs "You entered:" followed by a space character, followed by the input data, Sophia, which is referenced by the variable name.

Concepts in Practice 1.2.6 §1.2 source ↗

What is the output if the user enters "six" as the input?
print("Please enter a number: ")
number = input()
print("Value =", number)
  1. a.
    Value = six
  2. b.
    Value = 6
  3. c.
    Value = number

Answer

A. Value = six
The input is stored exactly as the user enters the input. So, the retrieved value is displayed exactly as entered by the user.

Concepts in Practice 1.3.1 §1.3 source ↗

Which line of code correctly retrieves the value of the variable, city, after the following assignment?
city = "Chicago"
  1. a.
    print("In which city do you live?")
  2. b.
    city = "London"
  3. c.
    print("The city where you live is", city)

Answer

C. print("The city where you live is", city)
This would produce the output string, "The city where you live is Chicago" .

Concepts in Practice 1.3.2 §1.3 source ↗

Which program stores and retrieves a variable correctly?
  1. a.
    print("Total =", total)
    total = 6
  2. b.
    total = 6
    print("Total =", total)
  3. c.
    print("Total =", total)
    total = input()

Answer

B.
total = 6
print("Total =", total)
These two lines of code would produce the output string, "Total = 6" .

Concepts in Practice 1.3.3 §1.3 source ↗

Which is the assignment operator?
  1. a.
    :
  2. b.
    ==
  3. c.
    =

Answer

C. =
The single = character is the assignment operator.

Concepts in Practice 1.3.4 §1.3 source ↗

Which is a valid assignment?
  1. a.
    temperature = 98.5
  2. b.
    98.5 = temperature
  3. c.
    temperature - 23.2

Answer

A. temperature = 98.5
An assignment statement has the variable name on the left side and the value on the right side of the assignment statement.

Concepts in Practice 1.3.5 §1.3 source ↗

Which can be used as a variable name?
  1. a.
    median
  2. b.
    class
  3. c.
    import

Answer

A. median
median is a descriptive name for a variable that stores a median value.

Concepts in Practice 1.3.6 §1.3 source ↗

Why is the name, 2nd_input, not a valid variable name?
  1. a.
    contains an underscore
  2. b.
    starts with a digit
  3. c.
    is a keyword

Answer

B. starts with a digit
A variable name can contain, but not start with, a digit.

Concepts in Practice 1.3.7 §1.3 source ↗

Which would be a good name for a variable storing a zip code?
  1. a.
    z
  2. b.
    var_2
  3. c.
    zip_code

Answer

C. zip_code
The name clearly indicates the value being stored is a zip code.

Concepts in Practice 1.3.8 §1.3 source ↗

Given the variable name, DogBreed, which improvement conforms to Python's style guide?
  1. a.
    dog_breed
  2. b.
    dogBreed
  3. c.
    dog-breed

Answer

A. dog_breed
Snake case separates each word with an underscore and uses only lowercase letters.

Concepts in Practice 1.4.1 §1.4 source ↗

Which of the following is a string?
  1. a.
    Hello!
  2. b.
    29
  3. c.
    "7 days"

Answer

C. "7 days"
A string that is enclosed by matching double quotes is a valid string. Note that '7 days' is also a string.

Concepts in Practice 1.4.2 §1.4 source ↗

Which line of code assigns a string to the variable email?
  1. a.
    "fred78@gmail.com"
  2. b.
    "email = fred78@gmail.com"
  3. c.
    email = "fred78@gmail.com"

Answer

C. email = "fred78@gmail.com"
The string, fred78@gmail.com, is enclosed in matching double quotes, and is assigned to the variable email. A string can contain characters such as "@" or "." in addition to letters and numbers.

Concepts in Practice 1.4.3 §1.4 source ↗

Which is a valid string?
  1. a.
    I know you'll answer correctly!
  2. b.
    'I know you'll answer correctly!'
  3. c.
    "I know you'll answer correctly!"

Answer

C. "I know you'll answer correctly!"
The string has matching double quotes. Since a single quote is a part of this string, double quotes must be used.

Concepts in Practice 1.4.4 §1.4 source ↗

Which is a valid string?
  1. a.
    You say "Please" to be polite
  2. b.
    "You say "Please" to be polite"
  3. c.
    'You say "Please" to be polite'

Answer

C. 'You say "Please" to be polite'
Note that the enclosing single quotes (') are not part of the string but the double quotes (") are.

Concepts in Practice 1.4.5 §1.4 source ↗

What is the return value for len("Hi Ali")?
  1. a.
    2
  2. b.
    5
  3. c.
    6

Answer

C. 6
The len() function returns the number of characters in the string. 6 characters exist in "Hi Ali" which are "H", "i", " ", "A", "l", and "i".

Concepts in Practice 1.4.6 §1.4 source ↗

What is the length of an empty string variable ("")?
  1. a.
    undefined
  2. b.
    0
  3. c.
    2

Answer

B. 0
The length of an empty string equals 0.

Concepts in Practice 1.4.7 §1.4 source ↗

What is the output of the following code?
number = "12"
number_of_digits = len(number)
print("Number", number, "has", number_of_digits, "digits.")
  1. a.
    Number 12 has 12 digits.
  2. b.
    Number 12 has 2 digits.
  3. c.
    Number 12 has number_of_digits digits.

Answer

B. Number 12 has 2 digits.
The number_of_digits variable's value is 2 , and as such, the output is "Number 12 has 2 digits." .

Concepts in Practice 1.4.8 §1.4 source ↗

Which produces the string "10"?
  1. a.
    1 + 0
  2. b.
    "1 + 0"
  3. c.
    "1" + "0"

Answer

C. "1" + "0"
The 1 and 0 are enclosed in quotes indicating valid strings, and the + is not, indicating a concatenation operation. Thus, the string "10" is produced. Note that concatenation operator does not include any default separator characters like the space character.

Concepts in Practice 1.4.9 §1.4 source ↗

Which produces the string "Awake"?
  1. a.
    "wake" + "A"
  2. b.
    "A + wake"
  3. c.
    "A" + "wake"

Answer

C. "A" + "wake"
"A" and "wake" are valid strings, and the + is not enclosed in quotes, indicating a concatenation operation. Thus the string "Awake" is produced. Note that the concatenation operator does not include any default separator characters like the space character.

Concepts in Practice 1.4.10 §1.4 source ↗

A user enters "red" after the following line of code.
color = input("What is your favorite color?")
Which produces the output "Your favorite color is red!"?
  1. a.
    print("Your favorite color is " + color + !)
  2. b.
    print("Your favorite color is " + "color" + "!")
  3. c.
    print("Your favorite color is " + color + "!")

Answer

C. print("Your favorite color is " + color + "!")
Note that space characters must be explicitly included when using the concatenation operator.

Concepts in Practice 1.4.11 §1.4 source ↗

Which of the following assigns "one-sided" to the variable holiday?
  1. a.
    holiday = "one" + "sided"
  2. b.
    holiday = one-sided
  3. c.
    holiday = "one-" + "sided"

Answer

C. holiday = "one-" + "sided"
A concatenation of the two strings is assigned to the variable holiday. Note that "one-sided" would assign the same string to holiday.

Concepts in Practice 1.5.1 §1.5 source ↗

Assume that x = 1, y = 2.0, and s = "32".

What is the output of the following code?
print(x, type(x))
  1. a.
    1 'int'.
  2. b.
    1.0 <class 'float'>.
  3. c.
    1 <class 'int'>.

Answer

C. 1 <class 'int'>.
x = 1 is an integer, and when printing the type of an integer variable, the output is <class 'int'>.

Concepts in Practice 1.5.2 §1.5 source ↗

Assume that x = 1, y = 2.0, and s = "32".

What is the output of the following code?
print(y, type(y))
  1. a.
    2.0 <class 'int'>
  2. b.
    2.0 <class 'float'>
  3. c.
    2 <class 'int'>

Answer

B. 2.0 <class 'float'>
y = 2.0 is floating-point number.

Concepts in Practice 1.5.3 §1.5 source ↗

Assume that x = 1, y = 2.0, and s = "32".

What is the type of the following value?
"12.0"
  1. a.
    string
  2. b.
    int
  3. c.
    float

Answer

A. string
Any value defined within quotation marks is a string value regardless of the content.

Concepts in Practice 1.5.4 §1.5 source ↗

Assume that x = 7, y = 20, and z = 2.

Given the following lines of code, what is the output of the code?
c = 0
c = x - z
c = c + 1
print(c)
  1. a.
    1
  2. b.
    5
  3. c.
    6

Answer

C. 6
Line 2 of the code evaluates as follows: c = x - z = 7 - 2 = 5. Line 3 increments variable c by 1, making the total equal to 6.

Concepts in Practice 1.5.5 §1.5 source ↗

Assume that x = 7, y = 20, and z = 2.

What is the value of a?
a = 3.5 - 1.5
  1. a.
    2
  2. b.
    2.0
  3. c.
    2.5

Answer

B. 2.0
The output of 3.5-1.5 equals 2.0 , which is a floating-point number.

Concepts in Practice 1.5.6 §1.5 source ↗

Assume that x = 7, y = 20, and z = 2.

What is the output of print(x / z)?
  1. a.
    3
  2. b.
    3.0
  3. c.
    3.5

Answer

C. 3.5
The result of the division operation is a floating-point number that isn't rounded down. 7/2 = 3.5.

Concepts in Practice 1.5.7 §1.5 source ↗

Assume that x = 7, y = 20, and z = 2.

What is the output of print(y / z)?
  1. a.
    0
  2. b.
    10
  3. c.
    10.0

Answer

C. 10.0
The output of the 20/2 equals 10.0 because the division operation results in a float.

Concepts in Practice 1.5.8 §1.5 source ↗

Assume that x = 7, y = 20, and z = 2.

What is the output of print(z * 1.5)?
  1. a.
    2
  2. b.
    3
  3. c.
    3.0

Answer

C. 3.0
The output of an arithmetic operator on any floating-point operand results in a floating-point value. 2 * 1.5 = 3.0.

Concepts in Practice 1.5.9 §1.5 source ↗

What is the value of 4 * 3 ** 2 + 1?
  1. a.
    37
  2. b.
    40
  3. c.
    145

Answer

A. 37
3 raised to the power 2 is 9, times 4 is 36, plus 1 is 37.

Concepts in Practice 1.5.10 §1.5 source ↗

Which part of (1 + 3) ** 2 / 4 evaluates first?
  1. a.
    4 ** 2
  2. b.
    1 + 3
  3. c.
    2 / 4

Answer

B. 1 + 3
Although addition has lower precedence than exponentiation, the parentheses cause 1 + 3 to be evaluated first.

Concepts in Practice 1.5.11 §1.5 source ↗

What is the value of -4 ** 2?
  1. a.
    -16
  2. b.
    16

Answer

A. -16
In Python, exponentiation, or raising to a power, has higher precedence than making a value negative. Python's -16 result can be confusing because, in math, "-4 squared" is 16 .

Concepts in Practice 1.5.12 §1.5 source ↗

How many operators are in the following statement?
result = -2 ** 3
  1. a.
    1
  2. b.
    2
  3. c.
    3

Answer

C. 3
The statement uses the assignment operator (=), the negative operator (-), and the exponentiation operator (**).

Concepts in Practice 1.6.1 §1.6 source ↗

Given the following error message:

Traceback (most recent call last):
  File "/home/student/Desktop/example.py", line 2
    print "test"
        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("test")?
What is the filename of the program?
  1. a.
    Desktop
  2. b.
    example.py
  3. c.
    test

Answer

B. example.py
The filename can be found after the last slash (/). Other names, like Desktop, refer to folders.

Concepts in Practice 1.6.2 §1.6 source ↗

Given the following error message:

Traceback (most recent call last):
  File "/home/student/Desktop/example.py", line 2
    print "test"
        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("test")?
On which line was the error found?
  1. a.
    1
  2. b.
    2
  3. c.
    3

Answer

B. 2
Line 2 is mentioned immediately after the filename.

Concepts in Practice 1.6.3 §1.6 source ↗

Given the following error message:

Traceback (most recent call last):
  File "/home/student/Desktop/example.py", line 2
    print "test"
        ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("test")?
What type of error was found?
  1. a.
    missing parentheses
  2. b.
    SyntaxError
  3. c.
    traceback

Answer

B. SyntaxError
The type of error is always the first word on the last line of the error message.

Concepts in Practice 1.6.4 §1.6 source ↗

For each program below, what type of error will occur?

print("Breakfast options:")
 print("A. Cereal")
 print("B. Eggs")
 print("C. Yogurt")
choice = input("What would you like? ")
  1. a.
    IndentationError
  2. b.
    NameError
  3. c.
    SyntaxError

Answer

A. IndentationError
The middle three lines are indented. In Python, lines may not begin with spaces or tabs unexpectedly.

Concepts in Practice 1.6.5 §1.6 source ↗

For each program below, what type of error will occur?

birth = input("Enter your birth date: )
print("Happy birthday on ", birth)
  1. a.
    IndentationError
  2. b.
    NameError
  3. c.
    SyntaxError

Answer

C. SyntaxError
The quote marks are missing at the end of the input prompt.

Concepts in Practice 1.6.6 §1.6 source ↗

For each program below, what type of error will occur?

print("Breakfast options:")
print(" A. Cereal")
print(" B. Eggs")
print(" C. Yogurt")
choice = intput("What would you like? ")
  1. a.
    IndentationError
  2. b.
    NameError
  3. c.
    SyntaxError

Answer

B. NameError
The name for "input" is misspelled.

Concepts in Practice 1.7.1 §1.7 source ↗

The main purpose of writing comments is to _____.
  1. a.
    avoid writing syntax errors
  2. b.
    explain what the code does
  3. c.
    make the code run faster

Answer

B. explain what the code does
Comments help explain the purpose of code so that other programmers understand what the code intends.

Concepts in Practice 1.7.2 §1.7 source ↗

Which symbol is used for comments in Python?
  1. a.
    #
  2. b.
    /*
  3. c.
    //

Answer

A. #
The hash character indicates the beginning of a comment.

Concepts in Practice 1.7.3 §1.7 source ↗

Which comment is formatted correctly?
  1. a.
    0 spaces:
    #Get the user input
  2. b.
    1 space:
    # Get the user input
  3. c.
    2 spaces:
    #  Get the user input

Answer

B. 1 space:
# Get the user input
Having one space after the # is the community standard.

Concepts in Practice 1.7.4 §1.7 source ↗

Which comment is most useful for the following code?
print("You said:", adj1 + " " + noun1)
  1. a.
    # Append adj1 and noun1
  2. b.
    # Print out a bunch of stuff
  3. c.
    # Show the resulting phrase

Answer

C. # Show the resulting phrase
This comment explains the intent of the code. If the code had a mistake, the programmer reading the comment might notice the mistake more easily.

Concepts in Practice 1.7.5 §1.7 source ↗

Where should a blank line be inserted?
1  name = input("Whose birthday is today? ")
2  print("Happy birthday to", name)
3  print("Everyone cheer for", name)
  1. a.
    After line 1
  2. b.
    After line 2
  3. c.
    After line 3

Answer

A. After line 1
A blank line separates the input from the output.
Then again, inserting a blank line in a three-line program is generally unnecessary.

Concepts in Practice 1.7.6 §1.7 source ↗

To temporarily prevent a line from being run, one might . . .
  1. a.
    introduce a syntax error in the line.
  2. b.
    remove the line from the program.
  3. c.
    insert a # at the beginning of the line.

Answer

C. insert a # at the beginning of the line.
The # symbol "comments out" the line without removing the line permanently.

Concepts in Practice 1.7.7 §1.7 source ↗

The main purpose of writing docstrings is to . . .
  1. a.
    summarize the program's purpose or usage.
  2. b.
    explain how each part of the code works.
  3. c.
    maintain a list of ideas for new features.

Answer

A. summarize the program's purpose or usage.
The docstring is primarily written for others who will use the program.

Concepts in Practice 1.7.8 §1.7 source ↗

Which of the following is NOT a docstring?
  1. a.
    """Vacations Madlib."""
  2. b.
    """Vacations Madlib.
    This program asks the user for two adjectives
    and two nouns, which are then used to print
    a funny story about a vacation.
    """
  3. c.
    # Vacations Madlib.
    #
    # This program asks the user for two adjectives
    # and two nouns, which are then used to print
    # a funny story about a vacation.

Answer

C.
# Vacations Madlib.
#
# This program asks the user for two adjectives
# and two nouns, which are then used to print
# a funny story about a vacation.
Docstrings must be a valid string, not a multi-line comment. Docstrings are stored in the program's memory, but comments are ignored.

Concepts in Practice 1.7.9 §1.7 source ↗

Which docstring is most useful for this program?
  1. a.
    """Vacations Madlib."""
  2. b.
    """Vacations Madlib.
    
    This program asks the user for two adjectives
    and two nouns, which are then used to print
    a funny story about a vacation.
    """
  3. c.
    """Vacations Madlib.
    
    This program asks the user for two adjectives
    and two nouns, which are then used to print
    a funny story about a vacation. The code uses
    four variables to store the user input: two for
    the adjectives, and two for the nouns. The
    output is displayed on seven lines, beginning
    with a blank line after the input.
    """

Answer

B.
"""Vacations Madlib.

This program asks the user for two adjectives
and two nouns, which are then used to print
a funny story about a vacation.
"""
The amount of information in this docstring is just right. In most cases, the docstring should not be longer than the code.

Concepts in Practice 1.8.1 §1.8 source ↗

The Python programming language was named after a _____.
  1. a.
    British comedy group
  2. b.
    Dutch programmer
  3. c.
    non-venomous snake

Answer

A. British comedy group
The language was named after Monty Python. Just for fun, the official documentation makes subtle references to Monty Python comedy sketches.

Concepts in Practice 1.8.2 §1.8 source ↗

Which programming language came first?
  1. a.
    Java
  2. b.
    Python

Answer

B. Python
Python's first release was in 1991, after being developed in the late 1980's.

Concepts in Practice 1.8.3 §1.8 source ↗

Which sentence best describes the beginning of Python?
  1. a.
    CWI hired Guido van Rossum to design a new programming language to compete with C++.
  2. b.
    Python started out as a hobby and became open source after several years of development.
  3. c.
    van Rossum posted Python's source code online after working on the language for one year.

Answer

C. van Rossum posted Python's source code online after working on the language for one year.
Making Python open source early on allowed for many programmers to provide feedback.

Concepts in Practice 1.8.4 §1.8 source ↗

According to the TIOBE Index, which two languages were most popular from 2001 to 2021?
  1. a.
    C and C++
  2. b.
    Java and C
  3. c.
    Python and JavaScript

Answer

B. Java and C
For 20 years, Java and C traded places as the top language until Python took over in 2021!

Concepts in Practice 1.8.5 §1.8 source ↗

In what year did Python become the most asked about language on Stack Overflow?
  1. a.
    2018
  2. b.
    2019
  3. c.
    2020

Answer

A. 2018
The line for Python crosses the line for JavaScript in the middle of 2018.

Concepts in Practice 1.8.6 §1.8 source ↗

How long has TIOBE been tracking programming language popularity?
  1. a.
    since 1991
  2. b.
    since 2001
  3. c.
    since 2015

Answer

B. since 2001
TIOBE started tracking language popularity in 2001, several years after web search engines became popular.

Loading chapter 2…

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.