Chapter 2: Introduction to Python Programming 1
Introduction to Python Programming
2.1 Introduction
No questions.
2.2 Variables and Assignment Statements
2.2 Q1: Which of the following statements is false?
a. Variables store values for later use in your code.
b. The following statement creates x and uses the assignment symbol (=) to give
x a value.
x = 7
c. Every statement stops at the end of the line in which it begins.
d. Once x and y are created and assigned values, you can use the values in expres-
sions like:
x + y
2.2 Q2: Which of the following statements is false?
a. The following assignment statement adds the values of variables x and y and
assigns the result to the variable total:
total = x + y
b. The snippet in Part (a) is read, “total is assigned the value of x + y.”
c. The = symbol is the assignment operator.
d. The right side of the = symbol always executes first, then the result is assigned
to the variable on the symbol’s left side.
2.2 Q3: Which of statements a), b) or c) is false?
a. A variable name, such as x, is an identifier.
b. Each identifier may consist of letters, digits and underscores (_) but may not
begin with a digit.
c. Python is case insensitive, so number and Number are the same identifier de-
spite the fact that one begins with a lowercase letter and the other begins with an
uppercase letter.
d. All of the above statements are true.
2 Chapter 2: Introduction to Python Programming
© Copyright 2020 by Pearson Education, Inc. All Rights Reserved.
Answer: c. Actually, Python is case sensitive, so number and Number are dif-
ferent identifiers because one begins with a lowercase letter and the other
begins with an uppercase letter.
2.2 Q4: The value 10.5 is a(n) _______ (that is, a number with a decimal point).
a. integer
b. string
c. floating-point number
d. None of the above
2.3 Arithmetic
2.3 Q1: Which of the following statements a), b) or c) is false?
a. Python uses the asterisk (*) as the multiplication operator (e.g., 7 * 4).
b. The exponentiation (**) operator raises one value to the power of another (e.g.,
2 ** 10).
c. To square a number, you can use the exponent 1/2 (that is, 0.5), as in:
9 ** (1 / 2)
d. All of the above statements are true.
2.3 Q2: Which of the following statements is false?
a. True division (/) divides a numerator by a denominator and yields a floating-
point number with a decimal point.
b. Floor division (//) divides a numerator by a denominator, yielding the highest
integer that’s not greater than the result.
c. The expression –13 / 4 evaluates to –3.25.
d. The expression –13 // 4 evaluates to -3.
2.3 Q3: Which of the following statements is false?
a. Dividing by zero with / or // is not allowed and results in an exception—an
indication that a problem occurred.
b. Python reports an exception with a traceback.
c. Most exception names end with Exception.
d. In IPython interactive mode, the line that begins with —-> shows the code
that caused the exception.
Chapter 2: Introduction to Python Programming 3
© Copyright 2020 by Pearson Education, Inc. All Rights Reserved.
Answer: c. Actually, most exception names end with Error.
2.3 Q4: Which of the following statements is false?
a. The value of the following expression is 3:
17 % 5
b. The value of the following expression is 0.5:
7.5 % 3.5
c. You can use the remainder operator for applications such as determining
whether one number is a multiple of another.
d. You can use the remainder operator to determine whether a number is odd or
even.
2.3 Q5: Which of the following statements is false?
a. Algebraic expressions must be typed in straight-line form using Python’s oper–
ators.
b. The following code multiplies 10 times the quantity 5 + 3, yielding the result
80:
10 * (5 + 3)
c. Parentheses in an expression are said to be redundant (unnecessary) if remov-
ing them yields the same result.
d. The parentheses in part (b) above are redundant.
2.3 Q6: Which of the following statements is false?
a. Python applies the operators in arithmetic expressions according to the rules
of operator precedence, which are generally the same as those in algebra.
b. Parentheses have the highest level of precedence, so expressions in parenthe-
ses evaluate first—thus, parentheses may force the order of evaluation to occur
in any sequence you desire.
c. In expressions with nested parentheses, such as (a / (b – c)), the expression
in the innermost parentheses (that is, b – c) evaluates first.
d. If an expression contains several exponentiation operations, Python applies
them from left to right.
Chapter 2: Introduction to Python Programming 5
d. When print completes its task, it positions the screen cursor at the beginning
of the next line by default.
2.4 Q2: Which of the following statements a), b) or c) is false?
a. When a backslash (\) appears in a string, it’s known as the escape character.
b. The backslash and the character immediately following it form an escape se-
quence. For example, \n represents the newline character escape sequence,
which tells print to move the output cursor to the next line.
c. Placing two backslashes back-to-back tells print to display a blank line.
d. All of the above statements are true.
2.5 Triple-Quoted Strings
2.5 Q1: Triple-quoted strings are used to create:
a. multiline strings
b. strings containing single or double quotes
c. docstrings, which are the recommended way to document the purposes of cer-
tain program components.
d. All of the above
2.5 Q2: Which of the following statements is false?
a. A string delimited by single quotes may include double-quote characters:
print(‘Display “hi” in quotes’)
b. A string delimited by double quotes may include single quote characters:
print(“Display the name O’Brien”)
c. A string delimited by double quotes may not include double quotes.
d. To avoid using \’ and \” inside strings, you can enclose such strings in triple
quotes.
8 Chapter 2: Introduction to Python Programming
d. Forgetting the colon (:) after the condition is a common syntax error.
2.7 Q7: Which of the following statements is false?
a. Python requires you to indent the statements in suites.
b. Incorrect indentation of the statements in a suite can cause errors.
c. Using the assignment symbol (=) instead of the equality operator (==) in an if
statement’s condition is a common logic error.
d. Using == in place of = in an assignment statement can lead to subtle problems.
2.7 Q8: The chained comparison 3 < y <= 11 is false for which of the following
values of y:
a. 11
b. 10
c. 4
d. 3
2.8 Objects and Dynamic Typing
2.8 Q1: Assume x is 3, y is 7.1 and z is ‘11.5‘. Which of the following statements
is incorrect?
a. type(x) is int
b. the value of z is 11.5
c. the value of y is 7.1
d. type(z) is str
2.8 Q2: Which of the following statements is false?
a. Assigning an object to a variable binds that variable’s name to the object. You
can then use the variable in your code to access the object’s value.
b. After the following snippet’s assignment, the variable x refers to the integer
object containing 7.
x = 7
c. The following statement changes x’s value:
x + 10
Chapter 2: Introduction to Python Programming 9
d. The following statement changes x’s value:
x = x + 10
2.8 Q3: Which of the following Python concepts are demonstrated directly or in-
directly by the following code:
In [1]: x = 7
Out[1]: int
In [2]: type(x)
Out[2]: int
In [3]: x = 4.1
In [4]: type(x)
Out[4]: float
In [5]: x = ‘dog’
In [6]: type(x)
Out[6]: str
a. Python uses dynamic typing—it determines the type of the object a variable
refers to while executing your code.
b. Over its lifetime a variable can be bound to different objects, even objects of
different types.
c. Python creates objects in memory and removes them from memory as neces-
sary. This removal process—called garbage collection—helps ensure that
memory is available for new objects you create.
d. All of the above.
2.9 Intro to Data Science: Basic Descriptive Statistics
2.9 Q1: Which of the following statements about descriptive statistics is false?
a. The minimum is the smallest value in a collection of values.
b. The range is the values starting with the minimum and ending with the maxi-
mum.
c. The count is the number of values in a collection.
10 Chapter 2: Introduction to Python Programming
d. Measures of dispersion (also called measures of variability), such as count, help
determine how spread out values are.
2.9 Q2: Which of the following statements is incorrect?
a. min(17, 19, 23, 29, 31, 37, 43) is a valid call to built-in function min.
b. The range of values is simply the minimum through the maximum value.
c. Much of data science is devoted to getting to know your data.
d. All of the above are correct.
2.9 Q3: Which of the following statements about functional-style programming
is false?
a. With functional-style programming capabilities you can write code that is more
concise, clearer and easier to debug (find and correct errors).
b. The min and max functions are examples of a functional-style programming
concept called reduction. They reduce a collection of values to a single value.
c. Other reductions you’ll see include the sum, average, variance and standard de-
viation of a collection of values.
d. All reductions are built into Python—you cannot define custom reductions.