|
95 | 95 | "cell_type": "markdown", |
96 | 96 | "metadata": {}, |
97 | 97 | "source": [ |
98 | | - "You can assign a value to a variable with the <tt>=</tt> operator" |
| 98 | + "- Very commonly, when programming, we will want to assign a name to some complex expression\n", |
| 99 | + "- We can give names to values using _variables_\n", |
| 100 | + "- As the name implies, the values stored in a variable can vary\n", |
| 101 | + "- You can assign a value to a variable with the <tt>=</tt> operator\n", |
| 102 | + "- Note that this is different from mathematical equality (which we will come to later...)" |
99 | 103 | ] |
100 | 104 | }, |
101 | 105 | { |
|
173 | 177 | "cell_type": "markdown", |
174 | 178 | "metadata": {}, |
175 | 179 | "source": [ |
176 | | - "Variables can be used on the right hand side of an assignment as well" |
| 180 | + "Variables can be used on the right hand side of an assignment as well (where they will be evaluated before the value is assigned to the variable on the left hand side)" |
177 | 181 | ] |
178 | 182 | }, |
179 | 183 | { |
|
191 | 195 | "cell_type": "markdown", |
192 | 196 | "metadata": {}, |
193 | 197 | "source": [ |
194 | | - "Including the current value of a variable itself" |
| 198 | + "You can use the current value of a variable itself in an assignment" |
195 | 199 | ] |
196 | 200 | }, |
197 | 201 | { |
|
205 | 209 | "metadata": {}, |
206 | 210 | "outputs": [] |
207 | 211 | }, |
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "In fact this is such a common idion that there are special operators that will do this implicitly (more on these later)" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "code", |
| 221 | + "collapsed": false, |
| 222 | + "input": [ |
| 223 | + "y += 1\n", |
| 224 | + "y" |
| 225 | + ], |
| 226 | + "language": "python", |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [] |
| 229 | + }, |
208 | 230 | { |
209 | 231 | "cell_type": "heading", |
210 | 232 | "level": 2, |
|
217 | 239 | "cell_type": "markdown", |
218 | 240 | "metadata": {}, |
219 | 241 | "source": [ |
| 242 | + "Python (and computers in general) treats different types of data differently. Python has 5 (or possibly 6) main basic data types.\n", |
| 243 | + "\n", |
220 | 244 | "Booleans:" |
221 | 245 | ] |
222 | 246 | }, |
|
359 | 383 | "Arithmetic" |
360 | 384 | ] |
361 | 385 | }, |
| 386 | + { |
| 387 | + "cell_type": "markdown", |
| 388 | + "metadata": {}, |
| 389 | + "source": [] |
| 390 | + }, |
362 | 391 | { |
363 | 392 | "cell_type": "code", |
364 | 393 | "collapsed": false, |
|
371 | 400 | "x % y # remainder of x/y\n", |
372 | 401 | "x ** y # exponentiation\n", |
373 | 402 | "pow(x, y) # another way to do exponentiation\n", |
374 | | - "print (x - y) * 4" |
| 403 | + "print ((x + 1) - y) * 4" |
375 | 404 | ], |
376 | 405 | "language": "python", |
377 | 406 | "metadata": {}, |
|
477 | 506 | "cell_type": "markdown", |
478 | 507 | "metadata": {}, |
479 | 508 | "source": [ |
| 509 | + "As well as the basic data types we introduced above, very commonly you will want to store and operate on collections of values, and python has several _data structures_ that you can use to do this. Which one you will use will depend on what problem you are trying to solve.\n", |
| 510 | + "\n", |
480 | 511 | "__Tuples__: fixed collections of values of mixed types" |
481 | 512 | ] |
482 | 513 | }, |
|
558 | 589 | "cell_type": "markdown", |
559 | 590 | "metadata": {}, |
560 | 591 | "source": [ |
561 | | - "__Lists__: mutable collections of values, i.e. lists can be altered once built" |
| 592 | + "__Lists__: mutable collections of values, i.e. lists _can_ be altered once initialised" |
562 | 593 | ] |
563 | 594 | }, |
564 | 595 | { |
|
600 | 631 | "cell_type": "markdown", |
601 | 632 | "metadata": {}, |
602 | 633 | "source": [ |
603 | | - "Manipulating tuples and lists: these can be used in very similar ways" |
| 634 | + "__Manipulating tuples and lists:__ these two types can be used in very similar ways" |
604 | 635 | ] |
605 | 636 | }, |
606 | 637 | { |
|
872 | 903 | "cell_type": "markdown", |
873 | 904 | "metadata": {}, |
874 | 905 | "source": [ |
875 | | - "Note that both of these change the list, if you want a sorted copy of the list, use <tt>sorted</tt>" |
| 906 | + "Note that both of these change the list, if you want a sorted copy of the list while leaving the original untouched, use <tt>sorted</tt>" |
876 | 907 | ] |
877 | 908 | }, |
878 | 909 | { |
|
901 | 932 | "source": [ |
902 | 933 | "__String manipulations__\n", |
903 | 934 | "\n", |
904 | | - "Strings are a lot like tuples, and individual characters and substrings can be accessed and manipulated using similar operations\n" |
| 935 | + "Strings are a lot like tuples of characters, and individual characters and substrings can be accessed and manipulated using similar operations\n" |
905 | 936 | ] |
906 | 937 | }, |
907 | 938 | { |
|
0 commit comments