Exercise 1.1
(a) Using Python as a Calculator
On your machine, start Python and use it as a calulator to solve the following problem.
Lucky Larry bought 75 shares of Google stock at a price of $235.14 per share. Today, shares of Google are priced at $711.25. Using Python’s interactive mode as a calculator, figure out how much profit Larry would make if he sold all of his shares.
>>> (711.25 - 235.14) * 75
35708.25
>>>
Pro-tip: Use the underscore (_) variable to use the result of the last calculation. For example, how much profit does Larry make after his evil broker takes their 20% cut?
>>> _ * 0.80
28566.600000000002
>>>
(b) Getting help
Use the help()
command to get help on the abs()
function. Then use help()
to
get help on the round()
function. Type help()
just by itself with no value to enter the
interactive help viewer.
One caution with help()
is that it doesn’t work for basic Python statements such as print
,
for
, if
, while
, and so forth (i.e., if you type help(print)
you’ll
get a syntax error). Sadly, there is no other option than to read the notes or a Python book for further information
here.
Followup: Go to http://docs.python.org and find the
documentation for the abs()
function (hint: it’s found under the
library reference related to built-in functions).
(c) Cutting and Pasting
For parts of this class, you will be required to cut and paste
interactive code samples from the exercise page to the
interactive session. To do this, you must select code starting after
the >>>
prompt and going up to, but not any further than the first
blank line or the next >>>
prompt (whichever appears first). Select
"copy" from the brower, go to the Python window, and select "paste" to
copy it into the Python shell. To get the code to run, you may have
to hit "Return" once after you’ve pasted it in.
Use cut-and-paste to execute the Python statements in this session:
>>> 12 + 20
32
>>> (3 + 4
+ 5 + 6)
18
>>> for i in range(5):
print i
0
1
2
3
4
>>>
Warning: It is never possible to paste more than one Python command (statements that
appear after >>>
) to the Python shell at a time. You have to paste each command one at a time.
Tip: Use keyboard shortcuts for cut and paste. On Windows, use Ctrl-C to copy, and Ctrl-V to paste. On the Mac, use Command-C to copy and Command-V to paste.
(d) Where is My Bus?
If you’ve made it this far and are waiting for others to catch up, type these statements to find out how long people waiting on the corner of Clark street and Balmoral in Chicago will have to wait for the next northbound CTA #22 bus:
>>> import urllib
>>> u = urllib.urlopen("http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=14791&route=22")
>>> from xml.etree.ElementTree import parse
>>> doc = parse(u)
>>> for pt in doc.findall(".//pt"):
print pt.text
6 MIN
18 MIN
28 MIN
>>>
Yes, you just downloaded a web page, parsed an XML document, and extracted some useful information in about 6 lines of code. The data you accessed is actually feeding the website http://ctabustracker.com/bustime/home.jsp. Try it again and watch the predictions change.