1.1 Background
1.2 Input/output
2.
c.
The
end=""
option removes the newline character after hello, producing Hello world!
on the same line.
3.
c.
The default space character that separates multiple items has been replaced with the dash character.
5.
b.
The
print()
function outputs
"You entered:"
followed by a space character, followed by the input data, Sophia
, which is referenced by the variable name.
6.
a.
The input is stored exactly as the user enters the input. So, the retrieved value is displayed exactly as entered by the user.
1.3 Variables
4.
a.
An assignment statement has the variable name on the left side and the value on the right side of the assignment statement.
1.4 String basics
1.
c.
A string that is enclosed by matching double quotes is a valid string. Note that
'7 days'
is also a string.
2.
c.
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.
3.
c.
The string has matching double quotes. Since a single quote is a part of this string, double quotes must be used.
4.
c.
Note that the enclosing single quotes (') are not part of the string but the double quotes (") are.
5.
c.
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".
7.
b.
The
number_of_digits
variable's value is
2
, and as such, the output is
"Number 12 has 2 digits."
.
8.
c.
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.
9.
c.
"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.
10.
c.
Note that space characters must be explicitly included when using the concatenation operator.
11.
c.
A concatenation of the two strings is assigned to the variable
holiday
. Note that
"one-sided"
would assign the same string to holiday
.
1.5 Number basics
1.
c.
x = 1
is an integer, and when printing the type of an integer variable, the output is <class 'int'>
.
4.
c.
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.
6.
c.
The result of the division operation is a floating-point number that isn't rounded down.
7/2 = 3.5
.
8.
c.
The output of an arithmetic operator on any floating-point operand results in a floating-point value.
2 * 1.5 = 3.0
.
10.
b.
Although addition has lower precedence than exponentiation, the parentheses cause
1 + 3
to be evaluated first.
11.
a.
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
.
12.
c.
The statement uses the assignment operator (
=
), the negative operator (-
), and the exponentiation operator (**
).
1.6 Error messages
1.
b.
The filename can be found after the last slash (/). Other names, like Desktop, refer to folders.
4.
a.
The middle three lines are indented. In Python, lines may not begin with spaces or tabs unexpectedly.
1.7 Comments
1.
b.
Comments help explain the purpose of code so that other programmers understand what the code intends.
4.
c.
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.
5.
a.
A blank line separates the input from the output.
Then again, inserting a blank line in a three-line program is generally unnecessary.
Then again, inserting a blank line in a three-line program is generally unnecessary.
8.
c.
Docstrings must be a valid string, not a multi-line comment. Docstrings are stored in the program's memory, but comments are ignored.
9.
b.
The amount of information in this docstring is just right. In most cases, the docstring should not be longer than the code.