Appendix B. Exercise answers

 

B.1. Chapter 4

Try this: Variables and expressions

In the Python shell, create some variables. What happens when you try to put spaces, dashes, or other nonalphanumeric characters in the variable name? Play around with a few complex expressions, such as x = 2 + 4 * 5 – 6 / 3. Use parentheses to group the numbers in different ways, and see how that changes the result compared with the original ungrouped expression.

>>> x = 3
>>> y = 3.14
>>> y
3.14
>>> x
3
>>> big var = 12
  File "<stdin>", line 1
    big var = 12
          ^
SyntaxError: invalid syntax
>>> big-var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'big' is not defined
>>> big&var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'big' is not defined
>>> x = 2 + 4 * 5 - 6 /3
>>> x
20.0
>>> x = (2 + 4) * 5 - 6 /3
>>> x
28.0
>>> x = (2 + 4) * (5 - 6) /3
>>> x
-2.0

B.2. Chapter 5

B.3. Chapter 6

B.4. Chapter 7

B.5. Chapter 8

B.6. Chapter 9

B.7. Chapter 10

B.8. Chapter 11

B.9. Chapter 12

B.10. Chapter 13

B.11. Chapter 14

B.12. Chapter 15

B.13. Chapter 16

B.14. Chapter 17

B.15. Chapter 18

B.16. Chapter 20

B.17. Chapter 21

B.18. Chapter 22

B.19. Chapter 23

B.20. Chapter 24

sitemap