|
| 1 | +# 1.1 Python |
| 2 | + |
| 3 | +In this part, we'll start with the absolute basics. |
| 4 | + |
| 5 | +## Notes |
| 6 | + |
| 7 | +### What is Python? |
| 8 | + |
| 9 | +Python is an interpreted high level programming language. It is often classified as a |
| 10 | +["scripting language"](https://en.wikipedia.org/wiki/Scripting_language) and |
| 11 | +is considered to be similar to languages such as Perl, Tcl, or Ruby. The syntax |
| 12 | +of Python is loosely inspired by elements of C programming. |
| 13 | + |
| 14 | +Python was created by Guido van Rossum around 1990 who named it in honor of Monty Python. |
| 15 | + |
| 16 | +### Where to get Python? |
| 17 | + |
| 18 | +[Python.org](https://www.python.org/) is where you obtain Python. For the purposes of this course, you |
| 19 | +only need a basic installation. We recommend installing Python 3.6 or newer. We will be using Python3 in |
| 20 | +our solutions and notes. |
| 21 | + |
| 22 | +### Why was Python created? |
| 23 | + |
| 24 | +According to Guido: |
| 25 | + |
| 26 | +> My original motivation for creating Python was the perceived need |
| 27 | +> for a higher level language in the Amoeba [Operating Systems] |
| 28 | +> project. I realized that the development of system administration |
| 29 | +> utilities in C was taking too long. Moreover, doing these things in |
| 30 | +> the Bourne shell wouldn't work for a variety of reasons. ... So, |
| 31 | +> there was a need for a language that would bridge the gap between C |
| 32 | +> and the shell. |
| 33 | +> |
| 34 | +> - Guido van Rossum |
| 35 | +
|
| 36 | +### Where is Python on my Machine? |
| 37 | + |
| 38 | +Although there are many environments in which you might run Python, |
| 39 | +this course has you run Python programs from the terminal or command |
| 40 | +shell. From the terminal, you should be able to type a command such as |
| 41 | +this: |
| 42 | + |
| 43 | +```bash |
| 44 | +python --version |
| 45 | +# or |
| 46 | +python3 --version |
| 47 | +``` |
| 48 | + |
| 49 | +If you are new to using the shell or a terminal, you should probably |
| 50 | +stop, finish a short tutorial on that first, and then return here. |
| 51 | + |
| 52 | +Just so you know, you will become a much better Python programmer if |
| 53 | +you are able to run, debug, and interact with Python at the terminal |
| 54 | +shell. This is Python's native environment. If you are able to use |
| 55 | +Python in the shell, you will be able to use virtually everywhere |
| 56 | +else. |
| 57 | + |
| 58 | +## Exercises |
| 59 | + |
| 60 | +### Using Python as a Calculator |
| 61 | + |
| 62 | +On your machine, start Python and use it as a calulator to solve the |
| 63 | +following problem. |
| 64 | + |
| 65 | +Lucky Larry bought 75 shares of Google stock at a price of $235.14 per |
| 66 | +share. Today, shares of Google are priced at $711.25. Using Python’s |
| 67 | +interactive mode as a calculator, figure out how much profit Larry would |
| 68 | +make if he sold all of his shares. |
| 69 | + |
| 70 | +```pycon |
| 71 | +>>> (711.25 - 235.14) * 75 |
| 72 | +35708.25 |
| 73 | +>>> |
| 74 | +``` |
| 75 | + |
| 76 | +Pro-tip: Use the underscore (\_) variable to use the result of the last |
| 77 | +calculation. For example, how much profit does Larry make after his evil |
| 78 | +broker takes their 20% cut? |
| 79 | + |
| 80 | +```pycon |
| 81 | +>>> _ * 0.80 |
| 82 | +28566.600000000002 |
| 83 | +>>> |
| 84 | +``` |
| 85 | + |
| 86 | +### Getting help |
| 87 | + |
| 88 | +Use the `help()` command to get help on the `abs()` function. Then use |
| 89 | +`help()` to get help on the `round()` function. Type `help()` just by |
| 90 | +itself with no value to enter the interactive help viewer. |
| 91 | + |
| 92 | +One caution with `help()` is that it doesn’t work for basic Python |
| 93 | +statements such as `for`, `if`, `while`, and so forth (i.e., if you type |
| 94 | +`help(for)` you’ll get a syntax error). You can try putting the help |
| 95 | +topic in quotes such as `help("for")` instead. If that doesn’t work, |
| 96 | +you’ll have to turn to an internet search. |
| 97 | + |
| 98 | +Followup: Go to <http://docs.python.org> and find the documentation for |
| 99 | +the `abs()` function (hint: it’s found under the library reference |
| 100 | +related to built-in functions). |
| 101 | + |
| 102 | +### Cutting and Pasting |
| 103 | + |
| 104 | +As you've noticed, this course is structured as a series of traditional |
| 105 | +web pages where you are encouraged to try interactive code samples by typing them |
| 106 | +by hand. If you are learning Python for the first time, this is encouraged. |
| 107 | +You will get a better feel for the language by slowing down, typing things in, |
| 108 | +and thinking about what you are doing. |
| 109 | + |
| 110 | +If you are inclined to use "cut and paste" in the editor, select code |
| 111 | +starting after the `>>>` prompt and going up to, but not any further |
| 112 | +than the first blank line or the next `>>>` prompt (whichever appears |
| 113 | +first). Select "copy" from the brower, go to the Python window, and |
| 114 | +select "paste" to copy it into the Python shell. To get the code to |
| 115 | +run, you may have to hit "Return" once after you’ve pasted it in. |
| 116 | + |
| 117 | +Use cut-and-paste to execute the Python statements in this session: |
| 118 | + |
| 119 | +```pycon |
| 120 | +>>> 12 + 20 |
| 121 | +32 |
| 122 | +>>> (3 + 4 |
| 123 | + + 5 + 6) |
| 124 | +18 |
| 125 | +>>> for i in range(5): |
| 126 | + print(i) |
| 127 | + |
| 128 | +0 |
| 129 | +1 |
| 130 | +2 |
| 131 | +3 |
| 132 | +4 |
| 133 | +>>> |
| 134 | +``` |
| 135 | + |
| 136 | +Warning: It is never possible to paste more than one Python command |
| 137 | +(statements that appear after `>>>`) to the basic Python shell at a |
| 138 | +time. You have to paste each command one at a time. |
| 139 | + |
| 140 | +### Where is My Bus? |
| 141 | + |
| 142 | +If you’ve made it this far, try something more advanced and type these |
| 143 | +statements to find out how long people waiting on the corner of Clark |
| 144 | +street and Balmoral in Chicago will have to wait for the next |
| 145 | +northbound CTA \#22 bus: |
| 146 | + |
| 147 | +```pycon |
| 148 | +>>> import urllib.request |
| 149 | +>>> u = urllib.request.urlopen('http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=14791&route=22') |
| 150 | +>>> from xml.etree.ElementTree import parse |
| 151 | +>>> doc = parse(u) |
| 152 | +>>> for pt in doc.findall('.//pt'): |
| 153 | + print(pt.text) |
| 154 | + |
| 155 | +6 MIN |
| 156 | +18 MIN |
| 157 | +28 MIN |
| 158 | +>>> |
| 159 | +``` |
| 160 | + |
| 161 | +Yes, you just downloaded a web page, parsed an XML document, and |
| 162 | +extracted some useful information in about 6 lines of code. The data |
| 163 | +you accessed is actually feeding the website |
| 164 | +<http://ctabustracker.com/bustime/home.jsp>. Try it again and watch |
| 165 | +the predictions change. |
| 166 | + |
| 167 | +If the first import statement `import urllib.request` fails, you’re |
| 168 | +probably using Python 2. For this course, you need to make sure you’re |
| 169 | +using Python 3.6 or newer. Go to <https://www.python.org> to download |
| 170 | +it if you need it. |
| 171 | + |
| 172 | +If your work environment requires the use of an HTTP proxy server, you may need |
| 173 | +to set the `HTTP_PROXY` environment variable to make this part of the |
| 174 | +exercise work. For example: |
| 175 | + |
| 176 | +```pycon |
| 177 | +>>> import os |
| 178 | +>>> os.environ['HTTP_PROXY'] = 'http://yourproxy.server.com' |
| 179 | +>>> |
| 180 | +``` |
| 181 | + |
| 182 | +\[ **[Solution](soln1_1.html)** | **[Next](ex1_2.html)** | |
| 183 | +**[Index](index.html)** \] |
| 184 | + |
0 commit comments