3.1 Strings revisited
6.
b.
Tilde (~) is the character for code point 126. The
chr()
function returns a string of length one.
8.
c.
The first backslash indicates an escape sequence, and the second backslash indicates the character.
3.2 Formatted strings
2.
a.
The string is not an f-string, so the brace characters (around temp and food) are printed literally.
3.
c.
Using an f-string allows the entire output to be specified without commas and extra quote marks.
4.
a.
The format specifier
02d
appends a 0 to the front of the integer when the integer is only one digit.
3.3 Variables revisited
2.
c.
The program assigns the int object
10
and the float object
9.7
, as shown in the memory diagram.
3.4 List basics
2.
a.
Individual elements in a list must be separated by a comma. Python does not understand what is meant by
2 3
.
4.
a.
The index -1 always refers to the last element of a list. The expression
len(name_list)-1
, which evaluates to
3
and refers to the last element in a list of 4 elements, can also be used in place of -1 here.
3.5 Tuple basics
3.
a.
A tuple must be created using commas. Parentheses are optional but recommended. The three elements are
"a"
,
"b"
, and
"c"
.
4.
b.
my_tuple
is initially assigned with the tuple (0.693, 0.414, 3.142)
. Then my_tuple
is assigned with the new tuple, (0.693, 1.414, 3.142)
, to correct a mistake with the second element.
5.
a.
Tuples are immutable. No changes, including appending, can be made after the tuple is created.
6.
b.
Tuples are immutable, so attempted changes produce errors. Using a tuple would protect the constants from accidental changes.