16.1: Chapter 2
( \newcommand{\kernel}{\mathrm{null}\,}\)
2.1 The Python shell
1.
a. Most programming languages are high-level and meant to be understood by humans.
2.
c. The most basic line of Python code is a single expression, such as the integer
1
.
3.
b. After the input is stored in memory, the shell displays a prompt for the next line.
4.
c. The up arrow displays the previous line, and the left arrow moves within that line.
5.
a. Pressing the up arrow twice moves to the line before the previous line.
2.2 Type conversion
1.
a.
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.
2.
a. Combining a float and an integer in a calculation produces a float.
book_rating
would be assigned with 4.0
.
3.
a. A calculation with a float and an integer produces a float.
x
is a float, 42.0
, after the code executes.
4.
a.
float()
converts the input number of slices from a string to a float for the pizza order calculation.
5.
b.
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.
6.
a.
y = int(4.5) = 4
. The fractional part, 0.5, is removed.
7.
b.
str(4.5)
produces "4.5"
.
8.
a.
float(y) = float(4) = 4.0
.The fractional part, 0.5, was removed during the integer conversion, int(x)
.
2.3 Mixed data types
1.
b. Multiplying an integer and a float produces a float.
2.
a. Multiplying two integers produces an integer.
3.
b. Dividing two integers produces a float, even when the result is a whole number.
4.
b. Dividing two integers produces a float and does not round.
5.
b. Dividing a float by an integer produces a float, even when the result is a whole number.
6.
a. Subtraction of a float from an integer produces a float.
7.
b. Subtraction of a float from an integer produces a float and does not round.
8.
b.
34 + 56
produces 90
, an integer.
9.
a.
'12' + ' red roses'
performs string concatenation.
10.
c. Multiplying a string by an integer is a useful shortcut.
11.
c. Adding
'5.2'
, a string, and 7
, an integer, is not an accepted operation and leads to an error.
12.
b. Adding a float and an integer produces a float.
13.
b.
'3.14'
and '159'
are concatenated.
14.
c. String repetition only works with strings and integers.
2.4 Floating-point errors
1.
b. An overflow error occurs because the value is larger than the upper limit of the floating-point range.
2.
a. The digits starting from the eighteenth place after the decimal are inaccurate due to approximation, so a round-off error occurs.
3.
c.
round(6.6) = 7
as 7 is the closest whole number to 6.6.
4.
c.
round(3.5, 2) = 3.50
as the input is rounded to two decimal places.
5.
b. When
round()
is applied to an integer value without specifying the number of decimal places, there will be no change to the value.
6.
a. The
round(0.1, 1)
rounds the value 0.1
to one decimal place, which is 0.1
.
2.5 Dividing integers
1.
b. 13 divided by 5 is 2 and 3/5. The / operator computes the result as a float.
2.
c. 5 goes into 13 two times, remainder 3. The % operator computes the remainder.
3.
a. 4 goes into 1 zero times, remainder 1. The // operator computes the quotient.
4.
c. Dividing by zero is undefined in math. Python raises an error when dividing by zero.
5.
b. The modulus is the amount to divide by. To convert hours to minutes, a program divides by 60.
6.
a. Programs that use floor division often use modulo on the next line.
2.6 The math module
1.
c. The import statement defines the math variable needed to access the module.
2.
b. The
round()
function, for rounding floating-point numbers, is built-in.
3.
a.
abs()
is a built-in function. The math module does not have an abs()
function.
4.
b. Tau is equal to 2π, so tau divided by 2 is π.
5.
a. The
math.sqrt()
function returns a float, even when given an integer.
6.
b. This expression is a concise way to write πr2.
7.
a. When given integers, the ** operator returns an integer result.
2.7 Formatting code
1.
a. The code has one space before and after the assignment operator (=).
2.
c. Each operator (=, +) has one space both before and after.
3.
b. The comma is followed by a space.
4.
a. The expressions
b**2
and 4*a*c
are evaluated first. Adding space around the -
operator makes the order of operations more readable.
5.
b. The two consecutive strings are automatically combined into one string before printing.
6.
a. The original strings do not have a space between the words
"is"
and "a"
.
7.
c. 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.
8.
b. The backslash, when used at the end of a line, indicates a multi-line statement.
9.
a. Using one print statement with parentheses joins the lines implicitly and does not increase the number of statements.
10.
c. The first line assigns
"Happy "
to the variable saying. The second line has no effect on the program.
2.8 Python careers
1.
d. Python is one of the most-used programming languages in the world, especially in fields outside of CS and IT.
2.
a. Matplotlib is a popular library for creating charts, plots, and other visualizations.
3.
b. As of 2023, Instagram is the largest deployment of the Python Django web framework.