Python 02 Exercises
Python 02 Exercises
Practical Programming
in Python
Inspired by ’Practical Programming’ by Paul Gries, Jennifer Campbell, Jason Montojo
• Every value in Python has a specific type, which determines what operations can be ap-
plied to it. The two types used to represent numbers are int and float. Floating-point
numbers are approximations to real numbers.
• Python stores every value in computer memory. A memory location containing a value
is called an object.
• Variables contain memory addresses of values. We say that variables refer to values.
1 9 - 3
2 8 * 2.5
3 9 / 2
4 9 / -2
5 9 // 2
6 9 % 2
7 9.0 % 2
8 9.0 % 2.0
9 9 % -2
10 -9 % 2
11 9 / -2.0
12 4 + 3 * 5
13 (4 + 3) * 5
2. Convert the value in temp from Celsius to Fahrenheit by multiplying by 1.8 and adding 32;
make temp refer to the resulting value.
What is temp’s new value? Draw a memory model diagram showing what happens.
Exercise 4: Operator Precedence
For each of the following expressions, in which order are the subexpressions evaluated?
1 6 * 3 + 7 * 4
2 5 + 3 / 4
3 5 - 2 * 3 ** 4
After the last statement has been executed, what are x and y’s values? Draw a memory model
diagram illustrating what happens.
1 6 * ------8
2 8 = people
3 ((((4 ** 3))))
4 (-(-(-(-5))))
5 4 += 7 / 2