1.14. Exercises#
For each of the following expressions, what value will the expression produce? Verify your answers by typing the expressions into Python.
9 - 3
8 * 2.5
9 / 2
9 / -2
9 // -2
9 % 2
9.0 % 2
9 % 2.0
9 % -2
-9 % 2
9 / -2.0
4 + 3 * 5
(4 + 3) * 5
Unary minus negates a number. Unary plus exists as well; for example, Python understands
+5
. Ifx
has the value-17
, what do you think+x
should do?Should it leave the sign of the number alone?
Should it act like absolute value, removing any negation? Use the Python shell to find out its behavior.
Write two assignment statements that do the following:
Create a new variable,
temp
, and assign it the value24
.Convert the value in
temp
from Celsius to Fahrenheit by multiplying by1.8
and adding32
. Maketemp
refer to the resulting value. What istemp
’s new value?
For each of the following expressions, in which order are the subexpressions evaluated?
6 * 3 + 7 * 4
5 + 3 / 4
5 - 2 * 3 ** 4
Exercise
Create a new variable
x
, and assign it the value10.5
.Create a new variable
y
, and assign it the value4
.Sum
x
andy
, and makex
refer to the resulting value. After this statement has been executed, what are the values ofx
andy
?
When a variable is used before it has been assigned a value, a
NameError
occurs. In the Python shell, write an expression that results in aNameError
.Which of the following expressions result in
SyntaxError
s?6 * ———--8
8 = people
((((4 ** 3))))
(-(-(-(-5))))
4 += 7 / 2