|
11 | 11 | "cell_type": "markdown", |
12 | 12 | "metadata": {}, |
13 | 13 | "source": [ |
14 | | - "A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets." |
| 14 | + "Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed (immutable) unlike lists (mutable). <br> Tuples use parentheses, whereas lists use square brackets." |
15 | 15 | ] |
16 | 16 | }, |
17 | 17 | { |
|
23 | 23 | }, |
24 | 24 | { |
25 | 25 | "cell_type": "code", |
26 | | - "execution_count": 5, |
27 | | - "metadata": { |
28 | | - "collapsed": true |
29 | | - }, |
| 26 | + "execution_count": 17, |
| 27 | + "metadata": {}, |
30 | 28 | "outputs": [], |
31 | 29 | "source": [ |
32 | 30 | "# empty tuple written as two parentheses containing nothing\n", |
33 | | - "tup1 = (); " |
| 31 | + "tup1 = (); \n", |
| 32 | + "\n", |
| 33 | + "# empty list\n", |
| 34 | + "list1 = []" |
34 | 35 | ] |
35 | 36 | }, |
36 | 37 | { |
37 | 38 | "cell_type": "code", |
38 | | - "execution_count": 6, |
39 | | - "metadata": { |
40 | | - "collapsed": true |
41 | | - }, |
42 | | - "outputs": [], |
| 39 | + "execution_count": 84, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "data": { |
| 44 | + "text/plain": [ |
| 45 | + "tuple" |
| 46 | + ] |
| 47 | + }, |
| 48 | + "execution_count": 84, |
| 49 | + "metadata": {}, |
| 50 | + "output_type": "execute_result" |
| 51 | + } |
| 52 | + ], |
43 | 53 | "source": [ |
44 | 54 | "# To write a tuple containing a single value you have to include a comma, even though there is only one value\n", |
45 | | - "tup1 = (50,);" |
| 55 | + "tup1 = 50, ;\n", |
| 56 | + "type(tup1)" |
46 | 57 | ] |
47 | 58 | }, |
48 | 59 | { |
49 | 60 | "cell_type": "code", |
50 | | - "execution_count": 22, |
51 | | - "metadata": { |
52 | | - "collapsed": true |
53 | | - }, |
54 | | - "outputs": [], |
| 61 | + "execution_count": 85, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [ |
| 64 | + { |
| 65 | + "name": "stdout", |
| 66 | + "output_type": "stream", |
| 67 | + "text": [ |
| 68 | + "<type 'tuple'>\n" |
| 69 | + ] |
| 70 | + } |
| 71 | + ], |
55 | 72 | "source": [ |
56 | 73 | "tup1 = 'Please', 'Subscribe';\n", |
| 74 | + "print(type(tup1))\n", |
57 | 75 | "tup2 = ('pretty please', );\n", |
58 | 76 | "tup3 = \"a\", \"b\", \"c\", \"d\";" |
59 | 77 | ] |
|
84 | 102 | }, |
85 | 103 | { |
86 | 104 | "cell_type": "code", |
87 | | - "execution_count": 24, |
88 | | - "metadata": { |
89 | | - "collapsed": false |
90 | | - }, |
| 105 | + "execution_count": 75, |
| 106 | + "metadata": {}, |
91 | 107 | "outputs": [ |
92 | 108 | { |
93 | 109 | "data": { |
94 | 110 | "text/plain": [ |
95 | 111 | "('a', 'b', 'c', 'd')" |
96 | 112 | ] |
97 | 113 | }, |
98 | | - "execution_count": 24, |
| 114 | + "execution_count": 75, |
99 | 115 | "metadata": {}, |
100 | 116 | "output_type": "execute_result" |
101 | 117 | } |
|
107 | 123 | { |
108 | 124 | "cell_type": "code", |
109 | 125 | "execution_count": 25, |
110 | | - "metadata": { |
111 | | - "collapsed": false |
112 | | - }, |
| 126 | + "metadata": {}, |
113 | 127 | "outputs": [ |
114 | 128 | { |
115 | 129 | "data": { |
|
130 | 144 | }, |
131 | 145 | { |
132 | 146 | "cell_type": "code", |
133 | | - "execution_count": 26, |
134 | | - "metadata": { |
135 | | - "collapsed": false |
136 | | - }, |
| 147 | + "execution_count": 12, |
| 148 | + "metadata": {}, |
137 | 149 | "outputs": [ |
138 | 150 | { |
139 | 151 | "data": { |
140 | 152 | "text/plain": [ |
141 | 153 | "('a', 'b', 'c')" |
142 | 154 | ] |
143 | 155 | }, |
144 | | - "execution_count": 26, |
| 156 | + "execution_count": 12, |
145 | 157 | "metadata": {}, |
146 | 158 | "output_type": "execute_result" |
147 | 159 | } |
|
153 | 165 | }, |
154 | 166 | { |
155 | 167 | "cell_type": "code", |
156 | | - "execution_count": 27, |
157 | | - "metadata": { |
158 | | - "collapsed": false |
159 | | - }, |
| 168 | + "execution_count": 13, |
| 169 | + "metadata": {}, |
160 | 170 | "outputs": [ |
161 | 171 | { |
162 | 172 | "data": { |
163 | 173 | "text/plain": [ |
164 | 174 | "('b', 'c', 'd')" |
165 | 175 | ] |
166 | 176 | }, |
167 | | - "execution_count": 27, |
| 177 | + "execution_count": 13, |
168 | 178 | "metadata": {}, |
169 | 179 | "output_type": "execute_result" |
170 | 180 | } |
|
177 | 187 | { |
178 | 188 | "cell_type": "code", |
179 | 189 | "execution_count": 35, |
180 | | - "metadata": { |
181 | | - "collapsed": false |
182 | | - }, |
| 190 | + "metadata": {}, |
183 | 191 | "outputs": [ |
184 | 192 | { |
185 | 193 | "data": { |
|
213 | 221 | }, |
214 | 222 | { |
215 | 223 | "cell_type": "code", |
216 | | - "execution_count": 36, |
217 | | - "metadata": { |
218 | | - "collapsed": false |
219 | | - }, |
| 224 | + "execution_count": 86, |
| 225 | + "metadata": {}, |
220 | 226 | "outputs": [ |
221 | 227 | { |
222 | 228 | "data": { |
223 | 229 | "text/plain": [ |
224 | 230 | "('Please', 'Subscribe')" |
225 | 231 | ] |
226 | 232 | }, |
227 | | - "execution_count": 36, |
| 233 | + "execution_count": 86, |
228 | 234 | "metadata": {}, |
229 | 235 | "output_type": "execute_result" |
230 | 236 | } |
|
235 | 241 | }, |
236 | 242 | { |
237 | 243 | "cell_type": "code", |
238 | | - "execution_count": 37, |
239 | | - "metadata": { |
240 | | - "collapsed": false |
241 | | - }, |
| 244 | + "execution_count": 87, |
| 245 | + "metadata": {}, |
242 | 246 | "outputs": [ |
243 | 247 | { |
244 | 248 | "data": { |
245 | 249 | "text/plain": [ |
246 | 250 | "('pretty please',)" |
247 | 251 | ] |
248 | 252 | }, |
249 | | - "execution_count": 37, |
| 253 | + "execution_count": 87, |
250 | 254 | "metadata": {}, |
251 | 255 | "output_type": "execute_result" |
252 | 256 | } |
|
257 | 261 | }, |
258 | 262 | { |
259 | 263 | "cell_type": "code", |
260 | | - "execution_count": 38, |
261 | | - "metadata": { |
262 | | - "collapsed": false |
263 | | - }, |
| 264 | + "execution_count": 88, |
| 265 | + "metadata": {}, |
264 | 266 | "outputs": [ |
265 | 267 | { |
266 | 268 | "name": "stdout", |
|
296 | 298 | "cell_type": "markdown", |
297 | 299 | "metadata": {}, |
298 | 300 | "source": [ |
299 | | - "tuples are useful for sequence unpacking <br>\n", |
300 | | - "if you some data you know you dont want to change, use tuples <br>\n", |
301 | | - "generated faster and iterated faster than a python list" |
| 301 | + "tuples are useful for sequence unpacking <br>" |
302 | 302 | ] |
303 | 303 | }, |
304 | 304 | { |
305 | 305 | "cell_type": "code", |
306 | | - "execution_count": 41, |
307 | | - "metadata": { |
308 | | - "collapsed": false |
309 | | - }, |
| 306 | + "execution_count": 92, |
| 307 | + "metadata": {}, |
310 | 308 | "outputs": [ |
311 | 309 | { |
312 | 310 | "name": "stdout", |
313 | 311 | "output_type": "stream", |
314 | 312 | "text": [ |
315 | | - "Value of x , y : 7 10\n" |
| 313 | + "('Value of x , y : ', 7, 10)\n" |
316 | 314 | ] |
317 | 315 | } |
318 | 316 | ], |
319 | 317 | "source": [ |
320 | | - "x, y = 7, 10;\n", |
| 318 | + "x, y = (7, 10);\n", |
321 | 319 | "print(\"Value of x , y : \", x, y)" |
322 | 320 | ] |
323 | 321 | }, |
|
337 | 335 | }, |
338 | 336 | { |
339 | 337 | "cell_type": "code", |
340 | | - "execution_count": 43, |
341 | | - "metadata": { |
342 | | - "collapsed": false |
343 | | - }, |
| 338 | + "execution_count": 47, |
| 339 | + "metadata": {}, |
344 | 340 | "outputs": [ |
345 | 341 | { |
346 | 342 | "name": "stdout", |
347 | 343 | "output_type": "stream", |
348 | 344 | "text": [ |
349 | | - "0 steve\n", |
350 | | - "1 rachel\n", |
351 | | - "2 michael\n", |
352 | | - "3 adam\n", |
353 | | - "4 monica\n" |
| 345 | + "(0, 'steve')\n", |
| 346 | + "(1, 'rachel')\n", |
| 347 | + "(2, 'michael')\n", |
| 348 | + "(3, 'adam')\n", |
| 349 | + "(4, 'monica')\n" |
354 | 350 | ] |
355 | 351 | } |
356 | 352 | ], |
357 | 353 | "source": [ |
358 | | - "friends = ['steve', 'rachel', 'michael', 'adam', 'monica']\n", |
| 354 | + "friends = ('steve', 'rachel', 'michael', 'adam', 'monica')\n", |
359 | 355 | "for index, friend in enumerate(friends):\n", |
360 | 356 | " print(index,friend)" |
361 | 357 | ] |
|
371 | 367 | "cell_type": "markdown", |
372 | 368 | "metadata": {}, |
373 | 369 | "source": [ |
374 | | - "Fibonacci sequence is an integer sequence characterized by the fact that every number after the first two is the sum of the two preceding one. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1 (<b>which is how I like to code it</b>), or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two." |
| 370 | + "Fibonacci sequence is an integer sequence characterized by the fact that every number after the first two is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1 (<b>which is how I like to code it</b>), or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two." |
375 | 371 | ] |
376 | 372 | }, |
377 | 373 | { |
378 | 374 | "cell_type": "code", |
379 | 375 | "execution_count": 2, |
380 | | - "metadata": { |
381 | | - "collapsed": false |
382 | | - }, |
| 376 | + "metadata": {}, |
383 | 377 | "outputs": [ |
384 | 378 | { |
385 | 379 | "name": "stdout", |
|
402 | 396 | }, |
403 | 397 | { |
404 | 398 | "cell_type": "code", |
405 | | - "execution_count": 3, |
406 | | - "metadata": { |
407 | | - "collapsed": false |
408 | | - }, |
| 399 | + "execution_count": 24, |
| 400 | + "metadata": {}, |
409 | 401 | "outputs": [ |
410 | 402 | { |
411 | 403 | "name": "stdout", |
412 | 404 | "output_type": "stream", |
413 | 405 | "text": [ |
414 | | - "1\n", |
415 | | - "1\n", |
416 | | - "2\n", |
417 | | - "3\n", |
418 | | - "5\n", |
419 | | - "8\n", |
420 | | - "13\n", |
421 | | - "21\n", |
422 | | - "34\n", |
423 | | - "55\n" |
| 406 | + "('Fib(a): ', 1, 'b is: ', 1)\n", |
| 407 | + "('Fib(a): ', 1, 'b is: ', 2)\n", |
| 408 | + "('Fib(a): ', 2, 'b is: ', 3)\n", |
| 409 | + "('Fib(a): ', 3, 'b is: ', 5)\n", |
| 410 | + "('Fib(a): ', 5, 'b is: ', 8)\n", |
| 411 | + "('Fib(a): ', 8, 'b is: ', 13)\n", |
| 412 | + "('Fib(a): ', 13, 'b is: ', 21)\n", |
| 413 | + "('Fib(a): ', 21, 'b is: ', 34)\n", |
| 414 | + "('Fib(a): ', 34, 'b is: ', 55)\n", |
| 415 | + "('Fib(a): ', 55, 'b is: ', 89)\n" |
424 | 416 | ] |
425 | 417 | } |
426 | 418 | ], |
427 | 419 | "source": [ |
428 | 420 | "# Note, there are better ways to code this which I will go over in later videos\n", |
429 | 421 | "a,b = 1,1\n", |
430 | 422 | "for i in range(10):\n", |
431 | | - " print(a)\n", |
| 423 | + " print(\"Fib(a): \", a, \"b is: \", b)\n", |
432 | 424 | " a,b = b,a+b " |
433 | 425 | ] |
434 | 426 | }, |
|
450 | 442 | "metadata": { |
451 | 443 | "anaconda-cloud": {}, |
452 | 444 | "kernelspec": { |
453 | | - "display_name": "Python [conda env:py36]", |
| 445 | + "display_name": "Python 2", |
454 | 446 | "language": "python", |
455 | | - "name": "conda-env-py36-py" |
| 447 | + "name": "python2" |
456 | 448 | }, |
457 | 449 | "language_info": { |
458 | 450 | "codemirror_mode": { |
459 | 451 | "name": "ipython", |
460 | | - "version": 3 |
| 452 | + "version": 2 |
461 | 453 | }, |
462 | 454 | "file_extension": ".py", |
463 | 455 | "mimetype": "text/x-python", |
464 | 456 | "name": "python", |
465 | 457 | "nbconvert_exporter": "python", |
466 | | - "pygments_lexer": "ipython3", |
467 | | - "version": "3.6.1" |
| 458 | + "pygments_lexer": "ipython2", |
| 459 | + "version": "2.7.13" |
468 | 460 | } |
469 | 461 | }, |
470 | 462 | "nbformat": 4, |
|
0 commit comments