Names.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # # Names
  4. #
  5. # Names are given to values in Python using an *assignment* statement. In an assignment, a name is followed by `=`, which is followed by any expression. The value of the expression to the right of `=` is *assigned* to the name. Once a name has a value assigned to it, the value will be substituted for that name in future expressions.
  6. # In[1]:
  7. a = 10
  8. b = 20
  9. a + b
  10. # A previously assigned name can be used in the expression to the right of `=`.
  11. # In[2]:
  12. quarter = 1/4
  13. half = 2 * quarter
  14. half
  15. # However, only the current value of an expression is assigned to a name. If that value changes later, names that were defined in terms of that value will not change automatically.
  16. # In[3]:
  17. quarter = 4
  18. half
  19. # Names must start with a letter, but can contain both letters and numbers. A name cannot contain a space; instead, it is common to use an underscore character `_` to replace each space. Names are only as useful as you make them; it's up to the programmer to choose names that are easy to interpret. Typically, more meaningful names can be invented than `a` and `b`. For example, to describe the sales tax on a $5 purchase in Berkeley, CA, the following names clarify the meaning of the various quantities involved.
  20. # In[4]:
  21. purchase_price = 5
  22. state_tax_rate = 0.075
  23. county_tax_rate = 0.02
  24. city_tax_rate = 0
  25. sales_tax_rate = state_tax_rate + county_tax_rate + city_tax_rate
  26. sales_tax = purchase_price * sales_tax_rate
  27. sales_tax