├── README.md
├── Tutorial_1_Basics
├── .ipynb_checkpoints
│ └── Programming basics-checkpoint.ipynb
└── Programming basics.ipynb
├── Tutorial_2_Loops_and_Indexing
├── .ipynb_checkpoints
│ ├── Indexing and loops-checkpoint.ipynb
│ └── Loops and indexing-checkpoint.ipynb
└── Indexing and loops.ipynb
├── Tutorial_3_Conditions_and_Functions
├── .ipynb_checkpoints
│ ├── Conditions and Functions-checkpoint.ipynb
│ ├── Untitled-checkpoint.ipynb
│ ├── bilguun-checkpoint.ipynb
│ ├── jiayi-checkpoint.ipynb
│ ├── john-checkpoint.ipynb
│ ├── katy-checkpoint.ipynb
│ └── nikki-checkpoint.ipynb
└── Conditions and Functions.ipynb
├── Tutorial_4_FileIO
├── File_input.ipynb
├── cars.csv
├── example.png
├── practice.txt
└── review.txt
└── Tutorial_5_Pythonista
├── .DS_Store
├── .ipynb_checkpoints
└── Tutorial 5 Pythonista-checkpoint.ipynb
├── Tutorial 5 Pythonista.ipynb
├── cars.csv
├── emails.txt
├── names.txt
├── practice2.txt
└── practice3.txt
/README.md:
--------------------------------------------------------------------------------
1 | # Introduction to python for data science and machine learning
2 | This repository contains some basic python tutorials, tailored specifically for people interested in data science, analytics and machine learning. The tutorials uses the Jupyter notebooks, so students can follow along the examples, and work on the exercises. The contents are beginner level, so no programming knowledge is nesssary.
3 |
4 | ## Using the tutorials without downloading
5 | You can also choose to use the tutorials online with your internet browser with this button -> [](http://mybinder.org:/repo/marshuang80/python_tutorials)
6 | Note that your code **will not be saved** using Binder
7 |
8 | ## Using the tutorials by saving locally
9 | ### Installation Requirements
10 | Please make sure you have python installed on your system. The **Anaconda** data science platform is highly recommended:
11 | **https://www.continuum.io/downloads**
12 | ### Downalod or clone the repository
13 | You will first have to clone the tutorials to your local directory from your terminal:
14 | ```
15 | $ git clone https://github.com/marshuang80/Python_workshop.git
16 | ```
17 | You can also choose to download the repository directly from **github**. Please find the *Clone or Download* button above.
18 | To open the tutorials, you will need to either open the **jupyter notebook** from the **anaconda navigator**, or use your terminal:
19 | ```
20 | $ jupyter notebook
21 | ```
22 |
23 | **UBIC@UCSD**
24 |
--------------------------------------------------------------------------------
/Tutorial_1_Basics/.ipynb_checkpoints/Programming basics-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 1"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "
\n",
16 | "## Jupyter notebook: "
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "
\n",
24 | "Jupyter notebooks are made up by \"cells\", which are the boxes you see below\n",
25 | "\n",
26 | "Each cell has an input region, which are labeled by **In [ ] ** on the left\n",
27 | "\n",
28 | "Press Shift+Enter to run cell\n",
29 | "\n",
30 | "The outputs are printed in the output region, labeled by **Out[ ]** "
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 2,
36 | "metadata": {
37 | "collapsed": true
38 | },
39 | "outputs": [
40 | {
41 | "data": {
42 | "text/plain": [
43 | "2"
44 | ]
45 | },
46 | "execution_count": 2,
47 | "metadata": {},
48 | "output_type": "execute_result"
49 | }
50 | ],
51 | "source": [
52 | "1+1"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "Here are some useful Jupyter notebook tips and tricks\n",
60 | "\n",
61 | "https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "
\n",
69 | "## Variable and types\n",
70 | "All programming languages have variables, which are values that can be changed.\n",
71 | "\n",
72 | "All valriables have a type, inclusing:
\n",
73 | "**char:** characters *(eg: \"a\",\"b\",\"c\",\"$\")*
\n",
74 | "**str:** words or sentences mades up by chars *(eg: \"hello world\")*
\n",
75 | "**int:** interger *(eg: 1)*
\n",
76 | "**float:** numbers with decimals *(eg: 1.00)*
\n",
77 | "**bool:** True or False"
78 | ]
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | "
\n",
85 | "Use **type( )**, with variable within the parenthesis to return the object's type."
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 1,
91 | "metadata": {
92 | "collapsed": true
93 | },
94 | "outputs": [
95 | {
96 | "data": {
97 | "text/plain": [
98 | "str"
99 | ]
100 | },
101 | "execution_count": 1,
102 | "metadata": {},
103 | "output_type": "execute_result"
104 | }
105 | ],
106 | "source": [
107 | "type('Hello World!')"
108 | ]
109 | },
110 | {
111 | "cell_type": "markdown",
112 | "metadata": {},
113 | "source": [
114 | "All **char** and **string** has to be surrounded by single or double quote, or else an error will occur"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": 3,
120 | "metadata": {
121 | "collapsed": true
122 | },
123 | "outputs": [
124 | {
125 | "ename": "SyntaxError",
126 | "evalue": "invalid syntax (, line 1)",
127 | "output_type": "error",
128 | "traceback": [
129 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m type(Hellow World)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
130 | ]
131 | }
132 | ],
133 | "source": [
134 | "type(Hellow World)"
135 | ]
136 | },
137 | {
138 | "cell_type": "markdown",
139 | "metadata": {},
140 | "source": [
141 | "Here are examples of other types of variables"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": 6,
147 | "metadata": {
148 | "collapsed": true
149 | },
150 | "outputs": [
151 | {
152 | "data": {
153 | "text/plain": [
154 | "NoneType"
155 | ]
156 | },
157 | "execution_count": 6,
158 | "metadata": {},
159 | "output_type": "execute_result"
160 | }
161 | ],
162 | "source": [
163 | "type(None)"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {
170 | "collapsed": false
171 | },
172 | "outputs": [],
173 | "source": [
174 | "type(1000000)"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": null,
180 | "metadata": {
181 | "collapsed": false
182 | },
183 | "outputs": [],
184 | "source": [
185 | "type(-20.0)"
186 | ]
187 | },
188 | {
189 | "cell_type": "markdown",
190 | "metadata": {},
191 | "source": [
192 | "All variables can also be assigned with a name, and be reused later on\n"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": null,
198 | "metadata": {
199 | "collapsed": true
200 | },
201 | "outputs": [],
202 | "source": [
203 | "a = 1\n",
204 | "b = 2"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Variable names have to start with a chacter, and you cannot use a python syntax as a name"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 6,
217 | "metadata": {
218 | "collapsed": true
219 | },
220 | "outputs": [
221 | {
222 | "ename": "SyntaxError",
223 | "evalue": "invalid syntax (, line 1)",
224 | "output_type": "error",
225 | "traceback": [
226 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m $ = 1\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
227 | ]
228 | }
229 | ],
230 | "source": [
231 | "$ = 1"
232 | ]
233 | },
234 | {
235 | "cell_type": "code",
236 | "execution_count": null,
237 | "metadata": {
238 | "collapsed": true
239 | },
240 | "outputs": [],
241 | "source": [
242 | "list = 1"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "You can do basic mathematics with python"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": null,
255 | "metadata": {
256 | "collapsed": true
257 | },
258 | "outputs": [],
259 | "source": [
260 | "1 + 1"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {
267 | "collapsed": true
268 | },
269 | "outputs": [],
270 | "source": [
271 | "a = 1\n",
272 | "b = 2\n",
273 | "a + b"
274 | ]
275 | },
276 | {
277 | "cell_type": "markdown",
278 | "metadata": {},
279 | "source": [
280 | "String or character variables cannot be used in calculations. \n",
281 | "\n",
282 | "Can you calculate sushi divided by elephant? That does not make any sense because they are different types"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": null,
288 | "metadata": {
289 | "collapsed": true
290 | },
291 | "outputs": [],
292 | "source": [
293 | "a = 'apple'\n",
294 | "b = 1 \n",
295 | "a + b"
296 | ]
297 | },
298 | {
299 | "cell_type": "markdown",
300 | "metadata": {},
301 | "source": [
302 | "To print a variable, use the print function: \n",
303 | "\n",
304 | "**print(** *variables* **)**"
305 | ]
306 | },
307 | {
308 | "cell_type": "code",
309 | "execution_count": null,
310 | "metadata": {
311 | "collapsed": true
312 | },
313 | "outputs": [],
314 | "source": [
315 | "x = \"hi\"\n",
316 | "y = \"bye\"\n",
317 | "print(x)\n",
318 | "print(y)"
319 | ]
320 | },
321 | {
322 | "cell_type": "markdown",
323 | "metadata": {},
324 | "source": [
325 | "
\n",
326 | "# Data structures\n",
327 | "\n",
328 | "Think of data strucutres a containters (*structure*) that can hold many variables (*data*)\n",
329 | "\n",
330 | "There are a few data structures in python, each with thier own properties and ways to store data\n",
331 | "\n",
332 | "### **Tuples** are immutable data structures that cannot be altered\n",
333 | "\n",
334 | "You can decare a tuple with the syntax: \n",
335 | "\n",
336 | "**(** *data1* **,** *data2* ..... **)** "
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 10,
342 | "metadata": {
343 | "collapsed": true
344 | },
345 | "outputs": [
346 | {
347 | "data": {
348 | "text/plain": [
349 | "tuple"
350 | ]
351 | },
352 | "execution_count": 10,
353 | "metadata": {},
354 | "output_type": "execute_result"
355 | }
356 | ],
357 | "source": [
358 | "x = (1, 'a', 2, 'b')\n",
359 | "type(x)"
360 | ]
361 | },
362 | {
363 | "cell_type": "markdown",
364 | "metadata": {},
365 | "source": [
366 | "
\n",
367 | "### **Lists** are a mutable data structure.\n",
368 | "\n",
369 | "Declare using syntax: \n",
370 | "\n",
371 | "**[** *data1* **,** *data2* ..... **]** "
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": 16,
377 | "metadata": {
378 | "collapsed": true
379 | },
380 | "outputs": [
381 | {
382 | "data": {
383 | "text/plain": [
384 | "list"
385 | ]
386 | },
387 | "execution_count": 16,
388 | "metadata": {},
389 | "output_type": "execute_result"
390 | }
391 | ],
392 | "source": [
393 | "y = [1, 'a', 2, 'b']\n",
394 | "type(y)"
395 | ]
396 | },
397 | {
398 | "cell_type": "markdown",
399 | "metadata": {},
400 | "source": [
401 | "You can access the certain elements in a list by using it's index number\n",
402 | "\n",
403 | "python uses zero indexing (starts with 0)\n",
404 | "\n",
405 | "*list_name* **[** *index_number* **]**"
406 | ]
407 | },
408 | {
409 | "cell_type": "code",
410 | "execution_count": 17,
411 | "metadata": {
412 | "collapsed": true
413 | },
414 | "outputs": [
415 | {
416 | "data": {
417 | "text/plain": [
418 | "'a'"
419 | ]
420 | },
421 | "execution_count": 17,
422 | "metadata": {},
423 | "output_type": "execute_result"
424 | }
425 | ],
426 | "source": [
427 | "y[1]"
428 | ]
429 | },
430 | {
431 | "cell_type": "markdown",
432 | "metadata": {},
433 | "source": [
434 | "A list can also contain other lists"
435 | ]
436 | },
437 | {
438 | "cell_type": "code",
439 | "execution_count": null,
440 | "metadata": {
441 | "collapsed": true
442 | },
443 | "outputs": [],
444 | "source": [
445 | "yy = [[1,2,3],[4,5,6],[7,8,9]]\n",
446 | "print(yy)"
447 | ]
448 | },
449 | {
450 | "cell_type": "markdown",
451 | "metadata": {},
452 | "source": [
453 | "
\n",
454 | "Use `append` to add an object to a list."
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "execution_count": 10,
460 | "metadata": {
461 | "collapsed": true
462 | },
463 | "outputs": [
464 | {
465 | "ename": "NameError",
466 | "evalue": "name 'x' is not defined",
467 | "output_type": "error",
468 | "traceback": [
469 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
470 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
471 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
472 | "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
473 | ]
474 | }
475 | ],
476 | "source": [
477 | "y.append(3.3)\n",
478 | "print(x)"
479 | ]
480 | },
481 | {
482 | "cell_type": "markdown",
483 | "metadata": {},
484 | "source": [
485 | "As mentioned above, tuples are immutable, so you cannot append anything after declaration"
486 | ]
487 | },
488 | {
489 | "cell_type": "code",
490 | "execution_count": 11,
491 | "metadata": {
492 | "collapsed": true
493 | },
494 | "outputs": [
495 | {
496 | "ename": "AttributeError",
497 | "evalue": "'tuple' object has no attribute 'append'",
498 | "output_type": "error",
499 | "traceback": [
500 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
501 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
502 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
503 | "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
504 | ]
505 | }
506 | ],
507 | "source": [
508 | "x.append(3.3)"
509 | ]
510 | },
511 | {
512 | "cell_type": "markdown",
513 | "metadata": {},
514 | "source": [
515 | "
\n",
516 | "Use `+` to concatenate lists."
517 | ]
518 | },
519 | {
520 | "cell_type": "code",
521 | "execution_count": 8,
522 | "metadata": {
523 | "collapsed": true
524 | },
525 | "outputs": [
526 | {
527 | "data": {
528 | "text/plain": [
529 | "[1, 2, 3, 4]"
530 | ]
531 | },
532 | "execution_count": 8,
533 | "metadata": {},
534 | "output_type": "execute_result"
535 | }
536 | ],
537 | "source": [
538 | "[1,2] + [3,4]"
539 | ]
540 | },
541 | {
542 | "cell_type": "markdown",
543 | "metadata": {},
544 | "source": [
545 | "
\n",
546 | "Use `*` to repeat lists."
547 | ]
548 | },
549 | {
550 | "cell_type": "code",
551 | "execution_count": 9,
552 | "metadata": {
553 | "collapsed": true
554 | },
555 | "outputs": [
556 | {
557 | "data": {
558 | "text/plain": [
559 | "[1, 1, 1]"
560 | ]
561 | },
562 | "execution_count": 9,
563 | "metadata": {},
564 | "output_type": "execute_result"
565 | }
566 | ],
567 | "source": [
568 | "[1]*3"
569 | ]
570 | },
571 | {
572 | "cell_type": "markdown",
573 | "metadata": {
574 | "collapsed": false
575 | },
576 | "source": [
577 | "Use **len** to find how many elements are in a list"
578 | ]
579 | },
580 | {
581 | "cell_type": "code",
582 | "execution_count": 19,
583 | "metadata": {
584 | "collapsed": true
585 | },
586 | "outputs": [
587 | {
588 | "data": {
589 | "text/plain": [
590 | "21"
591 | ]
592 | },
593 | "execution_count": 19,
594 | "metadata": {},
595 | "output_type": "execute_result"
596 | }
597 | ],
598 | "source": [
599 | "x = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]\n",
600 | "len(x)"
601 | ]
602 | },
603 | {
604 | "cell_type": "markdown",
605 | "metadata": {},
606 | "source": [
607 | "
\n",
608 | "Use the `in` operator to check if something is inside a list."
609 | ]
610 | },
611 | {
612 | "cell_type": "code",
613 | "execution_count": 12,
614 | "metadata": {
615 | "collapsed": true
616 | },
617 | "outputs": [
618 | {
619 | "data": {
620 | "text/plain": [
621 | "True"
622 | ]
623 | },
624 | "execution_count": 12,
625 | "metadata": {},
626 | "output_type": "execute_result"
627 | }
628 | ],
629 | "source": [
630 | "1 in [1, 2, 3]"
631 | ]
632 | },
633 | {
634 | "cell_type": "markdown",
635 | "metadata": {},
636 | "source": [
637 | "### **sets** are unordered data strucutres that dont take repeats\n",
638 | "\n",
639 | "You can declare a tuple with the syntax: \n",
640 | "\n",
641 | "**{** *data1* **,** *data2* ..... **}** "
642 | ]
643 | },
644 | {
645 | "cell_type": "code",
646 | "execution_count": 13,
647 | "metadata": {
648 | "collapsed": true
649 | },
650 | "outputs": [
651 | {
652 | "name": "stdout",
653 | "output_type": "stream",
654 | "text": [
655 | "{1, 2, 3}\n"
656 | ]
657 | }
658 | ],
659 | "source": [
660 | "z = {1,1,1,2,3}\n",
661 | "print(z)"
662 | ]
663 | },
664 | {
665 | "cell_type": "markdown",
666 | "metadata": {},
667 | "source": [
668 | "You can remove duplicates in a list by converting it to a set"
669 | ]
670 | },
671 | {
672 | "cell_type": "code",
673 | "execution_count": 14,
674 | "metadata": {
675 | "collapsed": true
676 | },
677 | "outputs": [
678 | {
679 | "data": {
680 | "text/plain": [
681 | "{1, 2, 3}"
682 | ]
683 | },
684 | "execution_count": 14,
685 | "metadata": {},
686 | "output_type": "execute_result"
687 | }
688 | ],
689 | "source": [
690 | "a = [1,1,1,2,2,2,3,3,3]\n",
691 | "set(a)"
692 | ]
693 | },
694 | {
695 | "cell_type": "markdown",
696 | "metadata": {},
697 | "source": [
698 | "### **dictionaries** pairs a key to it's values\n",
699 | "\n",
700 | "You can declare a dictionary using:\n",
701 | "\n",
702 | "**{** *key* **:** *value* **,** ..... **}** "
703 | ]
704 | },
705 | {
706 | "cell_type": "code",
707 | "execution_count": 17,
708 | "metadata": {
709 | "collapsed": true
710 | },
711 | "outputs": [
712 | {
713 | "data": {
714 | "text/plain": [
715 | "'brooksch@umich.edu'"
716 | ]
717 | },
718 | "execution_count": 17,
719 | "metadata": {},
720 | "output_type": "execute_result"
721 | }
722 | ],
723 | "source": [
724 | "x = {'Mars Huang': 'marshuang80@gmail.com', 'Bill Gates': 'billg@microsoft.com'}\n",
725 | "x['Mars Huang'] # Retrieve a value by using the indexing operator\n"
726 | ]
727 | },
728 | {
729 | "cell_type": "code",
730 | "execution_count": null,
731 | "metadata": {
732 | "collapsed": true
733 | },
734 | "outputs": [],
735 | "source": [
736 | "x['Tony Stark'] = None\n",
737 | "x['Tony Stark']"
738 | ]
739 | },
740 | {
741 | "cell_type": "markdown",
742 | "metadata": {},
743 | "source": [
744 | "You can set any types as a dictionary value, including lists"
745 | ]
746 | },
747 | {
748 | "cell_type": "code",
749 | "execution_count": null,
750 | "metadata": {
751 | "collapsed": true
752 | },
753 | "outputs": [],
754 | "source": [
755 | "dictionary = {'list1': [1,2,3,4,5], 'list2': [100,200,300,400,500]}\n",
756 | "dictionary['list1']"
757 | ]
758 | }
759 | ],
760 | "metadata": {
761 | "anaconda-cloud": {},
762 | "kernelspec": {
763 | "display_name": "Python [default]",
764 | "language": "python",
765 | "name": "python3"
766 | },
767 | "language_info": {
768 | "codemirror_mode": {
769 | "name": "ipython",
770 | "version": 3
771 | },
772 | "file_extension": ".py",
773 | "mimetype": "text/x-python",
774 | "name": "python",
775 | "nbconvert_exporter": "python",
776 | "pygments_lexer": "ipython3",
777 | "version": "3.5.2"
778 | }
779 | },
780 | "nbformat": 4,
781 | "nbformat_minor": 0
782 | }
783 |
--------------------------------------------------------------------------------
/Tutorial_1_Basics/Programming basics.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 1"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "
\n",
16 | "## Jupyter notebook: "
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "
\n",
24 | "Jupyter notebooks are made up by \"cells\", which are the boxes you see below\n",
25 | "\n",
26 | "Each cell has an input region, which are labeled by **In [ ] ** on the left\n",
27 | "\n",
28 | "Press Shift+Enter to run cell\n",
29 | "\n",
30 | "The outputs are printed in the output region, labeled by **Out[ ]** "
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 2,
36 | "metadata": {
37 | "collapsed": true
38 | },
39 | "outputs": [
40 | {
41 | "data": {
42 | "text/plain": [
43 | "2"
44 | ]
45 | },
46 | "execution_count": 2,
47 | "metadata": {},
48 | "output_type": "execute_result"
49 | }
50 | ],
51 | "source": [
52 | "1+1"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "Here are some useful Jupyter notebook tips and tricks\n",
60 | "\n",
61 | "https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/"
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "
\n",
69 | "## Variable and types\n",
70 | "All programming languages have variables, which are values that can be changed.\n",
71 | "\n",
72 | "All valriables have a type, inclusing:
\n",
73 | "**char:** characters *(eg: \"a\",\"b\",\"c\",\"$\")*
\n",
74 | "**str:** words or sentences mades up by chars *(eg: \"hello world\")*
\n",
75 | "**int:** interger *(eg: 1)*
\n",
76 | "**float:** numbers with decimals *(eg: 1.00)*
\n",
77 | "**bool:** True or False"
78 | ]
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | "
\n",
85 | "Use **type( )**, with variable within the parenthesis to return the object's type."
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 1,
91 | "metadata": {
92 | "collapsed": true
93 | },
94 | "outputs": [
95 | {
96 | "data": {
97 | "text/plain": [
98 | "str"
99 | ]
100 | },
101 | "execution_count": 1,
102 | "metadata": {},
103 | "output_type": "execute_result"
104 | }
105 | ],
106 | "source": [
107 | "type('Hello World!')"
108 | ]
109 | },
110 | {
111 | "cell_type": "markdown",
112 | "metadata": {},
113 | "source": [
114 | "All **char** and **string** has to be surrounded by single or double quote, or else an error will occur"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": 3,
120 | "metadata": {
121 | "collapsed": true
122 | },
123 | "outputs": [
124 | {
125 | "ename": "SyntaxError",
126 | "evalue": "invalid syntax (, line 1)",
127 | "output_type": "error",
128 | "traceback": [
129 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m type(Hellow World)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
130 | ]
131 | }
132 | ],
133 | "source": [
134 | "type(Hellow World)"
135 | ]
136 | },
137 | {
138 | "cell_type": "markdown",
139 | "metadata": {},
140 | "source": [
141 | "Here are examples of other types of variables"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": 6,
147 | "metadata": {
148 | "collapsed": true
149 | },
150 | "outputs": [
151 | {
152 | "data": {
153 | "text/plain": [
154 | "NoneType"
155 | ]
156 | },
157 | "execution_count": 6,
158 | "metadata": {},
159 | "output_type": "execute_result"
160 | }
161 | ],
162 | "source": [
163 | "type(None)"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {
170 | "collapsed": false
171 | },
172 | "outputs": [],
173 | "source": [
174 | "type(1000000)"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": null,
180 | "metadata": {
181 | "collapsed": false
182 | },
183 | "outputs": [],
184 | "source": [
185 | "type(-20.0)"
186 | ]
187 | },
188 | {
189 | "cell_type": "markdown",
190 | "metadata": {},
191 | "source": [
192 | "All variables can also be assigned with a name, and be reused later on\n"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": null,
198 | "metadata": {
199 | "collapsed": true
200 | },
201 | "outputs": [],
202 | "source": [
203 | "a = 1\n",
204 | "b = 2"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Variable names have to start with a chacter, and you cannot use a python syntax as a name"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 6,
217 | "metadata": {
218 | "collapsed": true
219 | },
220 | "outputs": [
221 | {
222 | "ename": "SyntaxError",
223 | "evalue": "invalid syntax (, line 1)",
224 | "output_type": "error",
225 | "traceback": [
226 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m $ = 1\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
227 | ]
228 | }
229 | ],
230 | "source": [
231 | "$ = 1"
232 | ]
233 | },
234 | {
235 | "cell_type": "code",
236 | "execution_count": null,
237 | "metadata": {
238 | "collapsed": true
239 | },
240 | "outputs": [],
241 | "source": [
242 | "list = 1"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "You can do basic mathematics with python"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": null,
255 | "metadata": {
256 | "collapsed": true
257 | },
258 | "outputs": [],
259 | "source": [
260 | "1 + 1"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {
267 | "collapsed": true
268 | },
269 | "outputs": [],
270 | "source": [
271 | "a = 1\n",
272 | "b = 2\n",
273 | "a + b"
274 | ]
275 | },
276 | {
277 | "cell_type": "markdown",
278 | "metadata": {},
279 | "source": [
280 | "String or character variables cannot be used in calculations. \n",
281 | "\n",
282 | "Can you calculate sushi divided by elephant? That does not make any sense because they are different types"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "execution_count": null,
288 | "metadata": {
289 | "collapsed": true
290 | },
291 | "outputs": [],
292 | "source": [
293 | "a = 'apple'\n",
294 | "b = 1 \n",
295 | "a + b"
296 | ]
297 | },
298 | {
299 | "cell_type": "markdown",
300 | "metadata": {},
301 | "source": [
302 | "To print a variable, use the print function: \n",
303 | "\n",
304 | "**print(** *variables* **)**"
305 | ]
306 | },
307 | {
308 | "cell_type": "code",
309 | "execution_count": null,
310 | "metadata": {
311 | "collapsed": true
312 | },
313 | "outputs": [],
314 | "source": [
315 | "x = \"hi\"\n",
316 | "y = \"bye\"\n",
317 | "print(x)\n",
318 | "print(y)"
319 | ]
320 | },
321 | {
322 | "cell_type": "markdown",
323 | "metadata": {},
324 | "source": [
325 | "
\n",
326 | "# Data structures\n",
327 | "\n",
328 | "Think of data strucutres a containters (*structure*) that can hold many variables (*data*)\n",
329 | "\n",
330 | "There are a few data structures in python, each with thier own properties and ways to store data\n",
331 | "\n",
332 | "### **Tuples** are immutable data structures that cannot be altered\n",
333 | "\n",
334 | "You can decare a tuple with the syntax: \n",
335 | "\n",
336 | "**(** *data1* **,** *data2* ..... **)** "
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 10,
342 | "metadata": {
343 | "collapsed": true
344 | },
345 | "outputs": [
346 | {
347 | "data": {
348 | "text/plain": [
349 | "tuple"
350 | ]
351 | },
352 | "execution_count": 10,
353 | "metadata": {},
354 | "output_type": "execute_result"
355 | }
356 | ],
357 | "source": [
358 | "x = (1, 'a', 2, 'b')\n",
359 | "type(x)"
360 | ]
361 | },
362 | {
363 | "cell_type": "markdown",
364 | "metadata": {},
365 | "source": [
366 | "
\n",
367 | "### **Lists** are a mutable data structure.\n",
368 | "\n",
369 | "Declare using syntax: \n",
370 | "\n",
371 | "**[** *data1* **,** *data2* ..... **]** "
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": 16,
377 | "metadata": {
378 | "collapsed": true
379 | },
380 | "outputs": [
381 | {
382 | "data": {
383 | "text/plain": [
384 | "list"
385 | ]
386 | },
387 | "execution_count": 16,
388 | "metadata": {},
389 | "output_type": "execute_result"
390 | }
391 | ],
392 | "source": [
393 | "y = [1, 'a', 2, 'b']\n",
394 | "type(y)"
395 | ]
396 | },
397 | {
398 | "cell_type": "markdown",
399 | "metadata": {},
400 | "source": [
401 | "You can access the certain elements in a list by using it's index number\n",
402 | "\n",
403 | "python uses zero indexing (starts with 0)\n",
404 | "\n",
405 | "*list_name* **[** *index_number* **]**"
406 | ]
407 | },
408 | {
409 | "cell_type": "code",
410 | "execution_count": 17,
411 | "metadata": {
412 | "collapsed": true
413 | },
414 | "outputs": [
415 | {
416 | "data": {
417 | "text/plain": [
418 | "'a'"
419 | ]
420 | },
421 | "execution_count": 17,
422 | "metadata": {},
423 | "output_type": "execute_result"
424 | }
425 | ],
426 | "source": [
427 | "y[1]"
428 | ]
429 | },
430 | {
431 | "cell_type": "markdown",
432 | "metadata": {},
433 | "source": [
434 | "A list can also contain other lists"
435 | ]
436 | },
437 | {
438 | "cell_type": "code",
439 | "execution_count": null,
440 | "metadata": {
441 | "collapsed": true
442 | },
443 | "outputs": [],
444 | "source": [
445 | "yy = [[1,2,3],[4,5,6],[7,8,9]]\n",
446 | "print(yy)"
447 | ]
448 | },
449 | {
450 | "cell_type": "markdown",
451 | "metadata": {},
452 | "source": [
453 | "
\n",
454 | "Use `append` to add an object to a list."
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "execution_count": 10,
460 | "metadata": {
461 | "collapsed": true
462 | },
463 | "outputs": [
464 | {
465 | "ename": "NameError",
466 | "evalue": "name 'x' is not defined",
467 | "output_type": "error",
468 | "traceback": [
469 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
470 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
471 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
472 | "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
473 | ]
474 | }
475 | ],
476 | "source": [
477 | "y.append(3.3)\n",
478 | "print(x)"
479 | ]
480 | },
481 | {
482 | "cell_type": "markdown",
483 | "metadata": {},
484 | "source": [
485 | "As mentioned above, tuples are immutable, so you cannot append anything after declaration"
486 | ]
487 | },
488 | {
489 | "cell_type": "code",
490 | "execution_count": 11,
491 | "metadata": {
492 | "collapsed": true
493 | },
494 | "outputs": [
495 | {
496 | "ename": "AttributeError",
497 | "evalue": "'tuple' object has no attribute 'append'",
498 | "output_type": "error",
499 | "traceback": [
500 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
501 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
502 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
503 | "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
504 | ]
505 | }
506 | ],
507 | "source": [
508 | "x.append(3.3)"
509 | ]
510 | },
511 | {
512 | "cell_type": "markdown",
513 | "metadata": {},
514 | "source": [
515 | "
\n",
516 | "Use `+` to concatenate lists."
517 | ]
518 | },
519 | {
520 | "cell_type": "code",
521 | "execution_count": 8,
522 | "metadata": {
523 | "collapsed": true
524 | },
525 | "outputs": [
526 | {
527 | "data": {
528 | "text/plain": [
529 | "[1, 2, 3, 4]"
530 | ]
531 | },
532 | "execution_count": 8,
533 | "metadata": {},
534 | "output_type": "execute_result"
535 | }
536 | ],
537 | "source": [
538 | "[1,2] + [3,4]"
539 | ]
540 | },
541 | {
542 | "cell_type": "markdown",
543 | "metadata": {},
544 | "source": [
545 | "
\n",
546 | "Use `*` to repeat lists."
547 | ]
548 | },
549 | {
550 | "cell_type": "code",
551 | "execution_count": 9,
552 | "metadata": {
553 | "collapsed": true
554 | },
555 | "outputs": [
556 | {
557 | "data": {
558 | "text/plain": [
559 | "[1, 1, 1]"
560 | ]
561 | },
562 | "execution_count": 9,
563 | "metadata": {},
564 | "output_type": "execute_result"
565 | }
566 | ],
567 | "source": [
568 | "[1]*3"
569 | ]
570 | },
571 | {
572 | "cell_type": "markdown",
573 | "metadata": {
574 | "collapsed": false
575 | },
576 | "source": [
577 | "Use **len** to find how many elements are in a list"
578 | ]
579 | },
580 | {
581 | "cell_type": "code",
582 | "execution_count": 19,
583 | "metadata": {
584 | "collapsed": true
585 | },
586 | "outputs": [
587 | {
588 | "data": {
589 | "text/plain": [
590 | "21"
591 | ]
592 | },
593 | "execution_count": 19,
594 | "metadata": {},
595 | "output_type": "execute_result"
596 | }
597 | ],
598 | "source": [
599 | "x = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]\n",
600 | "len(x)"
601 | ]
602 | },
603 | {
604 | "cell_type": "markdown",
605 | "metadata": {},
606 | "source": [
607 | "
\n",
608 | "Use the `in` operator to check if something is inside a list."
609 | ]
610 | },
611 | {
612 | "cell_type": "code",
613 | "execution_count": 12,
614 | "metadata": {
615 | "collapsed": true
616 | },
617 | "outputs": [
618 | {
619 | "data": {
620 | "text/plain": [
621 | "True"
622 | ]
623 | },
624 | "execution_count": 12,
625 | "metadata": {},
626 | "output_type": "execute_result"
627 | }
628 | ],
629 | "source": [
630 | "1 in [1, 2, 3]"
631 | ]
632 | },
633 | {
634 | "cell_type": "markdown",
635 | "metadata": {},
636 | "source": [
637 | "### **sets** are unordered data strucutres that dont take repeats\n",
638 | "\n",
639 | "You can declare a tuple with the syntax: \n",
640 | "\n",
641 | "**{** *data1* **,** *data2* ..... **}** "
642 | ]
643 | },
644 | {
645 | "cell_type": "code",
646 | "execution_count": 13,
647 | "metadata": {
648 | "collapsed": true
649 | },
650 | "outputs": [
651 | {
652 | "name": "stdout",
653 | "output_type": "stream",
654 | "text": [
655 | "{1, 2, 3}\n"
656 | ]
657 | }
658 | ],
659 | "source": [
660 | "z = {1,1,1,2,3}\n",
661 | "print(z)"
662 | ]
663 | },
664 | {
665 | "cell_type": "markdown",
666 | "metadata": {},
667 | "source": [
668 | "You can remove duplicates in a list by converting it to a set"
669 | ]
670 | },
671 | {
672 | "cell_type": "code",
673 | "execution_count": 14,
674 | "metadata": {
675 | "collapsed": true
676 | },
677 | "outputs": [
678 | {
679 | "data": {
680 | "text/plain": [
681 | "{1, 2, 3}"
682 | ]
683 | },
684 | "execution_count": 14,
685 | "metadata": {},
686 | "output_type": "execute_result"
687 | }
688 | ],
689 | "source": [
690 | "a = [1,1,1,2,2,2,3,3,3]\n",
691 | "set(a)"
692 | ]
693 | },
694 | {
695 | "cell_type": "markdown",
696 | "metadata": {},
697 | "source": [
698 | "### **dictionaries** pairs a key to it's values\n",
699 | "\n",
700 | "You can declare a dictionary using:\n",
701 | "\n",
702 | "**{** *key* **:** *value* **,** ..... **}** "
703 | ]
704 | },
705 | {
706 | "cell_type": "code",
707 | "execution_count": 17,
708 | "metadata": {
709 | "collapsed": true
710 | },
711 | "outputs": [
712 | {
713 | "data": {
714 | "text/plain": [
715 | "'brooksch@umich.edu'"
716 | ]
717 | },
718 | "execution_count": 17,
719 | "metadata": {},
720 | "output_type": "execute_result"
721 | }
722 | ],
723 | "source": [
724 | "x = {'Mars Huang': 'marshuang80@gmail.com', 'Bill Gates': 'billg@microsoft.com'}\n",
725 | "x['Mars Huang'] # Retrieve a value by using the indexing operator\n"
726 | ]
727 | },
728 | {
729 | "cell_type": "code",
730 | "execution_count": null,
731 | "metadata": {
732 | "collapsed": true
733 | },
734 | "outputs": [],
735 | "source": [
736 | "x['Tony Stark'] = None\n",
737 | "x['Tony Stark']"
738 | ]
739 | },
740 | {
741 | "cell_type": "markdown",
742 | "metadata": {},
743 | "source": [
744 | "You can set any types as a dictionary value, including lists"
745 | ]
746 | },
747 | {
748 | "cell_type": "code",
749 | "execution_count": null,
750 | "metadata": {
751 | "collapsed": true
752 | },
753 | "outputs": [],
754 | "source": [
755 | "dictionary = {'list1': [1,2,3,4,5], 'list2': [100,200,300,400,500]}\n",
756 | "dictionary['list1']"
757 | ]
758 | }
759 | ],
760 | "metadata": {
761 | "anaconda-cloud": {},
762 | "kernelspec": {
763 | "display_name": "Python [default]",
764 | "language": "python",
765 | "name": "python3"
766 | },
767 | "language_info": {
768 | "codemirror_mode": {
769 | "name": "ipython",
770 | "version": 3
771 | },
772 | "file_extension": ".py",
773 | "mimetype": "text/x-python",
774 | "name": "python",
775 | "nbconvert_exporter": "python",
776 | "pygments_lexer": "ipython3",
777 | "version": "3.5.2"
778 | }
779 | },
780 | "nbformat": 4,
781 | "nbformat_minor": 0
782 | }
783 |
--------------------------------------------------------------------------------
/Tutorial_2_Loops_and_Indexing/.ipynb_checkpoints/Indexing and loops-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Tutorial 2: Indexing and loops"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "## List indexing\n",
16 | "\n",
17 | "Get a range of values in a list using list indexing\n",
18 | "\n",
19 | "*list_name* **[** *start_index* **:** *stop_index* **:** *step_size* **]**\n",
20 | "\n",
21 | "the index number can be reversed with the **-** sign (eg *-1* is last index)\n",
22 | "\n",
23 | "by default python automates list indexing as **[** *0* **:** *-1* **:** *1* **]**\n",
24 | "\n",
25 | "note that the range will **STOP** before the *stop_index*"
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 21,
31 | "metadata": {
32 | "collapsed": true
33 | },
34 | "outputs": [
35 | {
36 | "name": "stdout",
37 | "output_type": "stream",
38 | "text": [
39 | "a\n",
40 | "['a']\n",
41 | "['a', 'b', 'c']\n",
42 | "['a', 'b', 'c']\n"
43 | ]
44 | }
45 | ],
46 | "source": [
47 | "x = ['a','b','c','d','e']\n",
48 | "print(x[0]) #first character\n",
49 | "print(x[0:1]) #first character, but we have explicitly set the end character\n",
50 | "print(x[0:3]) #first 3 characters\n",
51 | "print(x[:3]) # also first 3 (start index defaults to 0)"
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "
\n",
59 | "This will return the last element of the string."
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 13,
65 | "metadata": {
66 | "collapsed": true
67 | },
68 | "outputs": [
69 | {
70 | "data": {
71 | "text/plain": [
72 | "'g'"
73 | ]
74 | },
75 | "execution_count": 13,
76 | "metadata": {},
77 | "output_type": "execute_result"
78 | }
79 | ],
80 | "source": [
81 | "x[-1]"
82 | ]
83 | },
84 | {
85 | "cell_type": "markdown",
86 | "metadata": {},
87 | "source": [
88 | "get the reverse of a list by using negative step size"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 22,
94 | "metadata": {
95 | "collapsed": true
96 | },
97 | "outputs": [
98 | {
99 | "data": {
100 | "text/plain": [
101 | "['e', 'd', 'c', 'b', 'a']"
102 | ]
103 | },
104 | "execution_count": 22,
105 | "metadata": {},
106 | "output_type": "execute_result"
107 | }
108 | ],
109 | "source": [
110 | "x[::-1]"
111 | ]
112 | },
113 | {
114 | "cell_type": "markdown",
115 | "metadata": {},
116 | "source": [
117 | "
\n",
118 | "This will return the slice starting from the 4th element from the end and stopping before the 2nd element from the end."
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": null,
124 | "metadata": {
125 | "collapsed": false
126 | },
127 | "outputs": [],
128 | "source": [
129 | "x[-4:-2]"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {},
135 | "source": [
136 | "
\n",
137 | "This is a slice from the beginning of the string and stopping before the 3rd element."
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": 14,
143 | "metadata": {
144 | "collapsed": true
145 | },
146 | "outputs": [
147 | {
148 | "data": {
149 | "text/plain": [
150 | "'Thi'"
151 | ]
152 | },
153 | "execution_count": 14,
154 | "metadata": {},
155 | "output_type": "execute_result"
156 | }
157 | ],
158 | "source": [
159 | "x[:3]"
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "metadata": {},
165 | "source": [
166 | "
\n",
167 | "And this is a slice starting from the 3rd element of the string and going all the way to the end."
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 15,
173 | "metadata": {
174 | "collapsed": true
175 | },
176 | "outputs": [
177 | {
178 | "data": {
179 | "text/plain": [
180 | "'s is a string'"
181 | ]
182 | },
183 | "execution_count": 15,
184 | "metadata": {},
185 | "output_type": "execute_result"
186 | }
187 | ],
188 | "source": [
189 | "x[3:]"
190 | ]
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {},
195 | "source": [
196 | "
\n",
197 | "Strings are a list of characters"
198 | ]
199 | },
200 | {
201 | "cell_type": "code",
202 | "execution_count": null,
203 | "metadata": {
204 | "collapsed": true
205 | },
206 | "outputs": [],
207 | "source": [
208 | "a = \"hello world!\"\n",
209 | "a[2:5]"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": 9,
215 | "metadata": {
216 | "collapsed": true
217 | },
218 | "outputs": [],
219 | "source": [
220 | "#EXERCISE: set y to your name and print out characters from y one at time using for loop\n"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": 11,
226 | "metadata": {
227 | "collapsed": false
228 | },
229 | "outputs": [],
230 | "source": [
231 | "#EXERCISE: set variable name to your full name and print out the reverse of your last name using list slicing\n",
232 | "name = "
233 | ]
234 | },
235 | {
236 | "cell_type": "markdown",
237 | "metadata": {},
238 | "source": [
239 | "# Loops \n",
240 | "\n",
241 | "Loops are used to execuate a code for multiple times\n",
242 | "\n",
243 | "### while loops \n",
244 | "\n",
245 | "while loops are used to execute a block of code until the initial conidtion becomes True\n",
246 | "\n",
247 | "\n",
248 | "**while** *condition* **:**\n",
249 | " \n",
250 | " **\n",
251 | " \n",
252 | " **\n",
253 | " \n",
254 | " *......*\n",
255 | " \n",
256 | "**warning** if a conidition is never reached, while loops will run forever"
257 | ]
258 | },
259 | {
260 | "cell_type": "code",
261 | "execution_count": null,
262 | "metadata": {
263 | "collapsed": true
264 | },
265 | "outputs": [],
266 | "source": [
267 | "a = 0\n",
268 | "while a != 10: #!= (not equals to), == (equalts to), > (greater than), < (less than) = (variable assignment)\n",
269 | " a += 1 #+= (same as : a = a + 1 )\n",
270 | "print(a)"
271 | ]
272 | },
273 | {
274 | "cell_type": "markdown",
275 | "metadata": {},
276 | "source": [
277 | "### for loops\n",
278 | "\n",
279 | "for loops get each item in a list, give it a temporary variable name, and apply the indend lines of code after **:** \n",
280 | "\n",
281 | "**for** *variable_name* **in** *list* **:**\n",
282 | " \n",
283 | " **\n",
284 | " \n",
285 | " **\n",
286 | " \n",
287 | " *......*"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": 25,
293 | "metadata": {
294 | "collapsed": true
295 | },
296 | "outputs": [
297 | {
298 | "name": "stdout",
299 | "output_type": "stream",
300 | "text": [
301 | "dogs\n",
302 | "-----\n",
303 | "cats\n",
304 | "-----\n",
305 | "elephants\n",
306 | "-----\n",
307 | "kardashians\n",
308 | "-----\n"
309 | ]
310 | }
311 | ],
312 | "source": [
313 | "zoo = ['dogs','cats','elephants','kardashians']\n",
314 | "for animals in zoo:\n",
315 | " print(animals)\n",
316 | " print(\"-----\")"
317 | ]
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "metadata": {},
322 | "source": [
323 | "The **range** operator can be used to set a numeric range for the for loop inplace of a list\n",
324 | "\n",
325 | "**for** *variable_name* **in** **range(** *start_number*, *end_number*, *step_size* **)** **:**\n",
326 | " \n",
327 | " **\n",
328 | " \n",
329 | " **\n",
330 | " \n",
331 | " *......*"
332 | ]
333 | },
334 | {
335 | "cell_type": "code",
336 | "execution_count": null,
337 | "metadata": {
338 | "collapsed": true
339 | },
340 | "outputs": [],
341 | "source": [
342 | "for number in range(0,10,2):\n",
343 | " print(number)"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "For loops with ranges can also be used to repeat an operation certain number of times"
351 | ]
352 | },
353 | {
354 | "cell_type": "code",
355 | "execution_count": null,
356 | "metadata": {
357 | "collapsed": true
358 | },
359 | "outputs": [],
360 | "source": [
361 | "a = 0 \n",
362 | "for count in range(10):\n",
363 | " a += 1 \n",
364 | "print(a)"
365 | ]
366 | },
367 | {
368 | "cell_type": "markdown",
369 | "metadata": {},
370 | "source": [
371 | "
\n",
372 | "while loops can be used to index a list, but it is still prepered to use a for loop"
373 | ]
374 | },
375 | {
376 | "cell_type": "code",
377 | "execution_count": null,
378 | "metadata": {
379 | "collapsed": false
380 | },
381 | "outputs": [],
382 | "source": [
383 | "i=0\n",
384 | "while( i != len(x) ):\n",
385 | " print(x[i])\n",
386 | " i = i + 1"
387 | ]
388 | },
389 | {
390 | "cell_type": "code",
391 | "execution_count": 9,
392 | "metadata": {
393 | "collapsed": true
394 | },
395 | "outputs": [],
396 | "source": [
397 | "#EXERCISE: print out characters from y one at time using while loop\n"
398 | ]
399 | },
400 | {
401 | "cell_type": "markdown",
402 | "metadata": {},
403 | "source": [
404 | "There are many ways to loops through a dictionary"
405 | ]
406 | },
407 | {
408 | "cell_type": "markdown",
409 | "metadata": {},
410 | "source": [
411 | "
\n",
412 | "Iterate over all of the keys:"
413 | ]
414 | },
415 | {
416 | "cell_type": "code",
417 | "execution_count": 19,
418 | "metadata": {
419 | "collapsed": true
420 | },
421 | "outputs": [
422 | {
423 | "name": "stdout",
424 | "output_type": "stream",
425 | "text": [
426 | "None\n",
427 | "billg@microsoft.com\n",
428 | "brooksch@umich.edu\n"
429 | ]
430 | }
431 | ],
432 | "source": [
433 | "x = {'Mars':'marshuang80@gmail.com','Jupiter':'jupiterhuang80@gmail.com','Pluto':'plutohuang80@gmail.com'}\n",
434 | "for name in x:\n",
435 | " print(x[name])"
436 | ]
437 | },
438 | {
439 | "cell_type": "markdown",
440 | "metadata": {},
441 | "source": [
442 | "
\n",
443 | "Iterate over all of the values of a dictionary:\n",
444 | "\n",
445 | "**for** *variable_name* **in** *dictionary_name* **.values( )**:"
446 | ]
447 | },
448 | {
449 | "cell_type": "code",
450 | "execution_count": 20,
451 | "metadata": {
452 | "collapsed": true
453 | },
454 | "outputs": [
455 | {
456 | "name": "stdout",
457 | "output_type": "stream",
458 | "text": [
459 | "None\n",
460 | "billg@microsoft.com\n",
461 | "brooksch@umich.edu\n"
462 | ]
463 | }
464 | ],
465 | "source": [
466 | "for email in x.values():\n",
467 | " print(email)"
468 | ]
469 | },
470 | {
471 | "cell_type": "markdown",
472 | "metadata": {},
473 | "source": [
474 | "
\n",
475 | "Iterate over all of the keys and values in the dictionary:\n",
476 | "\n",
477 | "**for** *key* **,** *variable_name* **in** *dictionary_name* **.values( )**:"
478 | ]
479 | },
480 | {
481 | "cell_type": "code",
482 | "execution_count": 21,
483 | "metadata": {
484 | "collapsed": true
485 | },
486 | "outputs": [
487 | {
488 | "name": "stdout",
489 | "output_type": "stream",
490 | "text": [
491 | "Kevyn Collins-Thompson\n",
492 | "None\n",
493 | "Bill Gates\n",
494 | "billg@microsoft.com\n",
495 | "Christopher Brooks\n",
496 | "brooksch@umich.edu\n"
497 | ]
498 | }
499 | ],
500 | "source": [
501 | "for name, email in x.items():\n",
502 | " print(name)\n",
503 | " print(email)"
504 | ]
505 | },
506 | {
507 | "cell_type": "markdown",
508 | "metadata": {},
509 | "source": [
510 | "
\n",
511 | "# Strings and Printing"
512 | ]
513 | },
514 | {
515 | "cell_type": "code",
516 | "execution_count": null,
517 | "metadata": {
518 | "collapsed": false
519 | },
520 | "outputs": [],
521 | "source": [
522 | "firstname = 'Mars'\n",
523 | "lastname = 'Huang'\n",
524 | "\n",
525 | "print(firstname + ' ' + lastname) #joining strings together\n",
526 | "print(firstname*3) #repeating a string 3 times \n",
527 | "print('Mars' in firstname) #bool\n"
528 | ]
529 | },
530 | {
531 | "cell_type": "code",
532 | "execution_count": 14,
533 | "metadata": {
534 | "collapsed": true
535 | },
536 | "outputs": [
537 | {
538 | "name": "stdout",
539 | "output_type": "stream",
540 | "text": [
541 | "1\n",
542 | "2\n"
543 | ]
544 | }
545 | ],
546 | "source": [
547 | "##EXERCISE: print the following\n",
548 | "# print a,b and c on the same line\n",
549 | "a = 1\n",
550 | "b = 2\n",
551 | "c = 3\n"
552 | ]
553 | },
554 | {
555 | "cell_type": "code",
556 | "execution_count": 28,
557 | "metadata": {
558 | "collapsed": false,
559 | "scrolled": true
560 | },
561 | "outputs": [
562 | {
563 | "name": "stdout",
564 | "output_type": "stream",
565 | "text": [
566 | "[1, 2, 3, 4, 3, 4]\n"
567 | ]
568 | }
569 | ],
570 | "source": [
571 | "##EXERCISE\n",
572 | "# print lists a,b and c on the same line as seperate lists\n",
573 | "a = [1,2]\n",
574 | "b = [3,4]\n",
575 | "c = [5,6]"
576 | ]
577 | },
578 | {
579 | "cell_type": "markdown",
580 | "metadata": {},
581 | "source": [
582 | "Since **+** joins a list, use **,** to seperate lists while printing"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 29,
588 | "metadata": {
589 | "collapsed": true
590 | },
591 | "outputs": [
592 | {
593 | "name": "stdout",
594 | "output_type": "stream",
595 | "text": [
596 | "[1, 2] [3, 4] [5, 6]\n"
597 | ]
598 | }
599 | ],
600 | "source": [
601 | "a = [1,2]\n",
602 | "b = [3,4]\n",
603 | "c = [5,6]\n",
604 | "print(a,b,c)"
605 | ]
606 | },
607 | {
608 | "cell_type": "code",
609 | "execution_count": null,
610 | "metadata": {
611 | "collapsed": true
612 | },
613 | "outputs": [],
614 | "source": [
615 | "##EXERCISE\n",
616 | "# print lists the frist 4 elements of list A and the reverse of last 3 elements of list B as one list\n",
617 | "A = [0,1,2,3,4,5,6,7,8,9]\n",
618 | "B = [14,13,12,11,10]"
619 | ]
620 | },
621 | {
622 | "cell_type": "markdown",
623 | "metadata": {},
624 | "source": [
625 | "
\n",
626 | "`split` returns a list of all the words in a string, or a list split on a specific character."
627 | ]
628 | },
629 | {
630 | "cell_type": "code",
631 | "execution_count": 16,
632 | "metadata": {
633 | "collapsed": true
634 | },
635 | "outputs": [
636 | {
637 | "name": "stdout",
638 | "output_type": "stream",
639 | "text": [
640 | "['Mars', 'Shih-Cheng', 'Huang']\n"
641 | ]
642 | }
643 | ],
644 | "source": [
645 | "name = 'Mars Shih-Cheng Huang'\n",
646 | "print(name.split(' '))"
647 | ]
648 | },
649 | {
650 | "cell_type": "code",
651 | "execution_count": 30,
652 | "metadata": {
653 | "collapsed": true
654 | },
655 | "outputs": [
656 | {
657 | "name": "stdout",
658 | "output_type": "stream",
659 | "text": [
660 | "['Mars Shih-Cheng Huang']\n"
661 | ]
662 | }
663 | ],
664 | "source": [
665 | "name = 'Mars Shih-Cheng Huang'\n",
666 | "print(name.split(','))"
667 | ]
668 | },
669 | {
670 | "cell_type": "code",
671 | "execution_count": null,
672 | "metadata": {
673 | "collapsed": true
674 | },
675 | "outputs": [],
676 | "source": [
677 | "#print out my last name and first name from \"name\" using list indexing and split"
678 | ]
679 | },
680 | {
681 | "cell_type": "markdown",
682 | "metadata": {},
683 | "source": [
684 | "
\n",
685 | "Make sure you convert objects to strings before concatenating."
686 | ]
687 | },
688 | {
689 | "cell_type": "code",
690 | "execution_count": null,
691 | "metadata": {
692 | "collapsed": false
693 | },
694 | "outputs": [],
695 | "source": [
696 | "'Mars' + 2"
697 | ]
698 | },
699 | {
700 | "cell_type": "code",
701 | "execution_count": null,
702 | "metadata": {
703 | "collapsed": false
704 | },
705 | "outputs": [],
706 | "source": [
707 | "'Mars' + str(2)"
708 | ]
709 | },
710 | {
711 | "cell_type": "code",
712 | "execution_count": 17,
713 | "metadata": {
714 | "collapsed": true
715 | },
716 | "outputs": [],
717 | "source": [
718 | "#Exercise: Create a dictionary called classes, with the classes you're taking as key, \n",
719 | "#and the grade that your want to recieve as index\n"
720 | ]
721 | },
722 | {
723 | "cell_type": "code",
724 | "execution_count": 18,
725 | "metadata": {
726 | "collapsed": true
727 | },
728 | "outputs": [],
729 | "source": [
730 | "#Exercise: print out all key, values from your diciotnary in the following format:\n",
731 | "#\"The grade that I want to revieve for ____ class is ____\""
732 | ]
733 | },
734 | {
735 | "cell_type": "markdown",
736 | "metadata": {},
737 | "source": [
738 | "
\n",
739 | "You can unpack a sequence into different variables:"
740 | ]
741 | },
742 | {
743 | "cell_type": "code",
744 | "execution_count": 23,
745 | "metadata": {
746 | "collapsed": true
747 | },
748 | "outputs": [],
749 | "source": [
750 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
751 | "fname, lname, email = x"
752 | ]
753 | },
754 | {
755 | "cell_type": "code",
756 | "execution_count": 24,
757 | "metadata": {
758 | "collapsed": true
759 | },
760 | "outputs": [
761 | {
762 | "data": {
763 | "text/plain": [
764 | "'Christopher'"
765 | ]
766 | },
767 | "execution_count": 24,
768 | "metadata": {},
769 | "output_type": "execute_result"
770 | }
771 | ],
772 | "source": [
773 | "fname"
774 | ]
775 | },
776 | {
777 | "cell_type": "code",
778 | "execution_count": 25,
779 | "metadata": {
780 | "collapsed": true
781 | },
782 | "outputs": [
783 | {
784 | "data": {
785 | "text/plain": [
786 | "'Brooks'"
787 | ]
788 | },
789 | "execution_count": 25,
790 | "metadata": {},
791 | "output_type": "execute_result"
792 | }
793 | ],
794 | "source": [
795 | "lname"
796 | ]
797 | },
798 | {
799 | "cell_type": "code",
800 | "execution_count": null,
801 | "metadata": {
802 | "collapsed": false
803 | },
804 | "outputs": [],
805 | "source": [
806 | "print('Mars' + 2)"
807 | ]
808 | },
809 | {
810 | "cell_type": "code",
811 | "execution_count": null,
812 | "metadata": {
813 | "collapsed": false
814 | },
815 | "outputs": [],
816 | "source": [
817 | "print('Mars' + str(2))"
818 | ]
819 | },
820 | {
821 | "cell_type": "markdown",
822 | "metadata": {},
823 | "source": [
824 | "
\n",
825 | "Python has a built in method for convenient string formatting."
826 | ]
827 | },
828 | {
829 | "cell_type": "code",
830 | "execution_count": 19,
831 | "metadata": {
832 | "collapsed": true
833 | },
834 | "outputs": [
835 | {
836 | "name": "stdout",
837 | "output_type": "stream",
838 | "text": [
839 | "Chris bought 4 item(s) at a price of 3.24 each for a total of 12.96\n"
840 | ]
841 | }
842 | ],
843 | "source": [
844 | "sales_record = {\n",
845 | "'price': 3.24,\n",
846 | "'num_items': 4,\n",
847 | "'person': 'Chris'}\n",
848 | "\n",
849 | "sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}'\n",
850 | "\n",
851 | "print(sales_statement.format(sales_record['person'],\n",
852 | " sales_record['num_items'],\n",
853 | " sales_record['price'],\n",
854 | " sales_record['num_items']*sales_record['price']))\n"
855 | ]
856 | },
857 | {
858 | "cell_type": "code",
859 | "execution_count": 20,
860 | "metadata": {
861 | "collapsed": true
862 | },
863 | "outputs": [
864 | {
865 | "name": "stdout",
866 | "output_type": "stream",
867 | "text": [
868 | "Chris bought 4 item(s) at a price of 3.240000 each for a total of 12.960000\n"
869 | ]
870 | }
871 | ],
872 | "source": [
873 | "#Another method\n",
874 | "print('%s bought %i item(s) at a price of %f each for a total of %f'%(sales_record['person'],\n",
875 | " sales_record['num_items'],\n",
876 | " sales_record['price'],\n",
877 | " sales_record['num_items']*sales_record['price']))\n"
878 | ]
879 | },
880 | {
881 | "cell_type": "code",
882 | "execution_count": null,
883 | "metadata": {
884 | "collapsed": true
885 | },
886 | "outputs": [],
887 | "source": [
888 | "#Exercise: print out \"I took 'a' shots and drank 'b' bottles of beer yesterday night\"\n",
889 | "a = 7\n",
890 | "b = 2"
891 | ]
892 | },
893 | {
894 | "cell_type": "markdown",
895 | "metadata": {},
896 | "source": [
897 | "# Unpacking"
898 | ]
899 | },
900 | {
901 | "cell_type": "markdown",
902 | "metadata": {},
903 | "source": [
904 | "
\n",
905 | "You can unpack a sequence into different variables:"
906 | ]
907 | },
908 | {
909 | "cell_type": "code",
910 | "execution_count": 23,
911 | "metadata": {
912 | "collapsed": true
913 | },
914 | "outputs": [],
915 | "source": [
916 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
917 | "fname, lname, email = x"
918 | ]
919 | },
920 | {
921 | "cell_type": "code",
922 | "execution_count": 24,
923 | "metadata": {
924 | "collapsed": true
925 | },
926 | "outputs": [
927 | {
928 | "data": {
929 | "text/plain": [
930 | "'Christopher'"
931 | ]
932 | },
933 | "execution_count": 24,
934 | "metadata": {},
935 | "output_type": "execute_result"
936 | }
937 | ],
938 | "source": [
939 | "fname"
940 | ]
941 | },
942 | {
943 | "cell_type": "code",
944 | "execution_count": 25,
945 | "metadata": {
946 | "collapsed": true
947 | },
948 | "outputs": [
949 | {
950 | "data": {
951 | "text/plain": [
952 | "'Brooks'"
953 | ]
954 | },
955 | "execution_count": 25,
956 | "metadata": {},
957 | "output_type": "execute_result"
958 | }
959 | ],
960 | "source": [
961 | "lname"
962 | ]
963 | },
964 | {
965 | "cell_type": "markdown",
966 | "metadata": {},
967 | "source": [
968 | "
\n",
969 | "Make sure the number of values you are unpacking matches the number of variables being assigned."
970 | ]
971 | },
972 | {
973 | "cell_type": "code",
974 | "execution_count": null,
975 | "metadata": {
976 | "collapsed": false
977 | },
978 | "outputs": [],
979 | "source": [
980 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com', 'San Diego')\n",
981 | "fname, lname, email = x"
982 | ]
983 | }
984 | ],
985 | "metadata": {
986 | "anaconda-cloud": {},
987 | "kernelspec": {
988 | "display_name": "Python [default]",
989 | "language": "python",
990 | "name": "python3"
991 | },
992 | "language_info": {
993 | "codemirror_mode": {
994 | "name": "ipython",
995 | "version": 3
996 | },
997 | "file_extension": ".py",
998 | "mimetype": "text/x-python",
999 | "name": "python",
1000 | "nbconvert_exporter": "python",
1001 | "pygments_lexer": "ipython3",
1002 | "version": "3.5.2"
1003 | }
1004 | },
1005 | "nbformat": 4,
1006 | "nbformat_minor": 0
1007 | }
1008 |
--------------------------------------------------------------------------------
/Tutorial_2_Loops_and_Indexing/.ipynb_checkpoints/Loops and indexing-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Tutorial 2: Loops and indexing"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "# Loops \n",
16 | "\n",
17 | "Loops are used to execuate a code for multiple times\n",
18 | "\n",
19 | "### while loops \n",
20 | "\n",
21 | "while loops are used to execute a block of code until the initial conidtion becomes True\n",
22 | "\n",
23 | "\n",
24 | "**while** *condition* **:**\n",
25 | " \n",
26 | " **\n",
27 | " \n",
28 | " **\n",
29 | " \n",
30 | " *......*\n",
31 | " \n",
32 | "**warning** if a conidition is never reached, while loops will run forever"
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": null,
38 | "metadata": {
39 | "collapsed": true
40 | },
41 | "outputs": [],
42 | "source": [
43 | "a = 0\n",
44 | "while a != 10: #!= (not equals to), == (equalts to), > (greater than), < (less than) = (variable assignment)\n",
45 | " a += 1 #+= (same as : a = a + 1 )\n",
46 | "print(a)"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "### for loops\n",
54 | "\n",
55 | "for loops get each item in a list, give it a temporary variable name, and apply the indend lines of code after **:** \n",
56 | "\n",
57 | "**for** *variable_name* **in** *list* **:**\n",
58 | " \n",
59 | " **\n",
60 | " \n",
61 | " **\n",
62 | " \n",
63 | " *......*"
64 | ]
65 | },
66 | {
67 | "cell_type": "code",
68 | "execution_count": 25,
69 | "metadata": {
70 | "collapsed": true
71 | },
72 | "outputs": [
73 | {
74 | "name": "stdout",
75 | "output_type": "stream",
76 | "text": [
77 | "dogs\n",
78 | "-----\n",
79 | "cats\n",
80 | "-----\n",
81 | "elephants\n",
82 | "-----\n",
83 | "kardashians\n",
84 | "-----\n"
85 | ]
86 | }
87 | ],
88 | "source": [
89 | "zoo = ['dogs','cats','elephants','kardashians']\n",
90 | "for animals in zoo:\n",
91 | " print(animals)\n",
92 | " print(\"-----\")"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "The **range** operator can be used to set a numeric range for the for loop inplace of a list\n",
100 | "\n",
101 | "**for** *variable_name* **in** **range(** *start_number*, *end_number*, *step_size* **)** **:**\n",
102 | " \n",
103 | " **\n",
104 | " \n",
105 | " **\n",
106 | " \n",
107 | " *......*"
108 | ]
109 | },
110 | {
111 | "cell_type": "code",
112 | "execution_count": null,
113 | "metadata": {
114 | "collapsed": true
115 | },
116 | "outputs": [],
117 | "source": [
118 | "for number in range(0,10,2):\n",
119 | " print(number)"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "For loops with ranges can also be used to repeat an operation certain number of times"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "metadata": {
133 | "collapsed": true
134 | },
135 | "outputs": [],
136 | "source": [
137 | "a = 0 \n",
138 | "for count in range(10):\n",
139 | " a += 1 \n",
140 | "print(a)"
141 | ]
142 | },
143 | {
144 | "cell_type": "markdown",
145 | "metadata": {},
146 | "source": [
147 | "
\n",
148 | "while loops can be used to index a list, but it is still prepered to use a for loop"
149 | ]
150 | },
151 | {
152 | "cell_type": "code",
153 | "execution_count": null,
154 | "metadata": {
155 | "collapsed": false
156 | },
157 | "outputs": [],
158 | "source": [
159 | "i=0\n",
160 | "while( i != len(x) ):\n",
161 | " print(x[i])\n",
162 | " i = i + 1"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": 9,
168 | "metadata": {
169 | "collapsed": true
170 | },
171 | "outputs": [],
172 | "source": [
173 | "#EXERCISE: print out characters from y one at time using while loop\n"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "metadata": {},
179 | "source": [
180 | "There are many ways to loops through a dictionary"
181 | ]
182 | },
183 | {
184 | "cell_type": "markdown",
185 | "metadata": {},
186 | "source": [
187 | "
\n",
188 | "Iterate over all of the keys:"
189 | ]
190 | },
191 | {
192 | "cell_type": "code",
193 | "execution_count": 19,
194 | "metadata": {
195 | "collapsed": true
196 | },
197 | "outputs": [
198 | {
199 | "name": "stdout",
200 | "output_type": "stream",
201 | "text": [
202 | "None\n",
203 | "billg@microsoft.com\n",
204 | "brooksch@umich.edu\n"
205 | ]
206 | }
207 | ],
208 | "source": [
209 | "x = {'Mars':'marshuang80@gmail.com','Jupiter':'jupiterhuang80@gmail.com','Pluto':'plutohuang80@gmail.com'}\n",
210 | "for name in x:\n",
211 | " print(x[name])"
212 | ]
213 | },
214 | {
215 | "cell_type": "markdown",
216 | "metadata": {},
217 | "source": [
218 | "
\n",
219 | "Iterate over all of the values of a dictionary:\n",
220 | "\n",
221 | "**for** *variable_name* **in** *dictionary_name* **.values( )**:"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": 20,
227 | "metadata": {
228 | "collapsed": true
229 | },
230 | "outputs": [
231 | {
232 | "name": "stdout",
233 | "output_type": "stream",
234 | "text": [
235 | "None\n",
236 | "billg@microsoft.com\n",
237 | "brooksch@umich.edu\n"
238 | ]
239 | }
240 | ],
241 | "source": [
242 | "for email in x.values():\n",
243 | " print(email)"
244 | ]
245 | },
246 | {
247 | "cell_type": "markdown",
248 | "metadata": {},
249 | "source": [
250 | "
\n",
251 | "Iterate over all of the keys and values in the dictionary:\n",
252 | "\n",
253 | "**for** *key* **,** *variable_name* **in** *dictionary_name* **.values( )**:"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 21,
259 | "metadata": {
260 | "collapsed": true
261 | },
262 | "outputs": [
263 | {
264 | "name": "stdout",
265 | "output_type": "stream",
266 | "text": [
267 | "Kevyn Collins-Thompson\n",
268 | "None\n",
269 | "Bill Gates\n",
270 | "billg@microsoft.com\n",
271 | "Christopher Brooks\n",
272 | "brooksch@umich.edu\n"
273 | ]
274 | }
275 | ],
276 | "source": [
277 | "for name, email in x.items():\n",
278 | " print(name)\n",
279 | " print(email)"
280 | ]
281 | },
282 | {
283 | "cell_type": "markdown",
284 | "metadata": {},
285 | "source": [
286 | "
\n",
287 | "# Strings and Printing"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": null,
293 | "metadata": {
294 | "collapsed": false
295 | },
296 | "outputs": [],
297 | "source": [
298 | "firstname = 'Mars'\n",
299 | "lastname = 'Huang'\n",
300 | "\n",
301 | "print(firstname + ' ' + lastname) #joining strings together\n",
302 | "print(firstname*3) #repeating a string 3 times \n",
303 | "print('Mars' in firstname) #bool\n"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": 14,
309 | "metadata": {
310 | "collapsed": true
311 | },
312 | "outputs": [
313 | {
314 | "name": "stdout",
315 | "output_type": "stream",
316 | "text": [
317 | "1\n",
318 | "2\n"
319 | ]
320 | }
321 | ],
322 | "source": [
323 | "##EXERCISE: print the following\n",
324 | "# print a,b and c on the same line\n",
325 | "a = 1\n",
326 | "b = 2\n",
327 | "c = 3\n"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 28,
333 | "metadata": {
334 | "collapsed": false,
335 | "scrolled": true
336 | },
337 | "outputs": [
338 | {
339 | "name": "stdout",
340 | "output_type": "stream",
341 | "text": [
342 | "[1, 2, 3, 4, 3, 4]\n"
343 | ]
344 | }
345 | ],
346 | "source": [
347 | "##EXERCISE\n",
348 | "# print lists a,b and c on the same line as seperate lists\n",
349 | "a = [1,2]\n",
350 | "b = [3,4]\n",
351 | "c = [5,6]"
352 | ]
353 | },
354 | {
355 | "cell_type": "markdown",
356 | "metadata": {},
357 | "source": [
358 | "Since **+** joins a list, use **,** to seperate lists while printing"
359 | ]
360 | },
361 | {
362 | "cell_type": "code",
363 | "execution_count": 29,
364 | "metadata": {
365 | "collapsed": true
366 | },
367 | "outputs": [
368 | {
369 | "name": "stdout",
370 | "output_type": "stream",
371 | "text": [
372 | "[1, 2] [3, 4] [5, 6]\n"
373 | ]
374 | }
375 | ],
376 | "source": [
377 | "a = [1,2]\n",
378 | "b = [3,4]\n",
379 | "c = [5,6]\n",
380 | "print(a,b,c)"
381 | ]
382 | },
383 | {
384 | "cell_type": "code",
385 | "execution_count": null,
386 | "metadata": {
387 | "collapsed": true
388 | },
389 | "outputs": [],
390 | "source": [
391 | "##EXERCISE\n",
392 | "# print lists the frist 4 elements of list A and the reverse of last 3 elements of list B as one list\n",
393 | "A = [0,1,2,3,4,5,6,7,8,9]\n",
394 | "B = [14,13,12,11,10]"
395 | ]
396 | },
397 | {
398 | "cell_type": "markdown",
399 | "metadata": {},
400 | "source": [
401 | "
\n",
402 | "`split` returns a list of all the words in a string, or a list split on a specific character."
403 | ]
404 | },
405 | {
406 | "cell_type": "code",
407 | "execution_count": 16,
408 | "metadata": {
409 | "collapsed": true
410 | },
411 | "outputs": [
412 | {
413 | "name": "stdout",
414 | "output_type": "stream",
415 | "text": [
416 | "['Mars', 'Shih-Cheng', 'Huang']\n"
417 | ]
418 | }
419 | ],
420 | "source": [
421 | "name = 'Mars Shih-Cheng Huang'\n",
422 | "print(name.split(' '))"
423 | ]
424 | },
425 | {
426 | "cell_type": "code",
427 | "execution_count": 30,
428 | "metadata": {
429 | "collapsed": true
430 | },
431 | "outputs": [
432 | {
433 | "name": "stdout",
434 | "output_type": "stream",
435 | "text": [
436 | "['Mars Shih-Cheng Huang']\n"
437 | ]
438 | }
439 | ],
440 | "source": [
441 | "name = 'Mars Shih-Cheng Huang'\n",
442 | "print(name.split(','))"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": null,
448 | "metadata": {
449 | "collapsed": true
450 | },
451 | "outputs": [],
452 | "source": [
453 | "#print out my last name and first name from \"name\" using list indexing and split"
454 | ]
455 | },
456 | {
457 | "cell_type": "markdown",
458 | "metadata": {},
459 | "source": [
460 | "
\n",
461 | "Make sure you convert objects to strings before concatenating."
462 | ]
463 | },
464 | {
465 | "cell_type": "code",
466 | "execution_count": null,
467 | "metadata": {
468 | "collapsed": false
469 | },
470 | "outputs": [],
471 | "source": [
472 | "'Mars' + 2"
473 | ]
474 | },
475 | {
476 | "cell_type": "code",
477 | "execution_count": null,
478 | "metadata": {
479 | "collapsed": false
480 | },
481 | "outputs": [],
482 | "source": [
483 | "'Mars' + str(2)"
484 | ]
485 | },
486 | {
487 | "cell_type": "code",
488 | "execution_count": 17,
489 | "metadata": {
490 | "collapsed": true
491 | },
492 | "outputs": [],
493 | "source": [
494 | "#Exercise: Create a dictionary called classes, with the classes you're taking as key, \n",
495 | "#and the grade that your want to recieve as index\n"
496 | ]
497 | },
498 | {
499 | "cell_type": "code",
500 | "execution_count": 18,
501 | "metadata": {
502 | "collapsed": true
503 | },
504 | "outputs": [],
505 | "source": [
506 | "#Exercise: print out all key, values from your diciotnary in the following format:\n",
507 | "#\"The grade that I want to revieve for ____ class is ____\""
508 | ]
509 | },
510 | {
511 | "cell_type": "markdown",
512 | "metadata": {},
513 | "source": [
514 | "
\n",
515 | "You can unpack a sequence into different variables:"
516 | ]
517 | },
518 | {
519 | "cell_type": "code",
520 | "execution_count": 23,
521 | "metadata": {
522 | "collapsed": true
523 | },
524 | "outputs": [],
525 | "source": [
526 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
527 | "fname, lname, email = x"
528 | ]
529 | },
530 | {
531 | "cell_type": "code",
532 | "execution_count": 24,
533 | "metadata": {
534 | "collapsed": true
535 | },
536 | "outputs": [
537 | {
538 | "data": {
539 | "text/plain": [
540 | "'Christopher'"
541 | ]
542 | },
543 | "execution_count": 24,
544 | "metadata": {},
545 | "output_type": "execute_result"
546 | }
547 | ],
548 | "source": [
549 | "fname"
550 | ]
551 | },
552 | {
553 | "cell_type": "code",
554 | "execution_count": 25,
555 | "metadata": {
556 | "collapsed": true
557 | },
558 | "outputs": [
559 | {
560 | "data": {
561 | "text/plain": [
562 | "'Brooks'"
563 | ]
564 | },
565 | "execution_count": 25,
566 | "metadata": {},
567 | "output_type": "execute_result"
568 | }
569 | ],
570 | "source": [
571 | "lname"
572 | ]
573 | },
574 | {
575 | "cell_type": "code",
576 | "execution_count": null,
577 | "metadata": {
578 | "collapsed": false
579 | },
580 | "outputs": [],
581 | "source": [
582 | "print('Mars' + 2)"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": null,
588 | "metadata": {
589 | "collapsed": false
590 | },
591 | "outputs": [],
592 | "source": [
593 | "print('Mars' + str(2))"
594 | ]
595 | },
596 | {
597 | "cell_type": "markdown",
598 | "metadata": {},
599 | "source": [
600 | "
\n",
601 | "Python has a built in method for convenient string formatting."
602 | ]
603 | },
604 | {
605 | "cell_type": "code",
606 | "execution_count": 19,
607 | "metadata": {
608 | "collapsed": true
609 | },
610 | "outputs": [
611 | {
612 | "name": "stdout",
613 | "output_type": "stream",
614 | "text": [
615 | "Chris bought 4 item(s) at a price of 3.24 each for a total of 12.96\n"
616 | ]
617 | }
618 | ],
619 | "source": [
620 | "sales_record = {\n",
621 | "'price': 3.24,\n",
622 | "'num_items': 4,\n",
623 | "'person': 'Chris'}\n",
624 | "\n",
625 | "sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}'\n",
626 | "\n",
627 | "print(sales_statement.format(sales_record['person'],\n",
628 | " sales_record['num_items'],\n",
629 | " sales_record['price'],\n",
630 | " sales_record['num_items']*sales_record['price']))\n"
631 | ]
632 | },
633 | {
634 | "cell_type": "code",
635 | "execution_count": 20,
636 | "metadata": {
637 | "collapsed": true
638 | },
639 | "outputs": [
640 | {
641 | "name": "stdout",
642 | "output_type": "stream",
643 | "text": [
644 | "Chris bought 4 item(s) at a price of 3.240000 each for a total of 12.960000\n"
645 | ]
646 | }
647 | ],
648 | "source": [
649 | "#Another method\n",
650 | "print('%s bought %i item(s) at a price of %f each for a total of %f'%(sales_record['person'],\n",
651 | " sales_record['num_items'],\n",
652 | " sales_record['price'],\n",
653 | " sales_record['num_items']*sales_record['price']))\n"
654 | ]
655 | },
656 | {
657 | "cell_type": "code",
658 | "execution_count": null,
659 | "metadata": {
660 | "collapsed": true
661 | },
662 | "outputs": [],
663 | "source": [
664 | "#Exercise: print out \"I took 'a' shots and drank 'b' bottles of beer yesterday night\"\n",
665 | "a = 7\n",
666 | "b = 2"
667 | ]
668 | },
669 | {
670 | "cell_type": "markdown",
671 | "metadata": {},
672 | "source": [
673 | "# Unpacking"
674 | ]
675 | },
676 | {
677 | "cell_type": "markdown",
678 | "metadata": {},
679 | "source": [
680 | "
\n",
681 | "You can unpack a sequence into different variables:"
682 | ]
683 | },
684 | {
685 | "cell_type": "code",
686 | "execution_count": 23,
687 | "metadata": {
688 | "collapsed": true
689 | },
690 | "outputs": [],
691 | "source": [
692 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
693 | "fname, lname, email = x"
694 | ]
695 | },
696 | {
697 | "cell_type": "code",
698 | "execution_count": 24,
699 | "metadata": {
700 | "collapsed": true
701 | },
702 | "outputs": [
703 | {
704 | "data": {
705 | "text/plain": [
706 | "'Christopher'"
707 | ]
708 | },
709 | "execution_count": 24,
710 | "metadata": {},
711 | "output_type": "execute_result"
712 | }
713 | ],
714 | "source": [
715 | "fname"
716 | ]
717 | },
718 | {
719 | "cell_type": "code",
720 | "execution_count": 25,
721 | "metadata": {
722 | "collapsed": true
723 | },
724 | "outputs": [
725 | {
726 | "data": {
727 | "text/plain": [
728 | "'Brooks'"
729 | ]
730 | },
731 | "execution_count": 25,
732 | "metadata": {},
733 | "output_type": "execute_result"
734 | }
735 | ],
736 | "source": [
737 | "lname"
738 | ]
739 | },
740 | {
741 | "cell_type": "markdown",
742 | "metadata": {},
743 | "source": [
744 | "
\n",
745 | "Make sure the number of values you are unpacking matches the number of variables being assigned."
746 | ]
747 | },
748 | {
749 | "cell_type": "code",
750 | "execution_count": null,
751 | "metadata": {
752 | "collapsed": false
753 | },
754 | "outputs": [],
755 | "source": [
756 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com', 'San Diego')\n",
757 | "fname, lname, email = x"
758 | ]
759 | }
760 | ],
761 | "metadata": {
762 | "anaconda-cloud": {},
763 | "kernelspec": {
764 | "display_name": "Python [default]",
765 | "language": "python",
766 | "name": "python3"
767 | },
768 | "language_info": {
769 | "codemirror_mode": {
770 | "name": "ipython",
771 | "version": 3
772 | },
773 | "file_extension": ".py",
774 | "mimetype": "text/x-python",
775 | "name": "python",
776 | "nbconvert_exporter": "python",
777 | "pygments_lexer": "ipython3",
778 | "version": "3.5.2"
779 | }
780 | },
781 | "nbformat": 4,
782 | "nbformat_minor": 0
783 | }
784 |
--------------------------------------------------------------------------------
/Tutorial_2_Loops_and_Indexing/Indexing and loops.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Tutorial 2: Indexing and loops"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "## List indexing\n",
16 | "\n",
17 | "Get a range of values in a list using list indexing\n",
18 | "\n",
19 | "*list_name* **[** *start_index* **:** *stop_index* **:** *step_size* **]**\n",
20 | "\n",
21 | "the index number can be reversed with the **-** sign (eg *-1* is last index)\n",
22 | "\n",
23 | "by default python automates list indexing as **[** *0* **:** *-1* **:** *1* **]**\n",
24 | "\n",
25 | "note that the range will **STOP** before the *stop_index*"
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 21,
31 | "metadata": {
32 | "collapsed": true
33 | },
34 | "outputs": [
35 | {
36 | "name": "stdout",
37 | "output_type": "stream",
38 | "text": [
39 | "a\n",
40 | "['a']\n",
41 | "['a', 'b', 'c']\n",
42 | "['a', 'b', 'c']\n"
43 | ]
44 | }
45 | ],
46 | "source": [
47 | "x = ['a','b','c','d','e']\n",
48 | "print(x[0]) #first character\n",
49 | "print(x[0:1]) #first character, but we have explicitly set the end character\n",
50 | "print(x[0:3]) #first 3 characters\n",
51 | "print(x[:3]) # also first 3 (start index defaults to 0)"
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {},
57 | "source": [
58 | "
\n",
59 | "This will return the last element of the string."
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 13,
65 | "metadata": {
66 | "collapsed": true
67 | },
68 | "outputs": [
69 | {
70 | "data": {
71 | "text/plain": [
72 | "'g'"
73 | ]
74 | },
75 | "execution_count": 13,
76 | "metadata": {},
77 | "output_type": "execute_result"
78 | }
79 | ],
80 | "source": [
81 | "x[-1]"
82 | ]
83 | },
84 | {
85 | "cell_type": "markdown",
86 | "metadata": {},
87 | "source": [
88 | "get the reverse of a list by using negative step size"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 22,
94 | "metadata": {
95 | "collapsed": true
96 | },
97 | "outputs": [
98 | {
99 | "data": {
100 | "text/plain": [
101 | "['e', 'd', 'c', 'b', 'a']"
102 | ]
103 | },
104 | "execution_count": 22,
105 | "metadata": {},
106 | "output_type": "execute_result"
107 | }
108 | ],
109 | "source": [
110 | "x[::-1]"
111 | ]
112 | },
113 | {
114 | "cell_type": "markdown",
115 | "metadata": {},
116 | "source": [
117 | "
\n",
118 | "This will return the slice starting from the 4th element from the end and stopping before the 2nd element from the end."
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": null,
124 | "metadata": {
125 | "collapsed": false
126 | },
127 | "outputs": [],
128 | "source": [
129 | "x[-4:-2]"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {},
135 | "source": [
136 | "
\n",
137 | "This is a slice from the beginning of the string and stopping before the 3rd element."
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "execution_count": 14,
143 | "metadata": {
144 | "collapsed": true
145 | },
146 | "outputs": [
147 | {
148 | "data": {
149 | "text/plain": [
150 | "'Thi'"
151 | ]
152 | },
153 | "execution_count": 14,
154 | "metadata": {},
155 | "output_type": "execute_result"
156 | }
157 | ],
158 | "source": [
159 | "x[:3]"
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "metadata": {},
165 | "source": [
166 | "
\n",
167 | "And this is a slice starting from the 3rd element of the string and going all the way to the end."
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 15,
173 | "metadata": {
174 | "collapsed": true
175 | },
176 | "outputs": [
177 | {
178 | "data": {
179 | "text/plain": [
180 | "'s is a string'"
181 | ]
182 | },
183 | "execution_count": 15,
184 | "metadata": {},
185 | "output_type": "execute_result"
186 | }
187 | ],
188 | "source": [
189 | "x[3:]"
190 | ]
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {},
195 | "source": [
196 | "
\n",
197 | "Strings are a list of characters"
198 | ]
199 | },
200 | {
201 | "cell_type": "code",
202 | "execution_count": null,
203 | "metadata": {
204 | "collapsed": true
205 | },
206 | "outputs": [],
207 | "source": [
208 | "a = \"hello world!\"\n",
209 | "a[2:5]"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": 9,
215 | "metadata": {
216 | "collapsed": true
217 | },
218 | "outputs": [],
219 | "source": [
220 | "#EXERCISE: set y to your name and print out characters from y one at time using for loop\n"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": 11,
226 | "metadata": {
227 | "collapsed": false
228 | },
229 | "outputs": [],
230 | "source": [
231 | "#EXERCISE: set variable name to your full name and print out the reverse of your last name using list slicing\n",
232 | "name = "
233 | ]
234 | },
235 | {
236 | "cell_type": "markdown",
237 | "metadata": {},
238 | "source": [
239 | "# Loops \n",
240 | "\n",
241 | "Loops are used to execuate a code for multiple times\n",
242 | "\n",
243 | "### while loops \n",
244 | "\n",
245 | "while loops are used to execute a block of code until the initial conidtion becomes True\n",
246 | "\n",
247 | "\n",
248 | "**while** *condition* **:**\n",
249 | " \n",
250 | " **\n",
251 | " \n",
252 | " **\n",
253 | " \n",
254 | " *......*\n",
255 | " \n",
256 | "**warning** if a conidition is never reached, while loops will run forever"
257 | ]
258 | },
259 | {
260 | "cell_type": "code",
261 | "execution_count": null,
262 | "metadata": {
263 | "collapsed": true
264 | },
265 | "outputs": [],
266 | "source": [
267 | "a = 0\n",
268 | "while a != 10: #!= (not equals to), == (equalts to), > (greater than), < (less than) = (variable assignment)\n",
269 | " a += 1 #+= (same as : a = a + 1 )\n",
270 | "print(a)"
271 | ]
272 | },
273 | {
274 | "cell_type": "markdown",
275 | "metadata": {},
276 | "source": [
277 | "### for loops\n",
278 | "\n",
279 | "for loops get each item in a list, give it a temporary variable name, and apply the indend lines of code after **:** \n",
280 | "\n",
281 | "**for** *variable_name* **in** *list* **:**\n",
282 | " \n",
283 | " **\n",
284 | " \n",
285 | " **\n",
286 | " \n",
287 | " *......*"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": 25,
293 | "metadata": {
294 | "collapsed": true
295 | },
296 | "outputs": [
297 | {
298 | "name": "stdout",
299 | "output_type": "stream",
300 | "text": [
301 | "dogs\n",
302 | "-----\n",
303 | "cats\n",
304 | "-----\n",
305 | "elephants\n",
306 | "-----\n",
307 | "kardashians\n",
308 | "-----\n"
309 | ]
310 | }
311 | ],
312 | "source": [
313 | "zoo = ['dogs','cats','elephants','kardashians']\n",
314 | "for animals in zoo:\n",
315 | " print(animals)\n",
316 | " print(\"-----\")"
317 | ]
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "metadata": {},
322 | "source": [
323 | "The **range** operator can be used to set a numeric range for the for loop inplace of a list\n",
324 | "\n",
325 | "**for** *variable_name* **in** **range(** *start_number*, *end_number*, *step_size* **)** **:**\n",
326 | " \n",
327 | " **\n",
328 | " \n",
329 | " **\n",
330 | " \n",
331 | " *......*"
332 | ]
333 | },
334 | {
335 | "cell_type": "code",
336 | "execution_count": null,
337 | "metadata": {
338 | "collapsed": true
339 | },
340 | "outputs": [],
341 | "source": [
342 | "for number in range(0,10,2):\n",
343 | " print(number)"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "For loops with ranges can also be used to repeat an operation certain number of times"
351 | ]
352 | },
353 | {
354 | "cell_type": "code",
355 | "execution_count": null,
356 | "metadata": {
357 | "collapsed": true
358 | },
359 | "outputs": [],
360 | "source": [
361 | "a = 0 \n",
362 | "for count in range(10):\n",
363 | " a += 1 \n",
364 | "print(a)"
365 | ]
366 | },
367 | {
368 | "cell_type": "markdown",
369 | "metadata": {},
370 | "source": [
371 | "
\n",
372 | "while loops can be used to index a list, but it is still prepered to use a for loop"
373 | ]
374 | },
375 | {
376 | "cell_type": "code",
377 | "execution_count": null,
378 | "metadata": {
379 | "collapsed": false
380 | },
381 | "outputs": [],
382 | "source": [
383 | "i=0\n",
384 | "while( i != len(x) ):\n",
385 | " print(x[i])\n",
386 | " i = i + 1"
387 | ]
388 | },
389 | {
390 | "cell_type": "code",
391 | "execution_count": 9,
392 | "metadata": {
393 | "collapsed": true
394 | },
395 | "outputs": [],
396 | "source": [
397 | "#EXERCISE: print out characters from y one at time using while loop\n"
398 | ]
399 | },
400 | {
401 | "cell_type": "markdown",
402 | "metadata": {},
403 | "source": [
404 | "There are many ways to loops through a dictionary"
405 | ]
406 | },
407 | {
408 | "cell_type": "markdown",
409 | "metadata": {},
410 | "source": [
411 | "
\n",
412 | "Iterate over all of the keys:"
413 | ]
414 | },
415 | {
416 | "cell_type": "code",
417 | "execution_count": 19,
418 | "metadata": {
419 | "collapsed": true
420 | },
421 | "outputs": [
422 | {
423 | "name": "stdout",
424 | "output_type": "stream",
425 | "text": [
426 | "None\n",
427 | "billg@microsoft.com\n",
428 | "brooksch@umich.edu\n"
429 | ]
430 | }
431 | ],
432 | "source": [
433 | "x = {'Mars':'marshuang80@gmail.com','Jupiter':'jupiterhuang80@gmail.com','Pluto':'plutohuang80@gmail.com'}\n",
434 | "for name in x:\n",
435 | " print(x[name])"
436 | ]
437 | },
438 | {
439 | "cell_type": "markdown",
440 | "metadata": {},
441 | "source": [
442 | "
\n",
443 | "Iterate over all of the values of a dictionary:\n",
444 | "\n",
445 | "**for** *variable_name* **in** *dictionary_name* **.values( )**:"
446 | ]
447 | },
448 | {
449 | "cell_type": "code",
450 | "execution_count": 20,
451 | "metadata": {
452 | "collapsed": true
453 | },
454 | "outputs": [
455 | {
456 | "name": "stdout",
457 | "output_type": "stream",
458 | "text": [
459 | "None\n",
460 | "billg@microsoft.com\n",
461 | "brooksch@umich.edu\n"
462 | ]
463 | }
464 | ],
465 | "source": [
466 | "for email in x.values():\n",
467 | " print(email)"
468 | ]
469 | },
470 | {
471 | "cell_type": "markdown",
472 | "metadata": {},
473 | "source": [
474 | "
\n",
475 | "Iterate over all of the keys and values in the dictionary:\n",
476 | "\n",
477 | "**for** *key* **,** *variable_name* **in** *dictionary_name* **.values( )**:"
478 | ]
479 | },
480 | {
481 | "cell_type": "code",
482 | "execution_count": 21,
483 | "metadata": {
484 | "collapsed": true
485 | },
486 | "outputs": [
487 | {
488 | "name": "stdout",
489 | "output_type": "stream",
490 | "text": [
491 | "Kevyn Collins-Thompson\n",
492 | "None\n",
493 | "Bill Gates\n",
494 | "billg@microsoft.com\n",
495 | "Christopher Brooks\n",
496 | "brooksch@umich.edu\n"
497 | ]
498 | }
499 | ],
500 | "source": [
501 | "for name, email in x.items():\n",
502 | " print(name)\n",
503 | " print(email)"
504 | ]
505 | },
506 | {
507 | "cell_type": "markdown",
508 | "metadata": {},
509 | "source": [
510 | "
\n",
511 | "# Strings and Printing"
512 | ]
513 | },
514 | {
515 | "cell_type": "code",
516 | "execution_count": null,
517 | "metadata": {
518 | "collapsed": false
519 | },
520 | "outputs": [],
521 | "source": [
522 | "firstname = 'Mars'\n",
523 | "lastname = 'Huang'\n",
524 | "\n",
525 | "print(firstname + ' ' + lastname) #joining strings together\n",
526 | "print(firstname*3) #repeating a string 3 times \n",
527 | "print('Mars' in firstname) #bool\n"
528 | ]
529 | },
530 | {
531 | "cell_type": "code",
532 | "execution_count": 14,
533 | "metadata": {
534 | "collapsed": true
535 | },
536 | "outputs": [
537 | {
538 | "name": "stdout",
539 | "output_type": "stream",
540 | "text": [
541 | "1\n",
542 | "2\n"
543 | ]
544 | }
545 | ],
546 | "source": [
547 | "##EXERCISE: print the following\n",
548 | "# print a,b and c on the same line\n",
549 | "a = 1\n",
550 | "b = 2\n",
551 | "c = 3\n"
552 | ]
553 | },
554 | {
555 | "cell_type": "code",
556 | "execution_count": 28,
557 | "metadata": {
558 | "collapsed": false,
559 | "scrolled": true
560 | },
561 | "outputs": [
562 | {
563 | "name": "stdout",
564 | "output_type": "stream",
565 | "text": [
566 | "[1, 2, 3, 4, 3, 4]\n"
567 | ]
568 | }
569 | ],
570 | "source": [
571 | "##EXERCISE\n",
572 | "# print lists a,b and c on the same line as seperate lists\n",
573 | "a = [1,2]\n",
574 | "b = [3,4]\n",
575 | "c = [5,6]"
576 | ]
577 | },
578 | {
579 | "cell_type": "markdown",
580 | "metadata": {},
581 | "source": [
582 | "Since **+** joins a list, use **,** to seperate lists while printing"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 29,
588 | "metadata": {
589 | "collapsed": true
590 | },
591 | "outputs": [
592 | {
593 | "name": "stdout",
594 | "output_type": "stream",
595 | "text": [
596 | "[1, 2] [3, 4] [5, 6]\n"
597 | ]
598 | }
599 | ],
600 | "source": [
601 | "a = [1,2]\n",
602 | "b = [3,4]\n",
603 | "c = [5,6]\n",
604 | "print(a,b,c)"
605 | ]
606 | },
607 | {
608 | "cell_type": "code",
609 | "execution_count": null,
610 | "metadata": {
611 | "collapsed": true
612 | },
613 | "outputs": [],
614 | "source": [
615 | "##EXERCISE\n",
616 | "# print lists the frist 4 elements of list A and the reverse of last 3 elements of list B as one list\n",
617 | "A = [0,1,2,3,4,5,6,7,8,9]\n",
618 | "B = [14,13,12,11,10]"
619 | ]
620 | },
621 | {
622 | "cell_type": "markdown",
623 | "metadata": {},
624 | "source": [
625 | "
\n",
626 | "`split` returns a list of all the words in a string, or a list split on a specific character."
627 | ]
628 | },
629 | {
630 | "cell_type": "code",
631 | "execution_count": 16,
632 | "metadata": {
633 | "collapsed": true
634 | },
635 | "outputs": [
636 | {
637 | "name": "stdout",
638 | "output_type": "stream",
639 | "text": [
640 | "['Mars', 'Shih-Cheng', 'Huang']\n"
641 | ]
642 | }
643 | ],
644 | "source": [
645 | "name = 'Mars Shih-Cheng Huang'\n",
646 | "print(name.split(' '))"
647 | ]
648 | },
649 | {
650 | "cell_type": "code",
651 | "execution_count": 30,
652 | "metadata": {
653 | "collapsed": true
654 | },
655 | "outputs": [
656 | {
657 | "name": "stdout",
658 | "output_type": "stream",
659 | "text": [
660 | "['Mars Shih-Cheng Huang']\n"
661 | ]
662 | }
663 | ],
664 | "source": [
665 | "name = 'Mars Shih-Cheng Huang'\n",
666 | "print(name.split(','))"
667 | ]
668 | },
669 | {
670 | "cell_type": "code",
671 | "execution_count": null,
672 | "metadata": {
673 | "collapsed": true
674 | },
675 | "outputs": [],
676 | "source": [
677 | "#print out my last name and first name from \"name\" using list indexing and split"
678 | ]
679 | },
680 | {
681 | "cell_type": "markdown",
682 | "metadata": {},
683 | "source": [
684 | "
\n",
685 | "Make sure you convert objects to strings before concatenating."
686 | ]
687 | },
688 | {
689 | "cell_type": "code",
690 | "execution_count": null,
691 | "metadata": {
692 | "collapsed": false
693 | },
694 | "outputs": [],
695 | "source": [
696 | "'Mars' + 2"
697 | ]
698 | },
699 | {
700 | "cell_type": "code",
701 | "execution_count": null,
702 | "metadata": {
703 | "collapsed": false
704 | },
705 | "outputs": [],
706 | "source": [
707 | "'Mars' + str(2)"
708 | ]
709 | },
710 | {
711 | "cell_type": "code",
712 | "execution_count": 17,
713 | "metadata": {
714 | "collapsed": true
715 | },
716 | "outputs": [],
717 | "source": [
718 | "#Exercise: Create a dictionary called classes, with the classes you're taking as key, \n",
719 | "#and the grade that your want to recieve as index\n"
720 | ]
721 | },
722 | {
723 | "cell_type": "code",
724 | "execution_count": 18,
725 | "metadata": {
726 | "collapsed": true
727 | },
728 | "outputs": [],
729 | "source": [
730 | "#Exercise: print out all key, values from your diciotnary in the following format:\n",
731 | "#\"The grade that I want to revieve for ____ class is ____\""
732 | ]
733 | },
734 | {
735 | "cell_type": "markdown",
736 | "metadata": {},
737 | "source": [
738 | "
\n",
739 | "You can unpack a sequence into different variables:"
740 | ]
741 | },
742 | {
743 | "cell_type": "code",
744 | "execution_count": 23,
745 | "metadata": {
746 | "collapsed": true
747 | },
748 | "outputs": [],
749 | "source": [
750 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
751 | "fname, lname, email = x"
752 | ]
753 | },
754 | {
755 | "cell_type": "code",
756 | "execution_count": 24,
757 | "metadata": {
758 | "collapsed": true
759 | },
760 | "outputs": [
761 | {
762 | "data": {
763 | "text/plain": [
764 | "'Christopher'"
765 | ]
766 | },
767 | "execution_count": 24,
768 | "metadata": {},
769 | "output_type": "execute_result"
770 | }
771 | ],
772 | "source": [
773 | "fname"
774 | ]
775 | },
776 | {
777 | "cell_type": "code",
778 | "execution_count": 25,
779 | "metadata": {
780 | "collapsed": true
781 | },
782 | "outputs": [
783 | {
784 | "data": {
785 | "text/plain": [
786 | "'Brooks'"
787 | ]
788 | },
789 | "execution_count": 25,
790 | "metadata": {},
791 | "output_type": "execute_result"
792 | }
793 | ],
794 | "source": [
795 | "lname"
796 | ]
797 | },
798 | {
799 | "cell_type": "code",
800 | "execution_count": null,
801 | "metadata": {
802 | "collapsed": false
803 | },
804 | "outputs": [],
805 | "source": [
806 | "print('Mars' + 2)"
807 | ]
808 | },
809 | {
810 | "cell_type": "code",
811 | "execution_count": null,
812 | "metadata": {
813 | "collapsed": false
814 | },
815 | "outputs": [],
816 | "source": [
817 | "print('Mars' + str(2))"
818 | ]
819 | },
820 | {
821 | "cell_type": "markdown",
822 | "metadata": {},
823 | "source": [
824 | "
\n",
825 | "Python has a built in method for convenient string formatting."
826 | ]
827 | },
828 | {
829 | "cell_type": "code",
830 | "execution_count": 19,
831 | "metadata": {
832 | "collapsed": true
833 | },
834 | "outputs": [
835 | {
836 | "name": "stdout",
837 | "output_type": "stream",
838 | "text": [
839 | "Chris bought 4 item(s) at a price of 3.24 each for a total of 12.96\n"
840 | ]
841 | }
842 | ],
843 | "source": [
844 | "sales_record = {\n",
845 | "'price': 3.24,\n",
846 | "'num_items': 4,\n",
847 | "'person': 'Chris'}\n",
848 | "\n",
849 | "sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}'\n",
850 | "\n",
851 | "print(sales_statement.format(sales_record['person'],\n",
852 | " sales_record['num_items'],\n",
853 | " sales_record['price'],\n",
854 | " sales_record['num_items']*sales_record['price']))\n"
855 | ]
856 | },
857 | {
858 | "cell_type": "code",
859 | "execution_count": 20,
860 | "metadata": {
861 | "collapsed": true
862 | },
863 | "outputs": [
864 | {
865 | "name": "stdout",
866 | "output_type": "stream",
867 | "text": [
868 | "Chris bought 4 item(s) at a price of 3.240000 each for a total of 12.960000\n"
869 | ]
870 | }
871 | ],
872 | "source": [
873 | "#Another method\n",
874 | "print('%s bought %i item(s) at a price of %f each for a total of %f'%(sales_record['person'],\n",
875 | " sales_record['num_items'],\n",
876 | " sales_record['price'],\n",
877 | " sales_record['num_items']*sales_record['price']))\n"
878 | ]
879 | },
880 | {
881 | "cell_type": "code",
882 | "execution_count": null,
883 | "metadata": {
884 | "collapsed": true
885 | },
886 | "outputs": [],
887 | "source": [
888 | "#Exercise: print out \"I took 'a' shots and drank 'b' bottles of beer yesterday night\"\n",
889 | "a = 7\n",
890 | "b = 2"
891 | ]
892 | },
893 | {
894 | "cell_type": "markdown",
895 | "metadata": {},
896 | "source": [
897 | "# Unpacking"
898 | ]
899 | },
900 | {
901 | "cell_type": "markdown",
902 | "metadata": {},
903 | "source": [
904 | "
\n",
905 | "You can unpack a sequence into different variables:"
906 | ]
907 | },
908 | {
909 | "cell_type": "code",
910 | "execution_count": 23,
911 | "metadata": {
912 | "collapsed": true
913 | },
914 | "outputs": [],
915 | "source": [
916 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
917 | "fname, lname, email = x"
918 | ]
919 | },
920 | {
921 | "cell_type": "code",
922 | "execution_count": 24,
923 | "metadata": {
924 | "collapsed": true
925 | },
926 | "outputs": [
927 | {
928 | "data": {
929 | "text/plain": [
930 | "'Christopher'"
931 | ]
932 | },
933 | "execution_count": 24,
934 | "metadata": {},
935 | "output_type": "execute_result"
936 | }
937 | ],
938 | "source": [
939 | "fname"
940 | ]
941 | },
942 | {
943 | "cell_type": "code",
944 | "execution_count": 25,
945 | "metadata": {
946 | "collapsed": true
947 | },
948 | "outputs": [
949 | {
950 | "data": {
951 | "text/plain": [
952 | "'Brooks'"
953 | ]
954 | },
955 | "execution_count": 25,
956 | "metadata": {},
957 | "output_type": "execute_result"
958 | }
959 | ],
960 | "source": [
961 | "lname"
962 | ]
963 | },
964 | {
965 | "cell_type": "markdown",
966 | "metadata": {},
967 | "source": [
968 | "
\n",
969 | "Make sure the number of values you are unpacking matches the number of variables being assigned."
970 | ]
971 | },
972 | {
973 | "cell_type": "code",
974 | "execution_count": null,
975 | "metadata": {
976 | "collapsed": false
977 | },
978 | "outputs": [],
979 | "source": [
980 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com', 'San Diego')\n",
981 | "fname, lname, email = x"
982 | ]
983 | }
984 | ],
985 | "metadata": {
986 | "anaconda-cloud": {},
987 | "kernelspec": {
988 | "display_name": "Python [default]",
989 | "language": "python",
990 | "name": "python3"
991 | },
992 | "language_info": {
993 | "codemirror_mode": {
994 | "name": "ipython",
995 | "version": 3
996 | },
997 | "file_extension": ".py",
998 | "mimetype": "text/x-python",
999 | "name": "python",
1000 | "nbconvert_exporter": "python",
1001 | "pygments_lexer": "ipython3",
1002 | "version": "3.5.2"
1003 | }
1004 | },
1005 | "nbformat": 4,
1006 | "nbformat_minor": 0
1007 | }
1008 |
--------------------------------------------------------------------------------
/Tutorial_3_Conditions_and_Functions/.ipynb_checkpoints/Conditions and Functions-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 2"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 17,
14 | "metadata": {
15 | "collapsed": true
16 | },
17 | "outputs": [],
18 | "source": [
19 | "#Review Exercise: Create a dictionary called classes, with the classes you're taking as key, and the grade that your want to recieve as index"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 18,
25 | "metadata": {
26 | "collapsed": true
27 | },
28 | "outputs": [],
29 | "source": [
30 | "#Review Exercise: Exercise: print out all key, values from your diciotnary in the following format:\n",
31 | "#\"The grade that I want to revieve for ____ class is ____\""
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 2,
37 | "metadata": {
38 | "collapsed": false
39 | },
40 | "outputs": [
41 | {
42 | "name": "stdout",
43 | "output_type": "stream",
44 | "text": [
45 | "gaHsa\n"
46 | ]
47 | }
48 | ],
49 | "source": [
50 | "#Review Exercise: Store your full name in variable \"name\" and print out the reverse of the even characters\n",
51 | "# eg Mars_Huang = gaHsa\n",
52 | "name = 'Mars_Huang'\n",
53 | "print(name[::-2])"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "# Conditional Statements\n",
61 | "In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language, which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. - Wiki\n",
62 | "\n",
63 | "The **\"if\"** statement:\n",
64 | "In python, conditional statment is followed by the \"**:**\", and the actions are indented after the if statements.\n",
65 | "\n",
66 | "**if** *condition* **:**\n",
67 | " \n",
68 | " **action**\n",
69 | " \n",
70 | " **action**"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 1,
76 | "metadata": {
77 | "collapsed": true
78 | },
79 | "outputs": [
80 | {
81 | "name": "stdout",
82 | "output_type": "stream",
83 | "text": [
84 | "a is greater than 0\n",
85 | "end of if statement\n"
86 | ]
87 | }
88 | ],
89 | "source": [
90 | "a = 3 \n",
91 | "if a > 0: \n",
92 | " print(\"a is greater than 0\")\n",
93 | " print(\"end of if statement\")"
94 | ]
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "metadata": {},
99 | "source": [
100 | "Sometime you want to have an alternative action if the condition fails. You will want to use the if...else.. sytanx\n",
101 | "\n",
102 | "\n",
103 | "**if** *condition* **:**\n",
104 | " \n",
105 | " **action**\n",
106 | " \n",
107 | "** else: ** \n",
108 | " \n",
109 | " **action**"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": 2,
115 | "metadata": {
116 | "collapsed": true
117 | },
118 | "outputs": [
119 | {
120 | "name": "stdout",
121 | "output_type": "stream",
122 | "text": [
123 | "a is larger than 0\n"
124 | ]
125 | }
126 | ],
127 | "source": [
128 | "a = 2 \n",
129 | "if a < 0: \n",
130 | " print(\"a is smaller than 0\")\n",
131 | "else:\n",
132 | " print(\"a is larger than 0\")"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "You will also face situations where more than one conditions are nessasary. You will be using the if...elif...else syntax\n",
140 | "\n",
141 | "Sometime you want to have an alternative action if the condition fails: \n",
142 | "\n",
143 | "\n",
144 | "**if** *condition* **:**\n",
145 | " \n",
146 | " **action**\n",
147 | " \n",
148 | "** elif: ** \n",
149 | " \n",
150 | " **action**\n",
151 | " \n",
152 | "** else: ** \n",
153 | " \n",
154 | " **action**"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 3,
160 | "metadata": {
161 | "collapsed": true
162 | },
163 | "outputs": [
164 | {
165 | "name": "stdout",
166 | "output_type": "stream",
167 | "text": [
168 | "a is 0\n"
169 | ]
170 | }
171 | ],
172 | "source": [
173 | "a = 0\n",
174 | "if a < 0: \n",
175 | " print(\"a is smaller than 0\")\n",
176 | "elif a == 0:\n",
177 | " print(\"a is 0\")\n",
178 | "else: \n",
179 | " print(\"a is larger than 0\")"
180 | ]
181 | },
182 | {
183 | "cell_type": "markdown",
184 | "metadata": {},
185 | "source": [
186 | "
\n",
187 | "# Unpacking\n",
188 | "You can unpack a sequence into different variables:"
189 | ]
190 | },
191 | {
192 | "cell_type": "code",
193 | "execution_count": 3,
194 | "metadata": {
195 | "collapsed": true
196 | },
197 | "outputs": [],
198 | "source": [
199 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
200 | "fname, lname, email = x"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 4,
206 | "metadata": {
207 | "collapsed": false
208 | },
209 | "outputs": [
210 | {
211 | "data": {
212 | "text/plain": [
213 | "'Mars'"
214 | ]
215 | },
216 | "execution_count": 4,
217 | "metadata": {},
218 | "output_type": "execute_result"
219 | }
220 | ],
221 | "source": [
222 | "fname"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": 5,
228 | "metadata": {
229 | "collapsed": false
230 | },
231 | "outputs": [
232 | {
233 | "data": {
234 | "text/plain": [
235 | "'Huang'"
236 | ]
237 | },
238 | "execution_count": 5,
239 | "metadata": {},
240 | "output_type": "execute_result"
241 | }
242 | ],
243 | "source": [
244 | "lname"
245 | ]
246 | },
247 | {
248 | "cell_type": "markdown",
249 | "metadata": {},
250 | "source": [
251 | "
\n",
252 | "Make sure the number of values you are unpacking matches the number of variables being assigned."
253 | ]
254 | },
255 | {
256 | "cell_type": "code",
257 | "execution_count": 6,
258 | "metadata": {
259 | "collapsed": true
260 | },
261 | "outputs": [
262 | {
263 | "ename": "ValueError",
264 | "evalue": "too many values to unpack (expected 3)",
265 | "output_type": "error",
266 | "traceback": [
267 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
268 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
269 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'Mars'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Huang'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'marshuang80@gmail.com'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'San Diego'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0memail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
270 | "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 3)"
271 | ]
272 | }
273 | ],
274 | "source": [
275 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com', 'San Diego')\n",
276 | "fname, lname, email = x"
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {},
282 | "source": [
283 | "You can also unpack from a list"
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "execution_count": 7,
289 | "metadata": {
290 | "collapsed": true
291 | },
292 | "outputs": [],
293 | "source": [
294 | "x = ['Mars','Huang','marshuang80@gmail.com']\n",
295 | "fname, lname, email = x"
296 | ]
297 | },
298 | {
299 | "cell_type": "markdown",
300 | "metadata": {},
301 | "source": [
302 | "# The Python Programming Language: Functions"
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": 1,
308 | "metadata": {
309 | "collapsed": true
310 | },
311 | "outputs": [
312 | {
313 | "data": {
314 | "text/plain": [
315 | "3"
316 | ]
317 | },
318 | "execution_count": 1,
319 | "metadata": {},
320 | "output_type": "execute_result"
321 | }
322 | ],
323 | "source": [
324 | "x = 1\n",
325 | "y = 2\n",
326 | "x + y"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": 2,
332 | "metadata": {
333 | "collapsed": true
334 | },
335 | "outputs": [
336 | {
337 | "data": {
338 | "text/plain": [
339 | "1"
340 | ]
341 | },
342 | "execution_count": 2,
343 | "metadata": {},
344 | "output_type": "execute_result"
345 | }
346 | ],
347 | "source": [
348 | "x"
349 | ]
350 | },
351 | {
352 | "cell_type": "markdown",
353 | "metadata": {},
354 | "source": [
355 | "
\n",
356 | "`add_numbers` is a function that takes two numbers and adds them together."
357 | ]
358 | },
359 | {
360 | "cell_type": "code",
361 | "execution_count": 3,
362 | "metadata": {
363 | "collapsed": true
364 | },
365 | "outputs": [
366 | {
367 | "data": {
368 | "text/plain": [
369 | "3"
370 | ]
371 | },
372 | "execution_count": 3,
373 | "metadata": {},
374 | "output_type": "execute_result"
375 | }
376 | ],
377 | "source": [
378 | "def add_numbers(x, y):\n",
379 | " return x + y\n",
380 | "\n",
381 | "add_numbers(1, 2)"
382 | ]
383 | },
384 | {
385 | "cell_type": "markdown",
386 | "metadata": {},
387 | "source": [
388 | "
\n",
389 | "`add_numbers` updated to take an optional 3rd parameter. Using `print` allows printing of multiple expressions within a single cell."
390 | ]
391 | },
392 | {
393 | "cell_type": "code",
394 | "execution_count": null,
395 | "metadata": {
396 | "collapsed": false
397 | },
398 | "outputs": [],
399 | "source": [
400 | "def add_numbers(x,y,z=None):\n",
401 | " if (z==None):\n",
402 | " return x+y\n",
403 | " else:\n",
404 | " return x+y+z\n",
405 | "\n",
406 | "print(add_numbers(1, 2))\n",
407 | "print(add_numbers(1, 2, 3))"
408 | ]
409 | },
410 | {
411 | "cell_type": "markdown",
412 | "metadata": {},
413 | "source": [
414 | "
\n",
415 | "`add_numbers` updated to take an optional flag parameter."
416 | ]
417 | },
418 | {
419 | "cell_type": "code",
420 | "execution_count": 12,
421 | "metadata": {
422 | "collapsed": false
423 | },
424 | "outputs": [
425 | {
426 | "name": "stdout",
427 | "output_type": "stream",
428 | "text": [
429 | "Flag is true!\n",
430 | "3\n"
431 | ]
432 | }
433 | ],
434 | "source": [
435 | "def add_numbers(x, y, z=None, flag=False):\n",
436 | " if (flag):\n",
437 | " print('Flag is true!')\n",
438 | " if (z==None):\n",
439 | " return x + y\n",
440 | " else:\n",
441 | " return x + y + z\n",
442 | " \n",
443 | "print(add_numbers(1, 2, flag=True))"
444 | ]
445 | },
446 | {
447 | "cell_type": "markdown",
448 | "metadata": {},
449 | "source": [
450 | "
\n",
451 | "Assign function `add_numbers` to variable `a`."
452 | ]
453 | },
454 | {
455 | "cell_type": "code",
456 | "execution_count": 4,
457 | "metadata": {
458 | "collapsed": true
459 | },
460 | "outputs": [
461 | {
462 | "data": {
463 | "text/plain": [
464 | "3"
465 | ]
466 | },
467 | "execution_count": 4,
468 | "metadata": {},
469 | "output_type": "execute_result"
470 | }
471 | ],
472 | "source": [
473 | "def add_numbers(x,y):\n",
474 | " return x+y\n",
475 | "\n",
476 | "a = add_numbers\n",
477 | "a(1,2)"
478 | ]
479 | },
480 | {
481 | "cell_type": "markdown",
482 | "metadata": {},
483 | "source": [
484 | "You can also use unpacking for functions"
485 | ]
486 | },
487 | {
488 | "cell_type": "code",
489 | "execution_count": 3,
490 | "metadata": {
491 | "collapsed": true
492 | },
493 | "outputs": [
494 | {
495 | "name": "stdout",
496 | "output_type": "stream",
497 | "text": [
498 | "0.6666666666666666\n",
499 | "6\n"
500 | ]
501 | }
502 | ],
503 | "source": [
504 | "def div_and_mul(x,y):\n",
505 | " return x/y, x*y\n",
506 | "a,b = div_and_mul(2,3)\n",
507 | "print(a)\n",
508 | "print(b)"
509 | ]
510 | },
511 | {
512 | "cell_type": "code",
513 | "execution_count": 22,
514 | "metadata": {
515 | "collapsed": true
516 | },
517 | "outputs": [],
518 | "source": [
519 | "##Quiz\n",
520 | "#make a function that has two parameters: a list of numbers \"num\" a second number \"val\"\n",
521 | "#return the sum of all odd numbers in num, minus the sum of all even numbers, \n",
522 | "#and multiply by val if a second parameter is given\n",
523 | "num = [14,7,2,4,5,6,9,13,17,4,16,9,0,3]\n",
524 | "\n",
525 | "#write function below\n",
526 | "\n",
527 | "#two test cases:\n",
528 | "print(get_result(num))\n",
529 | "print(get_result(num,7)"
530 | ]
531 | }
532 | ],
533 | "metadata": {
534 | "anaconda-cloud": {},
535 | "kernelspec": {
536 | "display_name": "Python [default]",
537 | "language": "python",
538 | "name": "python3"
539 | },
540 | "language_info": {
541 | "codemirror_mode": {
542 | "name": "ipython",
543 | "version": 3
544 | },
545 | "file_extension": ".py",
546 | "mimetype": "text/x-python",
547 | "name": "python",
548 | "nbconvert_exporter": "python",
549 | "pygments_lexer": "ipython3",
550 | "version": "3.5.2"
551 | }
552 | },
553 | "nbformat": 4,
554 | "nbformat_minor": 0
555 | }
556 |
--------------------------------------------------------------------------------
/Tutorial_3_Conditions_and_Functions/.ipynb_checkpoints/Untitled-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [],
3 | "metadata": {},
4 | "nbformat": 4,
5 | "nbformat_minor": 1
6 | }
7 |
--------------------------------------------------------------------------------
/Tutorial_3_Conditions_and_Functions/Conditions and Functions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 2"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 17,
14 | "metadata": {
15 | "collapsed": true
16 | },
17 | "outputs": [],
18 | "source": [
19 | "#Review Exercise: Create a dictionary called classes, with the classes you're taking as key, and the grade that your want to recieve as index"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 18,
25 | "metadata": {
26 | "collapsed": true
27 | },
28 | "outputs": [],
29 | "source": [
30 | "#Review Exercise: Exercise: print out all key, values from your diciotnary in the following format:\n",
31 | "#\"The grade that I want to revieve for ____ class is ____\""
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 2,
37 | "metadata": {
38 | "collapsed": false
39 | },
40 | "outputs": [
41 | {
42 | "name": "stdout",
43 | "output_type": "stream",
44 | "text": [
45 | "gaHsa\n"
46 | ]
47 | }
48 | ],
49 | "source": [
50 | "#Review Exercise: Store your full name in variable \"name\" and print out the reverse of the even characters\n",
51 | "# eg Mars_Huang = gaHsa\n",
52 | "name = 'Mars_Huang'\n",
53 | "print(name[::-2])"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "# Conditional Statements\n",
61 | "In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language, which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. - Wiki\n",
62 | "\n",
63 | "The **\"if\"** statement:\n",
64 | "In python, conditional statment is followed by the \"**:**\", and the actions are indented after the if statements.\n",
65 | "\n",
66 | "**if** *condition* **:**\n",
67 | " \n",
68 | " **action**\n",
69 | " \n",
70 | " **action**"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 1,
76 | "metadata": {
77 | "collapsed": true
78 | },
79 | "outputs": [
80 | {
81 | "name": "stdout",
82 | "output_type": "stream",
83 | "text": [
84 | "a is greater than 0\n",
85 | "end of if statement\n"
86 | ]
87 | }
88 | ],
89 | "source": [
90 | "a = 3 \n",
91 | "if a > 0: \n",
92 | " print(\"a is greater than 0\")\n",
93 | " print(\"end of if statement\")"
94 | ]
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "metadata": {},
99 | "source": [
100 | "Sometime you want to have an alternative action if the condition fails. You will want to use the if...else.. sytanx\n",
101 | "\n",
102 | "\n",
103 | "**if** *condition* **:**\n",
104 | " \n",
105 | " **action**\n",
106 | " \n",
107 | "** else: ** \n",
108 | " \n",
109 | " **action**"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": 2,
115 | "metadata": {
116 | "collapsed": true
117 | },
118 | "outputs": [
119 | {
120 | "name": "stdout",
121 | "output_type": "stream",
122 | "text": [
123 | "a is larger than 0\n"
124 | ]
125 | }
126 | ],
127 | "source": [
128 | "a = 2 \n",
129 | "if a < 0: \n",
130 | " print(\"a is smaller than 0\")\n",
131 | "else:\n",
132 | " print(\"a is larger than 0\")"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "You will also face situations where more than one conditions are nessasary. You will be using the if...elif...else syntax\n",
140 | "\n",
141 | "Sometime you want to have an alternative action if the condition fails: \n",
142 | "\n",
143 | "\n",
144 | "**if** *condition* **:**\n",
145 | " \n",
146 | " **action**\n",
147 | " \n",
148 | "** elif: ** \n",
149 | " \n",
150 | " **action**\n",
151 | " \n",
152 | "** else: ** \n",
153 | " \n",
154 | " **action**"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 3,
160 | "metadata": {
161 | "collapsed": true
162 | },
163 | "outputs": [
164 | {
165 | "name": "stdout",
166 | "output_type": "stream",
167 | "text": [
168 | "a is 0\n"
169 | ]
170 | }
171 | ],
172 | "source": [
173 | "a = 0\n",
174 | "if a < 0: \n",
175 | " print(\"a is smaller than 0\")\n",
176 | "elif a == 0:\n",
177 | " print(\"a is 0\")\n",
178 | "else: \n",
179 | " print(\"a is larger than 0\")"
180 | ]
181 | },
182 | {
183 | "cell_type": "markdown",
184 | "metadata": {},
185 | "source": [
186 | "
\n",
187 | "# Unpacking\n",
188 | "You can unpack a sequence into different variables:"
189 | ]
190 | },
191 | {
192 | "cell_type": "code",
193 | "execution_count": 3,
194 | "metadata": {
195 | "collapsed": true
196 | },
197 | "outputs": [],
198 | "source": [
199 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com')\n",
200 | "fname, lname, email = x"
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 4,
206 | "metadata": {
207 | "collapsed": false
208 | },
209 | "outputs": [
210 | {
211 | "data": {
212 | "text/plain": [
213 | "'Mars'"
214 | ]
215 | },
216 | "execution_count": 4,
217 | "metadata": {},
218 | "output_type": "execute_result"
219 | }
220 | ],
221 | "source": [
222 | "fname"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": 5,
228 | "metadata": {
229 | "collapsed": false
230 | },
231 | "outputs": [
232 | {
233 | "data": {
234 | "text/plain": [
235 | "'Huang'"
236 | ]
237 | },
238 | "execution_count": 5,
239 | "metadata": {},
240 | "output_type": "execute_result"
241 | }
242 | ],
243 | "source": [
244 | "lname"
245 | ]
246 | },
247 | {
248 | "cell_type": "markdown",
249 | "metadata": {},
250 | "source": [
251 | "
\n",
252 | "Make sure the number of values you are unpacking matches the number of variables being assigned."
253 | ]
254 | },
255 | {
256 | "cell_type": "code",
257 | "execution_count": 6,
258 | "metadata": {
259 | "collapsed": true
260 | },
261 | "outputs": [
262 | {
263 | "ename": "ValueError",
264 | "evalue": "too many values to unpack (expected 3)",
265 | "output_type": "error",
266 | "traceback": [
267 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
268 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
269 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'Mars'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Huang'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'marshuang80@gmail.com'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'San Diego'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0memail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
270 | "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 3)"
271 | ]
272 | }
273 | ],
274 | "source": [
275 | "x = ('Mars', 'Huang', 'marshuang80@gmail.com', 'San Diego')\n",
276 | "fname, lname, email = x"
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {},
282 | "source": [
283 | "You can also unpack from a list"
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "execution_count": 7,
289 | "metadata": {
290 | "collapsed": true
291 | },
292 | "outputs": [],
293 | "source": [
294 | "x = ['Mars','Huang','marshuang80@gmail.com']\n",
295 | "fname, lname, email = x"
296 | ]
297 | },
298 | {
299 | "cell_type": "markdown",
300 | "metadata": {},
301 | "source": [
302 | "# The Python Programming Language: Functions"
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": 1,
308 | "metadata": {
309 | "collapsed": true
310 | },
311 | "outputs": [
312 | {
313 | "data": {
314 | "text/plain": [
315 | "3"
316 | ]
317 | },
318 | "execution_count": 1,
319 | "metadata": {},
320 | "output_type": "execute_result"
321 | }
322 | ],
323 | "source": [
324 | "x = 1\n",
325 | "y = 2\n",
326 | "x + y"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": 2,
332 | "metadata": {
333 | "collapsed": true
334 | },
335 | "outputs": [
336 | {
337 | "data": {
338 | "text/plain": [
339 | "1"
340 | ]
341 | },
342 | "execution_count": 2,
343 | "metadata": {},
344 | "output_type": "execute_result"
345 | }
346 | ],
347 | "source": [
348 | "x"
349 | ]
350 | },
351 | {
352 | "cell_type": "markdown",
353 | "metadata": {},
354 | "source": [
355 | "
\n",
356 | "`add_numbers` is a function that takes two numbers and adds them together."
357 | ]
358 | },
359 | {
360 | "cell_type": "code",
361 | "execution_count": 3,
362 | "metadata": {
363 | "collapsed": true
364 | },
365 | "outputs": [
366 | {
367 | "data": {
368 | "text/plain": [
369 | "3"
370 | ]
371 | },
372 | "execution_count": 3,
373 | "metadata": {},
374 | "output_type": "execute_result"
375 | }
376 | ],
377 | "source": [
378 | "def add_numbers(x, y):\n",
379 | " return x + y\n",
380 | "\n",
381 | "add_numbers(1, 2)"
382 | ]
383 | },
384 | {
385 | "cell_type": "markdown",
386 | "metadata": {},
387 | "source": [
388 | "
\n",
389 | "`add_numbers` updated to take an optional 3rd parameter. Using `print` allows printing of multiple expressions within a single cell."
390 | ]
391 | },
392 | {
393 | "cell_type": "code",
394 | "execution_count": null,
395 | "metadata": {
396 | "collapsed": false
397 | },
398 | "outputs": [],
399 | "source": [
400 | "def add_numbers(x,y,z=None):\n",
401 | " if (z==None):\n",
402 | " return x+y\n",
403 | " else:\n",
404 | " return x+y+z\n",
405 | "\n",
406 | "print(add_numbers(1, 2))\n",
407 | "print(add_numbers(1, 2, 3))"
408 | ]
409 | },
410 | {
411 | "cell_type": "markdown",
412 | "metadata": {},
413 | "source": [
414 | "
\n",
415 | "`add_numbers` updated to take an optional flag parameter."
416 | ]
417 | },
418 | {
419 | "cell_type": "code",
420 | "execution_count": 12,
421 | "metadata": {
422 | "collapsed": false
423 | },
424 | "outputs": [
425 | {
426 | "name": "stdout",
427 | "output_type": "stream",
428 | "text": [
429 | "Flag is true!\n",
430 | "3\n"
431 | ]
432 | }
433 | ],
434 | "source": [
435 | "def add_numbers(x, y, z=None, flag=False):\n",
436 | " if (flag):\n",
437 | " print('Flag is true!')\n",
438 | " if (z==None):\n",
439 | " return x + y\n",
440 | " else:\n",
441 | " return x + y + z\n",
442 | " \n",
443 | "print(add_numbers(1, 2, flag=True))"
444 | ]
445 | },
446 | {
447 | "cell_type": "markdown",
448 | "metadata": {},
449 | "source": [
450 | "
\n",
451 | "Assign function `add_numbers` to variable `a`."
452 | ]
453 | },
454 | {
455 | "cell_type": "code",
456 | "execution_count": 4,
457 | "metadata": {
458 | "collapsed": true
459 | },
460 | "outputs": [
461 | {
462 | "data": {
463 | "text/plain": [
464 | "3"
465 | ]
466 | },
467 | "execution_count": 4,
468 | "metadata": {},
469 | "output_type": "execute_result"
470 | }
471 | ],
472 | "source": [
473 | "def add_numbers(x,y):\n",
474 | " return x+y\n",
475 | "\n",
476 | "a = add_numbers\n",
477 | "a(1,2)"
478 | ]
479 | },
480 | {
481 | "cell_type": "markdown",
482 | "metadata": {},
483 | "source": [
484 | "You can also use unpacking for functions"
485 | ]
486 | },
487 | {
488 | "cell_type": "code",
489 | "execution_count": 3,
490 | "metadata": {
491 | "collapsed": true
492 | },
493 | "outputs": [
494 | {
495 | "name": "stdout",
496 | "output_type": "stream",
497 | "text": [
498 | "0.6666666666666666\n",
499 | "6\n"
500 | ]
501 | }
502 | ],
503 | "source": [
504 | "def div_and_mul(x,y):\n",
505 | " return x/y, x*y\n",
506 | "a,b = div_and_mul(2,3)\n",
507 | "print(a)\n",
508 | "print(b)"
509 | ]
510 | },
511 | {
512 | "cell_type": "code",
513 | "execution_count": 22,
514 | "metadata": {
515 | "collapsed": true
516 | },
517 | "outputs": [],
518 | "source": [
519 | "##Quiz\n",
520 | "#make a function that has two parameters: a list of numbers \"num\" a second number \"val\"\n",
521 | "#return the sum of all odd numbers in num, minus the sum of all even numbers, \n",
522 | "#and multiply by val if a second parameter is given\n",
523 | "num = [14,7,2,4,5,6,9,13,17,4,16,9,0,3]\n",
524 | "\n",
525 | "#write function below\n",
526 | "\n",
527 | "#two test cases:\n",
528 | "print(get_result(num))\n",
529 | "print(get_result(num,7)"
530 | ]
531 | }
532 | ],
533 | "metadata": {
534 | "anaconda-cloud": {},
535 | "kernelspec": {
536 | "display_name": "Python [default]",
537 | "language": "python",
538 | "name": "python3"
539 | },
540 | "language_info": {
541 | "codemirror_mode": {
542 | "name": "ipython",
543 | "version": 3
544 | },
545 | "file_extension": ".py",
546 | "mimetype": "text/x-python",
547 | "name": "python",
548 | "nbconvert_exporter": "python",
549 | "pygments_lexer": "ipython3",
550 | "version": "3.5.2"
551 | }
552 | },
553 | "nbformat": 4,
554 | "nbformat_minor": 0
555 | }
556 |
--------------------------------------------------------------------------------
/Tutorial_4_FileIO/File_input.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 3"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 1,
14 | "metadata": {
15 | "collapsed": true
16 | },
17 | "outputs": [],
18 | "source": [
19 | "#Review Exercise: Print out the Keys and Values of the following dictionary\n",
20 | "# in this format: \"My _key_ is very _value_\" \n",
21 | "to_print = {\n",
22 | " \"appple\": \"red\", \n",
23 | " \"banana\": \"yellow\",\n",
24 | " \"orange\": \"orange\",\n",
25 | " \"lemon\" : \"purple\",\n",
26 | " \"blueberry\": \"modest\",\n",
27 | "}"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 18,
33 | "metadata": {
34 | "collapsed": true
35 | },
36 | "outputs": [],
37 | "source": [
38 | "#Review Exercise: make a function that returns the sum and difference\n",
39 | "#of the two input numbers (hint: use zip)"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 2,
45 | "metadata": {
46 | "collapsed": true
47 | },
48 | "outputs": [
49 | {
50 | "data": {
51 | "text/plain": [
52 | "['This is my humble attempt, \\n',\n",
53 | " 'to write a poem again.\\n',\n",
54 | " 'I always feel contempt, \\n',\n",
55 | " 'that no one is popping champagne.\\n']"
56 | ]
57 | },
58 | "execution_count": 2,
59 | "metadata": {},
60 | "output_type": "execute_result"
61 | }
62 | ],
63 | "source": [
64 | "#Review Exercise: open file \"review.txt\" and store each line in a list\n",
65 | "#remember to remove the newline character \n",
66 | "poem = []\n"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "metadata": {},
72 | "source": [
73 | "# Reading and Writing files"
74 | ]
75 | },
76 | {
77 | "cell_type": "markdown",
78 | "metadata": {},
79 | "source": [
80 | "Learning the \"with open... as\" syntax'\n",
81 | "\n",
82 | "r stands for \"read\""
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 16,
88 | "metadata": {
89 | "collapsed": false
90 | },
91 | "outputs": [
92 | {
93 | "name": "stdout",
94 | "output_type": "stream",
95 | "text": [
96 | "<_io.TextIOWrapper name='practice.txt' mode='r' encoding='US-ASCII'>\n"
97 | ]
98 | }
99 | ],
100 | "source": [
101 | "with open(\"practice.txt\",\"r\") as file:\n",
102 | " print(file)"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 17,
108 | "metadata": {
109 | "collapsed": false
110 | },
111 | "outputs": [
112 | {
113 | "name": "stdout",
114 | "output_type": "stream",
115 | "text": [
116 | "hi guys, name is mars \n",
117 | "welcome to my workshop\n",
118 | "i enjoy eating mars bars\n",
119 | "and i steal them from the candy shop\n",
120 | "\n"
121 | ]
122 | }
123 | ],
124 | "source": [
125 | "with open(\"practice.txt\",\"r\") as file:\n",
126 | " print(file.read())"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": 20,
132 | "metadata": {
133 | "collapsed": false
134 | },
135 | "outputs": [
136 | {
137 | "name": "stdout",
138 | "output_type": "stream",
139 | "text": [
140 | "['hi guys, name is mars \\n', 'welcome to my workshop\\n', 'i enjoy eating mars bars\\n', 'and i steal them from the candy shop\\n']\n"
141 | ]
142 | }
143 | ],
144 | "source": [
145 | "text = []\n",
146 | "with open(\"practice.txt\",\"r\") as file:\n",
147 | " for line in file:\n",
148 | " text.append(line)\n",
149 | "print(text)"
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": null,
155 | "metadata": {
156 | "collapsed": true
157 | },
158 | "outputs": [],
159 | "source": [
160 | "##Pop-quiz: remove the \\n symbol from the list\n"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "
\n",
168 | "# CSV files\n",
169 | "CSV files stand for comma seperated values, which how programs like excel format their files. \n",
170 | "\n",
171 | "
"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": 11,
177 | "metadata": {
178 | "collapsed": true
179 | },
180 | "outputs": [
181 | {
182 | "data": {
183 | "text/plain": [
184 | "['2012',\n",
185 | " 'MITSUBISHI',\n",
186 | " 'i-MiEV',\n",
187 | " 'SUBCOMPACT',\n",
188 | " '49',\n",
189 | " 'A1',\n",
190 | " 'B',\n",
191 | " '16.9',\n",
192 | " '21.4',\n",
193 | " '18.7',\n",
194 | " '1.9',\n",
195 | " '2.4',\n",
196 | " '2.1',\n",
197 | " '0',\n",
198 | " 'n/a',\n",
199 | " '100',\n",
200 | " '7\\n']"
201 | ]
202 | },
203 | "execution_count": 11,
204 | "metadata": {},
205 | "output_type": "execute_result"
206 | }
207 | ],
208 | "source": [
209 | "#Exercise open this file and store the information in a list\n",
210 | "# remember to use split on commas\n"
211 | ]
212 | },
213 | {
214 | "cell_type": "markdown",
215 | "metadata": {},
216 | "source": [
217 | "Using the \"csv\" package"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": 4,
223 | "metadata": {
224 | "collapsed": true
225 | },
226 | "outputs": [],
227 | "source": [
228 | "import csv\n",
229 | "import csv as c #not recommeded (package name is already short)"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 7,
235 | "metadata": {
236 | "collapsed": true
237 | },
238 | "outputs": [
239 | {
240 | "data": {
241 | "text/plain": [
242 | "['2012',\n",
243 | " 'MITSUBISHI',\n",
244 | " 'i-MiEV',\n",
245 | " 'SUBCOMPACT',\n",
246 | " '49',\n",
247 | " 'A1',\n",
248 | " 'B',\n",
249 | " '16.9',\n",
250 | " '21.4',\n",
251 | " '18.7',\n",
252 | " '1.9',\n",
253 | " '2.4',\n",
254 | " '2.1',\n",
255 | " '0',\n",
256 | " 'n/a',\n",
257 | " '100',\n",
258 | " '7']"
259 | ]
260 | },
261 | "execution_count": 7,
262 | "metadata": {},
263 | "output_type": "execute_result"
264 | }
265 | ],
266 | "source": [
267 | "with open('cars.csv') as csvfile:\n",
268 | " cars = list(csv.reader(csvfile,delimiter =','))\n",
269 | "#default delimiter is ','\n",
270 | "with open('cars.csv') as csvfile:\n",
271 | " cars = list(csv.reader(csvfile))\n",
272 | "cars[0]"
273 | ]
274 | },
275 | {
276 | "cell_type": "code",
277 | "execution_count": null,
278 | "metadata": {
279 | "collapsed": true
280 | },
281 | "outputs": [],
282 | "source": [
283 | "#print out he make and model of the first 5 cars"
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "execution_count": 8,
289 | "metadata": {
290 | "collapsed": true
291 | },
292 | "outputs": [
293 | {
294 | "data": {
295 | "text/plain": [
296 | "[{'(g/km)': '0',\n",
297 | " '(kW)': '49',\n",
298 | " '(km)': '100',\n",
299 | " 'CITY (Le/100 km)': '1.9',\n",
300 | " 'CITY (kWh/100 km)': '16.9',\n",
301 | " 'COMB (Le/100 km)': '2.1',\n",
302 | " 'COMB (kWh/100 km)': '18.7',\n",
303 | " 'HWY (Le/100 km)': '2.4',\n",
304 | " 'HWY (kWh/100 km)': '21.4',\n",
305 | " 'Make': 'MITSUBISHI',\n",
306 | " 'Model': 'i-MiEV',\n",
307 | " 'RATING': 'n/a',\n",
308 | " 'Size': 'SUBCOMPACT',\n",
309 | " 'TIME (h)': '7',\n",
310 | " 'TYPE': 'B',\n",
311 | " 'Unnamed: 5': 'A1',\n",
312 | " 'YEAR': '2012'},\n",
313 | " {'(g/km)': '0',\n",
314 | " '(kW)': '80',\n",
315 | " '(km)': '117',\n",
316 | " 'CITY (Le/100 km)': '2.2',\n",
317 | " 'CITY (kWh/100 km)': '19.3',\n",
318 | " 'COMB (Le/100 km)': '2.4',\n",
319 | " 'COMB (kWh/100 km)': '21.1',\n",
320 | " 'HWY (Le/100 km)': '2.6',\n",
321 | " 'HWY (kWh/100 km)': '23.0',\n",
322 | " 'Make': 'NISSAN',\n",
323 | " 'Model': 'LEAF',\n",
324 | " 'RATING': 'n/a',\n",
325 | " 'Size': 'MID-SIZE',\n",
326 | " 'TIME (h)': '7',\n",
327 | " 'TYPE': 'B',\n",
328 | " 'Unnamed: 5': 'A1',\n",
329 | " 'YEAR': '2012'},\n",
330 | " {'(g/km)': '0',\n",
331 | " '(kW)': '107',\n",
332 | " '(km)': '122',\n",
333 | " 'CITY (Le/100 km)': '2.1',\n",
334 | " 'CITY (kWh/100 km)': '19.0',\n",
335 | " 'COMB (Le/100 km)': '2.2',\n",
336 | " 'COMB (kWh/100 km)': '20.0',\n",
337 | " 'HWY (Le/100 km)': '2.4',\n",
338 | " 'HWY (kWh/100 km)': '21.1',\n",
339 | " 'Make': 'FORD',\n",
340 | " 'Model': 'FOCUS ELECTRIC',\n",
341 | " 'RATING': 'n/a',\n",
342 | " 'Size': 'COMPACT',\n",
343 | " 'TIME (h)': '4',\n",
344 | " 'TYPE': 'B',\n",
345 | " 'Unnamed: 5': 'A1',\n",
346 | " 'YEAR': '2013'}]"
347 | ]
348 | },
349 | "execution_count": 8,
350 | "metadata": {},
351 | "output_type": "execute_result"
352 | }
353 | ],
354 | "source": [
355 | "with open('cars.csv') as csvfile:\n",
356 | " cars = list(csv.DictReader(csvfile))\n",
357 | " \n",
358 | "cars[:3] # The first three dictionaries in our list."
359 | ]
360 | },
361 | {
362 | "cell_type": "markdown",
363 | "metadata": {},
364 | "source": [
365 | "
\n",
366 | "`csv.Dictreader` has read in each row of our csv file as a dictionary. `len` shows that our list is comprised of 53 dictionaries."
367 | ]
368 | },
369 | {
370 | "cell_type": "code",
371 | "execution_count": 9,
372 | "metadata": {
373 | "collapsed": true
374 | },
375 | "outputs": [
376 | {
377 | "data": {
378 | "text/plain": [
379 | "53"
380 | ]
381 | },
382 | "execution_count": 9,
383 | "metadata": {},
384 | "output_type": "execute_result"
385 | }
386 | ],
387 | "source": [
388 | "len(cars)"
389 | ]
390 | },
391 | {
392 | "cell_type": "markdown",
393 | "metadata": {},
394 | "source": [
395 | "
\n",
396 | "since mpg is a list of dictionaries, `keys` gives us the column names of our csv."
397 | ]
398 | },
399 | {
400 | "cell_type": "code",
401 | "execution_count": 10,
402 | "metadata": {
403 | "collapsed": true
404 | },
405 | "outputs": [
406 | {
407 | "data": {
408 | "text/plain": [
409 | "dict_keys(['COMB (kWh/100 km)', 'Size', 'CITY (kWh/100 km)', 'COMB (Le/100 km)', 'Model', '(kW)', 'Make', 'TYPE', 'CITY (Le/100 km)', 'RATING', '(g/km)', '(km)', 'TIME (h)', 'HWY (Le/100 km)', 'Unnamed: 5', 'YEAR', 'HWY (kWh/100 km)'])"
410 | ]
411 | },
412 | "execution_count": 10,
413 | "metadata": {},
414 | "output_type": "execute_result"
415 | }
416 | ],
417 | "source": [
418 | "cars[0].keys()"
419 | ]
420 | },
421 | {
422 | "cell_type": "code",
423 | "execution_count": 27,
424 | "metadata": {
425 | "collapsed": true
426 | },
427 | "outputs": [
428 | {
429 | "data": {
430 | "text/plain": [
431 | "2.21"
432 | ]
433 | },
434 | "execution_count": 27,
435 | "metadata": {},
436 | "output_type": "execute_result"
437 | }
438 | ],
439 | "source": [
440 | "##Exercise: calculate the average electricity consumption while driving in the city"
441 | ]
442 | },
443 | {
444 | "cell_type": "code",
445 | "execution_count": 29,
446 | "metadata": {
447 | "collapsed": true
448 | },
449 | "outputs": [
450 | {
451 | "data": {
452 | "text/plain": [
453 | "8"
454 | ]
455 | },
456 | "execution_count": 29,
457 | "metadata": {},
458 | "output_type": "execute_result"
459 | }
460 | ],
461 | "source": [
462 | "##Exercise2: count how many different brands of cars there are, and print out all the different brands once"
463 | ]
464 | }
465 | ],
466 | "metadata": {
467 | "anaconda-cloud": {},
468 | "kernelspec": {
469 | "display_name": "Python [default]",
470 | "language": "python",
471 | "name": "python3"
472 | },
473 | "language_info": {
474 | "codemirror_mode": {
475 | "name": "ipython",
476 | "version": 3
477 | },
478 | "file_extension": ".py",
479 | "mimetype": "text/x-python",
480 | "name": "python",
481 | "nbconvert_exporter": "python",
482 | "pygments_lexer": "ipython3",
483 | "version": "3.5.2"
484 | }
485 | },
486 | "nbformat": 4,
487 | "nbformat_minor": 0
488 | }
489 |
--------------------------------------------------------------------------------
/Tutorial_4_FileIO/cars.csv:
--------------------------------------------------------------------------------
1 | YEAR,Make,Model,Size,(kW),Unnamed: 5,TYPE,CITY (kWh/100 km),HWY (kWh/100 km),COMB (kWh/100 km),CITY (Le/100 km),HWY (Le/100 km),COMB (Le/100 km),(g/km),RATING,(km),TIME (h)
2 | 2012,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
3 | 2012,NISSAN,LEAF,MID-SIZE,80,A1,B,19.3,23.0,21.1,2.2,2.6,2.4,0,n/a,117,7
4 | 2013,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
5 | 2013,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
6 | 2013,NISSAN,LEAF,MID-SIZE,80,A1,B,19.3,23.0,21.1,2.2,2.6,2.4,0,n/a,117,7
7 | 2013,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
8 | 2013,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
9 | 2013,TESLA,MODEL S (40 kWh battery),FULL-SIZE,270,A1,B,22.4,21.9,22.2,2.5,2.5,2.5,0,n/a,224,6
10 | 2013,TESLA,MODEL S (60 kWh battery),FULL-SIZE,270,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
11 | 2013,TESLA,MODEL S (85 kWh battery),FULL-SIZE,270,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
12 | 2013,TESLA,MODEL S PERFORMANCE,FULL-SIZE,310,A1,B,23.9,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
13 | 2014,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,n/a,131,7
14 | 2014,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
15 | 2014,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
16 | 2014,NISSAN,LEAF,MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,n/a,135,5
17 | 2014,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
18 | 2014,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
19 | 2014,TESLA,MODEL S (60 kWh battery),FULL-SIZE,225,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
20 | 2014,TESLA,MODEL S (85 kWh battery),FULL-SIZE,270,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
21 | 2014,TESLA,MODEL S PERFORMANCE,FULL-SIZE,310,A1,B,23.9,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
22 | 2015,BMW,i3,SUBCOMPACT,125,A1,B,15.2,18.8,16.8,1.7,2.1,1.9,0,n/a,130,4
23 | 2015,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,n/a,131,7
24 | 2015,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
25 | 2015,KIA,SOUL EV,STATION WAGON - SMALL,81,A1,B,17.5,22.7,19.9,2.0,2.6,2.2,0,n/a,149,4
26 | 2015,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
27 | 2015,NISSAN,LEAF,MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,n/a,135,5
28 | 2015,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
29 | 2015,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
30 | 2015,TESLA,MODEL S (60 kWh battery),FULL-SIZE,283,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
31 | 2015,TESLA,MODEL S (70 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,377,12
32 | 2015,TESLA,MODEL S (85/90 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
33 | 2015,TESLA,MODEL S 70D,FULL-SIZE,280,A1,B,20.8,20.6,20.7,2.3,2.3,2.3,0,n/a,386,12
34 | 2015,TESLA,MODEL S 85D/90D,FULL-SIZE,280,A1,B,22.0,19.8,21.0,2.5,2.2,2.4,0,n/a,435,12
35 | 2015,TESLA,MODEL S P85D/P90D,FULL-SIZE,515,A1,B,23.4,21.5,22.5,2.6,2.4,2.5,0,n/a,407,12
36 | 2016,BMW,i3,SUBCOMPACT,125,A1,B,15.2,18.8,16.8,1.7,2.1,1.9,0,10,130,4
37 | 2016,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,10,131,7
38 | 2016,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,10,122,4
39 | 2016,KIA,SOUL EV,STATION WAGON - SMALL,81,A1,B,17.5,22.7,19.9,2.0,2.6,2.2,0,10,149,4
40 | 2016,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,10,100,7
41 | 2016,NISSAN,LEAF (24 kWh battery),MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,10,135,5
42 | 2016,NISSAN,LEAF (30 kWh battery),MID-SIZE,80,A1,B,17.0,20.7,18.6,1.9,2.3,2.1,0,10,172,6
43 | 2016,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,10,109,8
44 | 2016,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,10,109,8
45 | 2016,TESLA,MODEL S (60 kWh battery),FULL-SIZE,283,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,10,335,10
46 | 2016,TESLA,MODEL S (70 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,10,377,12
47 | 2016,TESLA,MODEL S (85/90 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,10,426,12
48 | 2016,TESLA,MODEL S 70D,FULL-SIZE,386,A1,B,20.8,20.6,20.7,2.3,2.3,2.3,0,10,386,12
49 | 2016,TESLA,MODEL S 85D/90D,FULL-SIZE,386,A1,B,22.0,19.8,21.0,2.5,2.2,2.4,0,10,435,12
50 | 2016,TESLA,MODEL S 90D (Refresh),FULL-SIZE,386,A1,B,20.8,19.7,20.3,2.3,2.2,2.3,0,10,473,12
51 | 2016,TESLA,MODEL S P85D/P90D,FULL-SIZE,568,A1,B,23.4,21.5,22.5,2.6,2.4,2.5,0,10,407,12
52 | 2016,TESLA,MODEL S P90D (Refresh),FULL-SIZE,568,A1,B,22.9,21.0,22.1,2.6,2.4,2.5,0,10,435,12
53 | 2016,TESLA,MODEL X 90D,SUV - STANDARD,386,A1,B,23.2,22.2,22.7,2.6,2.5,2.6,0,10,414,12
54 | 2016,TESLA,MODEL X P90D,SUV - STANDARD,568,A1,B,23.6,23.3,23.5,2.7,2.6,2.6,0,10,402,12
55 |
--------------------------------------------------------------------------------
/Tutorial_4_FileIO/example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/marshuang80/Python_Tutorials/3c72513f716ab53b8089830830d1c162df952929/Tutorial_4_FileIO/example.png
--------------------------------------------------------------------------------
/Tutorial_4_FileIO/review.txt:
--------------------------------------------------------------------------------
1 | This is my humble attempt,
2 | to write a poem again.
3 | I always feel contempt,
4 | that no one is popping champagne.
5 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/marshuang80/Python_Tutorials/3c72513f716ab53b8089830830d1c162df952929/Tutorial_5_Pythonista/.DS_Store
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/.ipynb_checkpoints/Tutorial 5 Pythonista-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 5"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 27,
14 | "metadata": {
15 | "collapsed": true
16 | },
17 | "outputs": [
18 | {
19 | "data": {
20 | "text/plain": [
21 | "2.21"
22 | ]
23 | },
24 | "execution_count": 27,
25 | "metadata": {},
26 | "output_type": "execute_result"
27 | }
28 | ],
29 | "source": [
30 | "##Review Exercise: calculate the average electricity consumption while driving in the city"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 29,
36 | "metadata": {
37 | "collapsed": true
38 | },
39 | "outputs": [
40 | {
41 | "data": {
42 | "text/plain": [
43 | "8"
44 | ]
45 | },
46 | "execution_count": 29,
47 | "metadata": {},
48 | "output_type": "execute_result"
49 | }
50 | ],
51 | "source": [
52 | "##Review Exercise2: count how many different brands of cars there are, and print out all the different brands once"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "# The power of python's one liner\n",
60 | "Quicksort with only one line (by Rohit Malshe) "
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 12,
66 | "metadata": {
67 | "collapsed": true
68 | },
69 | "outputs": [
70 | {
71 | "data": {
72 | "text/plain": [
73 | "[1, 2, 2, 5, 6, 7, 8, 8]"
74 | ]
75 | },
76 | "execution_count": 12,
77 | "metadata": {},
78 | "output_type": "execute_result"
79 | }
80 | ],
81 | "source": [
82 | "qsort = lambda l : l if len(l)<=1 else qsort([x for x in l[1:] if x < l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])\n",
83 | "qsort([1,2,5,7,8,2,6,8])"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "# List Comprehension"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {
96 | "collapsed": true
97 | },
98 | "source": [
99 | "imagine if you can fit your for loops in one line"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 1,
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [
109 | {
110 | "data": {
111 | "text/plain": [
112 | "[2, 4, 6, 8, 10, 12, 14]"
113 | ]
114 | },
115 | "execution_count": 1,
116 | "metadata": {},
117 | "output_type": "execute_result"
118 | }
119 | ],
120 | "source": [
121 | "a = [1,2,3,4,5,6,7]\n",
122 | "b = []\n",
123 | "for num in a: \n",
124 | " b.append(num*2)\n",
125 | "b"
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "metadata": {},
131 | "source": [
132 | "using the \"**for... in**\" syntax"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 2,
138 | "metadata": {
139 | "collapsed": false
140 | },
141 | "outputs": [
142 | {
143 | "data": {
144 | "text/plain": [
145 | "[2, 4, 6, 8, 10, 12, 14]"
146 | ]
147 | },
148 | "execution_count": 2,
149 | "metadata": {},
150 | "output_type": "execute_result"
151 | }
152 | ],
153 | "source": [
154 | "a = [1,2,3,4,5,6,7]\n",
155 | "b = [num*2 for num in a]\n",
156 | "b"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 2,
162 | "metadata": {
163 | "collapsed": true
164 | },
165 | "outputs": [
166 | {
167 | "data": {
168 | "text/plain": [
169 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
170 | ]
171 | },
172 | "execution_count": 2,
173 | "metadata": {},
174 | "output_type": "execute_result"
175 | }
176 | ],
177 | "source": [
178 | "#Exercise: using list comprehension and \"range\" syntax to store number 0 to 9 to list \"numbers\" "
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {},
184 | "source": [
185 | "We can also include if statements in list comprhension\n",
186 | "\n",
187 | "\"** if ... else ... for ... in ... **\"\n",
188 | "\n",
189 | "or\n",
190 | "\n",
191 | "\"** for ... in ... if ...**\""
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": 3,
197 | "metadata": {
198 | "collapsed": false
199 | },
200 | "outputs": [
201 | {
202 | "data": {
203 | "text/plain": [
204 | "[0, 2, 4, 6, 8, 10]"
205 | ]
206 | },
207 | "execution_count": 3,
208 | "metadata": {},
209 | "output_type": "execute_result"
210 | }
211 | ],
212 | "source": [
213 | "a = [0,1,2,3,4,5,6,7,8,9,10]\n",
214 | "b = [num for num in a if num%2 == 0]\n",
215 | "b"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": 6,
221 | "metadata": {
222 | "collapsed": true
223 | },
224 | "outputs": [
225 | {
226 | "data": {
227 | "text/plain": [
228 | "[0, 10, -2, 30, -4, 50, -6, 70, -8, 90, -10]"
229 | ]
230 | },
231 | "execution_count": 6,
232 | "metadata": {},
233 | "output_type": "execute_result"
234 | }
235 | ],
236 | "source": [
237 | "c = [-num if num%2 ==0 else num*10 for num in a]\n",
238 | "c"
239 | ]
240 | },
241 | {
242 | "cell_type": "markdown",
243 | "metadata": {},
244 | "source": [
245 | "Just like a for loop, a **for.. range(..)** syntax can also be used in list comprehension"
246 | ]
247 | },
248 | {
249 | "cell_type": "code",
250 | "execution_count": 1,
251 | "metadata": {
252 | "collapsed": false
253 | },
254 | "outputs": [
255 | {
256 | "data": {
257 | "text/plain": [
258 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
259 | ]
260 | },
261 | "execution_count": 1,
262 | "metadata": {},
263 | "output_type": "execute_result"
264 | }
265 | ],
266 | "source": [
267 | "d = [a for a in range(10)]\n",
268 | "d"
269 | ]
270 | },
271 | {
272 | "cell_type": "markdown",
273 | "metadata": {},
274 | "source": [
275 | "You can also create a list of different data structures in list comprehension\n",
276 | "Here, we creates a list of lists:"
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": 2,
282 | "metadata": {
283 | "collapsed": true
284 | },
285 | "outputs": [
286 | {
287 | "data": {
288 | "text/plain": [
289 | "[[0, 0, 0],\n",
290 | " [1, 10, 100],\n",
291 | " [2, 20, 200],\n",
292 | " [3, 30, 300],\n",
293 | " [4, 40, 400],\n",
294 | " [5, 50, 500],\n",
295 | " [6, 60, 600],\n",
296 | " [7, 70, 700],\n",
297 | " [8, 80, 800],\n",
298 | " [9, 90, 900],\n",
299 | " [10, 100, 1000],\n",
300 | " [11, 110, 1100],\n",
301 | " [12, 120, 1200],\n",
302 | " [13, 130, 1300],\n",
303 | " [14, 140, 1400],\n",
304 | " [15, 150, 1500],\n",
305 | " [16, 160, 1600],\n",
306 | " [17, 170, 1700],\n",
307 | " [18, 180, 1800],\n",
308 | " [19, 190, 1900]]"
309 | ]
310 | },
311 | "execution_count": 2,
312 | "metadata": {},
313 | "output_type": "execute_result"
314 | }
315 | ],
316 | "source": [
317 | "e = [[a*1, a*10, a*100] for a in range(20)]\n",
318 | "e"
319 | ]
320 | },
321 | {
322 | "cell_type": "markdown",
323 | "metadata": {},
324 | "source": [
325 | "the **zip** syntax can be useful when you want to do list comprehension on two lists"
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "execution_count": 3,
331 | "metadata": {
332 | "collapsed": true
333 | },
334 | "outputs": [
335 | {
336 | "data": {
337 | "text/plain": [
338 | "[('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]"
339 | ]
340 | },
341 | "execution_count": 3,
342 | "metadata": {},
343 | "output_type": "execute_result"
344 | }
345 | ],
346 | "source": [
347 | "t1 = [\"a\",\"b\",\"c\",\"d\"]\n",
348 | "t2 = [\"1\",\"2\",\"3\",\"4\"]\n",
349 | "f = [(a,b) for a,b in zip(t1,t2)]\n",
350 | "f"
351 | ]
352 | },
353 | {
354 | "cell_type": "markdown",
355 | "metadata": {},
356 | "source": [
357 | "a dictionary can also be made with using list comprehension's syntax"
358 | ]
359 | },
360 | {
361 | "cell_type": "code",
362 | "execution_count": 4,
363 | "metadata": {
364 | "collapsed": true
365 | },
366 | "outputs": [
367 | {
368 | "name": "stdout",
369 | "output_type": "stream",
370 | "text": [
371 | "d 4\n",
372 | "a 1\n",
373 | "c 3\n",
374 | "b 2\n"
375 | ]
376 | }
377 | ],
378 | "source": [
379 | "g = {a:b for a,b in zip(t1,t2)}\n",
380 | "for key,val in g.items():\n",
381 | " print(key,val)"
382 | ]
383 | },
384 | {
385 | "cell_type": "markdown",
386 | "metadata": {},
387 | "source": [
388 | "you can even open a file and store it's contents in a list with the syntax: \n",
389 | "\n",
390 | "[ **for** ... **in** **open(...)**]"
391 | ]
392 | },
393 | {
394 | "cell_type": "code",
395 | "execution_count": 5,
396 | "metadata": {
397 | "collapsed": false
398 | },
399 | "outputs": [
400 | {
401 | "data": {
402 | "text/plain": [
403 | "['line1\\n', 'line2\\n', 'line3\\n', 'line4\\n', 'line5\\n']"
404 | ]
405 | },
406 | "execution_count": 5,
407 | "metadata": {},
408 | "output_type": "execute_result"
409 | }
410 | ],
411 | "source": [
412 | "h = [a for a in open(\"practice3.txt\")]\n",
413 | "h"
414 | ]
415 | },
416 | {
417 | "cell_type": "code",
418 | "execution_count": null,
419 | "metadata": {
420 | "collapsed": true
421 | },
422 | "outputs": [],
423 | "source": [
424 | "#Exercise: remove the '\\n' symbol \n",
425 | "e1 = [a for a in open(\"practice3.txt\")] # <- make changes to this line\n",
426 | "e1"
427 | ]
428 | },
429 | {
430 | "cell_type": "code",
431 | "execution_count": 18,
432 | "metadata": {
433 | "collapsed": true
434 | },
435 | "outputs": [
436 | {
437 | "data": {
438 | "text/plain": [
439 | "['THE',\n",
440 | " 'MAIN',\n",
441 | " 'co',\n",
442 | " 'FOR',\n",
443 | " 'THE',\n",
444 | " 'ac',\n",
445 | " 'OF',\n",
446 | " 'LOVE',\n",
447 | " 'IS',\n",
448 | " 'THE',\n",
449 | " 'ov',\n",
450 | " 'OF',\n",
451 | " 'on',\n",
452 | " 'na',\n",
453 | " 'THE',\n",
454 | " 'na',\n",
455 | " 'or',\n",
456 | " 'IS',\n",
457 | " 'ONE',\n",
458 | " 'IN',\n",
459 | " 'wh',\n",
460 | " 'ONE',\n",
461 | " 'ex',\n",
462 | " 'AS',\n",
463 | " 'REAL',\n",
464 | " 'ONLY',\n",
465 | " 'THAT',\n",
466 | " 'wh',\n",
467 | " 'ex',\n",
468 | " 'wi',\n",
469 | " 'on',\n",
470 | " 'wh',\n",
471 | " 'THE',\n",
472 | " 'ph',\n",
473 | " 'IN',\n",
474 | " 'THE',\n",
475 | " 'ou',\n",
476 | " 'wo',\n",
477 | " 'HAVE',\n",
478 | " 'NO',\n",
479 | " 're',\n",
480 | " 'IN',\n",
481 | " 'th',\n",
482 | " 'BUT',\n",
483 | " 'ARE',\n",
484 | " 'ex',\n",
485 | " 'ONLY',\n",
486 | " 'FROM',\n",
487 | " 'THE',\n",
488 | " 'vi',\n",
489 | " 'OF',\n",
490 | " 'th',\n",
491 | " 'be',\n",
492 | " 'us',\n",
493 | " 'OR',\n",
494 | " 'da',\n",
495 | " 'TO',\n",
496 | " 'ONE.',\n",
497 | " 'THE',\n",
498 | " 'op',\n",
499 | " 'POLE',\n",
500 | " 'TO',\n",
501 | " 'na',\n",
502 | " 'IS',\n",
503 | " 'ob',\n",
504 | " 'IT',\n",
505 | " 'IS',\n",
506 | " 'THE',\n",
507 | " 'fa',\n",
508 | " 'TO',\n",
509 | " 'SEE',\n",
510 | " 'ot',\n",
511 | " 'pe',\n",
512 | " 'AND',\n",
513 | " 'th',\n",
514 | " 'AS',\n",
515 | " 'THEY',\n",
516 | " 'ARE,',\n",
517 | " 'ob',\n",
518 | " 'AND',\n",
519 | " 'TO',\n",
520 | " 'BE',\n",
521 | " 'ABLE',\n",
522 | " 'TO',\n",
523 | " 'se',\n",
524 | " 'THIS',\n",
525 | " 'ob',\n",
526 | " 'pi',\n",
527 | " 'FROM',\n",
528 | " 'A',\n",
529 | " 'pi',\n",
530 | " 'wh',\n",
531 | " 'IS',\n",
532 | " 'fo',\n",
533 | " 'BY',\n",
534 | " 'on',\n",
535 | " 'de',\n",
536 | " 'AND',\n",
537 | " 'fe']"
538 | ]
539 | },
540 | "execution_count": 18,
541 | "metadata": {},
542 | "output_type": "execute_result"
543 | }
544 | ],
545 | "source": [
546 | "#Exercise: read in file \"practice2.txt\". parse each word as an element in the list. \n",
547 | "#return only the first 2 characters for words longer than 5 characters\n",
548 | "#captialize words than are shorter than 5 characters\n",
549 | "#Use list comprehension!\n",
550 | "#eg: ['THE','MAIN','co','FOR','THE','ac','OF','LOVE','IS','THE','ov','OF'.........\n"
551 | ]
552 | },
553 | {
554 | "cell_type": "code",
555 | "execution_count": null,
556 | "metadata": {
557 | "collapsed": true
558 | },
559 | "outputs": [],
560 | "source": [
561 | "#Exercise: Open file \"names.txt\" and \"emails.txt\" and store each pair into a dictionary\n",
562 | "#the names and emails are in order for both files"
563 | ]
564 | },
565 | {
566 | "cell_type": "markdown",
567 | "metadata": {
568 | "collapsed": true
569 | },
570 | "source": [
571 | "# Applying List Comprehension\n",
572 | "back on our cars.csv example"
573 | ]
574 | },
575 | {
576 | "cell_type": "code",
577 | "execution_count": null,
578 | "metadata": {
579 | "collapsed": true
580 | },
581 | "outputs": [],
582 | "source": [
583 | "###Exercise: calculate the average electricity consumption while driving in the city\n",
584 | "#Using list comprehension"
585 | ]
586 | },
587 | {
588 | "cell_type": "code",
589 | "execution_count": null,
590 | "metadata": {
591 | "collapsed": true
592 | },
593 | "outputs": [],
594 | "source": [
595 | "##Exercise2: count how many different brands of cars there are, and print out all the different brands once\n",
596 | "#Using list comprehension"
597 | ]
598 | },
599 | {
600 | "cell_type": "markdown",
601 | "metadata": {},
602 | "source": [
603 | "# Lambda Functions: writing functions in 1 line"
604 | ]
605 | },
606 | {
607 | "cell_type": "markdown",
608 | "metadata": {},
609 | "source": [
610 | "lambda __variable__ : __returnValue__"
611 | ]
612 | },
613 | {
614 | "cell_type": "code",
615 | "execution_count": 11,
616 | "metadata": {
617 | "collapsed": true
618 | },
619 | "outputs": [
620 | {
621 | "data": {
622 | "text/plain": [
623 | "4"
624 | ]
625 | },
626 | "execution_count": 11,
627 | "metadata": {},
628 | "output_type": "execute_result"
629 | }
630 | ],
631 | "source": [
632 | "a = lambda x: x*2\n",
633 | "a(2)"
634 | ]
635 | },
636 | {
637 | "cell_type": "code",
638 | "execution_count": 12,
639 | "metadata": {
640 | "collapsed": true
641 | },
642 | "outputs": [
643 | {
644 | "data": {
645 | "text/plain": [
646 | "5"
647 | ]
648 | },
649 | "execution_count": 12,
650 | "metadata": {},
651 | "output_type": "execute_result"
652 | }
653 | ],
654 | "source": [
655 | "b = lambda x,y: x+y\n",
656 | "b(2,3)"
657 | ]
658 | },
659 | {
660 | "cell_type": "code",
661 | "execution_count": 1,
662 | "metadata": {
663 | "collapsed": true
664 | },
665 | "outputs": [],
666 | "source": [
667 | "#Exercise: make a lambda function named \"c\" that returns the product of two numbers\n"
668 | ]
669 | },
670 | {
671 | "cell_type": "markdown",
672 | "metadata": {},
673 | "source": [
674 | "Lambda functions can also take in conditional statements\n",
675 | "\n",
676 | "*lambda* **variable**: **returnValue1** if *conidtion* else **returnVlaue2**"
677 | ]
678 | },
679 | {
680 | "cell_type": "code",
681 | "execution_count": 5,
682 | "metadata": {
683 | "collapsed": true
684 | },
685 | "outputs": [
686 | {
687 | "name": "stdout",
688 | "output_type": "stream",
689 | "text": [
690 | "1\n",
691 | "-1\n"
692 | ]
693 | }
694 | ],
695 | "source": [
696 | "b = lambda x: -1 if x >= 0 else 1\n",
697 | "print(b(-20))\n",
698 | "print(b(99))"
699 | ]
700 | },
701 | {
702 | "cell_type": "markdown",
703 | "metadata": {},
704 | "source": [
705 | "# Using lambda functions as variables\n",
706 | "There are some standard python functions that takes in lambda functions as a variable. In this tutorial, we are going to talk about 4 of them: \n",
707 | "\n",
708 | "**Filter:**\n",
709 | "\n",
710 | "Filter calls the lambda function on it's second input (usually a list), and return only those that are *true* under the lambda condition"
711 | ]
712 | },
713 | {
714 | "cell_type": "code",
715 | "execution_count": 7,
716 | "metadata": {
717 | "collapsed": true
718 | },
719 | "outputs": [
720 | {
721 | "name": "stdout",
722 | "output_type": "stream",
723 | "text": [
724 | "\n"
725 | ]
726 | }
727 | ],
728 | "source": [
729 | "nums = [-5,2,200,-1,-19,-20]\n",
730 | "r = filter(lambda x: x >= 0, nums)\n",
731 | "print(r)"
732 | ]
733 | },
734 | {
735 | "cell_type": "markdown",
736 | "metadata": {},
737 | "source": [
738 | "As you can see, the return value of **filter** is a **filter object**, we have to manually convert the results to a list"
739 | ]
740 | },
741 | {
742 | "cell_type": "code",
743 | "execution_count": null,
744 | "metadata": {
745 | "collapsed": true
746 | },
747 | "outputs": [],
748 | "source": [
749 | "print(list(r))\n",
750 | "#or\n",
751 | "print(list(filter(lambda x: x >= 0, nums)))"
752 | ]
753 | },
754 | {
755 | "cell_type": "markdown",
756 | "metadata": {},
757 | "source": [
758 | "**Map:**\n",
759 | "\n",
760 | "The map function simply applies the lambda function to a list"
761 | ]
762 | },
763 | {
764 | "cell_type": "code",
765 | "execution_count": 8,
766 | "metadata": {
767 | "collapsed": true
768 | },
769 | "outputs": [
770 | {
771 | "name": "stdout",
772 | "output_type": "stream",
773 | "text": [
774 | "[1, 4, 9, 16, 25]\n"
775 | ]
776 | }
777 | ],
778 | "source": [
779 | "nums = [1,2,3,4,5]\n",
780 | "print(list(map(lambda x: x**2, nums)))"
781 | ]
782 | },
783 | {
784 | "cell_type": "markdown",
785 | "metadata": {},
786 | "source": [
787 | "**Reduce:**\n",
788 | "\n",
789 | "The reduce function takes in two variables, thus it's lambda function has to have two parameters as well. The function take in the first two elements from the list and return a value. Then, the value is reused again as the first parameter, with the next element of the list being the second. This process is repeated until the end of the input list.\n",
790 | "\n",
791 | "*reduce has to be imported from functools*\n",
792 | "\n",
793 | "*returns a value, not stored in reduce variable*"
794 | ]
795 | },
796 | {
797 | "cell_type": "code",
798 | "execution_count": 11,
799 | "metadata": {
800 | "collapsed": true
801 | },
802 | "outputs": [
803 | {
804 | "name": "stdout",
805 | "output_type": "stream",
806 | "text": [
807 | "20\n"
808 | ]
809 | }
810 | ],
811 | "source": [
812 | "from functools import reduce\n",
813 | "nums = [0,1,1,2,3,5,8]\n",
814 | "print(reduce(lambda a,b: a+b,nums))"
815 | ]
816 | },
817 | {
818 | "cell_type": "markdown",
819 | "metadata": {
820 | "collapsed": true
821 | },
822 | "source": [
823 | "Some regular python functions can also apply the lambda functinos. \n",
824 | "\n",
825 | "For example the **sorted** function usually sorts a list:"
826 | ]
827 | },
828 | {
829 | "cell_type": "code",
830 | "execution_count": 6,
831 | "metadata": {
832 | "collapsed": true
833 | },
834 | "outputs": [
835 | {
836 | "data": {
837 | "text/plain": [
838 | "[0, 1, 2, 3, 3, 5, 6, 8, 12, 34]"
839 | ]
840 | },
841 | "execution_count": 6,
842 | "metadata": {},
843 | "output_type": "execute_result"
844 | }
845 | ],
846 | "source": [
847 | "a = [5,3,6,1,2,3,8,12,34,0]\n",
848 | "sorted(a)"
849 | ]
850 | },
851 | {
852 | "cell_type": "markdown",
853 | "metadata": {},
854 | "source": [
855 | "Sometimes you might need to provide special conditions for your sorted list\n",
856 | "\n",
857 | "For example, if you have a list of tuples with peoples name and age, you might want to sort the list by the second value of each tuple:\n",
858 | "\n",
859 | "Remember to use the **key** parameter to specify your lambda function. "
860 | ]
861 | },
862 | {
863 | "cell_type": "code",
864 | "execution_count": 10,
865 | "metadata": {
866 | "collapsed": false,
867 | "scrolled": true
868 | },
869 | "outputs": [
870 | {
871 | "name": "stdout",
872 | "output_type": "stream",
873 | "text": [
874 | "[('Mark', 18), ('Patty', 34), ('Rob', 87), ('Tom', 22)]\n",
875 | "[('Mark', 18), ('Tom', 22), ('Patty', 34), ('Rob', 87)]\n"
876 | ]
877 | }
878 | ],
879 | "source": [
880 | "age = [(\"Mark\",18),(\"Tom\",22),(\"Rob\",87),(\"Patty\",34)]\n",
881 | "#Without the lambda function, the list will automatically be sorted by the first values of the tuples,\n",
882 | "#In this case, the first values are Strings, so they will be sorted by alphabetical order\n",
883 | "print(sorted(age))\n",
884 | "print(sorted(age,key = lambda x: x[1]))"
885 | ]
886 | },
887 | {
888 | "cell_type": "code",
889 | "execution_count": null,
890 | "metadata": {
891 | "collapsed": true
892 | },
893 | "outputs": [],
894 | "source": []
895 | }
896 | ],
897 | "metadata": {
898 | "anaconda-cloud": {},
899 | "kernelspec": {
900 | "display_name": "Python [default]",
901 | "language": "python",
902 | "name": "python3"
903 | },
904 | "language_info": {
905 | "codemirror_mode": {
906 | "name": "ipython",
907 | "version": 3
908 | },
909 | "file_extension": ".py",
910 | "mimetype": "text/x-python",
911 | "name": "python",
912 | "nbconvert_exporter": "python",
913 | "pygments_lexer": "ipython3",
914 | "version": "3.5.2"
915 | }
916 | },
917 | "nbformat": 4,
918 | "nbformat_minor": 0
919 | }
920 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/Tutorial 5 Pythonista.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
\n",
8 | "# Mars' Python Tutorial 5"
9 | ]
10 | },
11 | {
12 | "cell_type": "code",
13 | "execution_count": 27,
14 | "metadata": {
15 | "collapsed": true
16 | },
17 | "outputs": [
18 | {
19 | "data": {
20 | "text/plain": [
21 | "2.21"
22 | ]
23 | },
24 | "execution_count": 27,
25 | "metadata": {},
26 | "output_type": "execute_result"
27 | }
28 | ],
29 | "source": [
30 | "##Review Exercise: calculate the average electricity consumption while driving in the city"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 29,
36 | "metadata": {
37 | "collapsed": true
38 | },
39 | "outputs": [
40 | {
41 | "data": {
42 | "text/plain": [
43 | "8"
44 | ]
45 | },
46 | "execution_count": 29,
47 | "metadata": {},
48 | "output_type": "execute_result"
49 | }
50 | ],
51 | "source": [
52 | "##Review Exercise2: count how many different brands of cars there are, and print out all the different brands once"
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "# The power of python's one liner\n",
60 | "Quicksort with only one line (by Rohit Malshe) "
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 12,
66 | "metadata": {
67 | "collapsed": true
68 | },
69 | "outputs": [
70 | {
71 | "data": {
72 | "text/plain": [
73 | "[1, 2, 2, 5, 6, 7, 8, 8]"
74 | ]
75 | },
76 | "execution_count": 12,
77 | "metadata": {},
78 | "output_type": "execute_result"
79 | }
80 | ],
81 | "source": [
82 | "qsort = lambda l : l if len(l)<=1 else qsort([x for x in l[1:] if x < l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])\n",
83 | "qsort([1,2,5,7,8,2,6,8])"
84 | ]
85 | },
86 | {
87 | "cell_type": "markdown",
88 | "metadata": {},
89 | "source": [
90 | "# List Comprehension"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {
96 | "collapsed": true
97 | },
98 | "source": [
99 | "imagine if you can fit your for loops in one line"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 1,
105 | "metadata": {
106 | "collapsed": false
107 | },
108 | "outputs": [
109 | {
110 | "data": {
111 | "text/plain": [
112 | "[2, 4, 6, 8, 10, 12, 14]"
113 | ]
114 | },
115 | "execution_count": 1,
116 | "metadata": {},
117 | "output_type": "execute_result"
118 | }
119 | ],
120 | "source": [
121 | "a = [1,2,3,4,5,6,7]\n",
122 | "b = []\n",
123 | "for num in a: \n",
124 | " b.append(num*2)\n",
125 | "b"
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "metadata": {},
131 | "source": [
132 | "using the \"**for... in**\" syntax"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 2,
138 | "metadata": {
139 | "collapsed": false
140 | },
141 | "outputs": [
142 | {
143 | "data": {
144 | "text/plain": [
145 | "[2, 4, 6, 8, 10, 12, 14]"
146 | ]
147 | },
148 | "execution_count": 2,
149 | "metadata": {},
150 | "output_type": "execute_result"
151 | }
152 | ],
153 | "source": [
154 | "a = [1,2,3,4,5,6,7]\n",
155 | "b = [num*2 for num in a]\n",
156 | "b"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 2,
162 | "metadata": {
163 | "collapsed": true
164 | },
165 | "outputs": [
166 | {
167 | "data": {
168 | "text/plain": [
169 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
170 | ]
171 | },
172 | "execution_count": 2,
173 | "metadata": {},
174 | "output_type": "execute_result"
175 | }
176 | ],
177 | "source": [
178 | "#Exercise: using list comprehension and \"range\" syntax to store number 0 to 9 to list \"numbers\" "
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {},
184 | "source": [
185 | "We can also include if statements in list comprhension\n",
186 | "\n",
187 | "\"** if ... else ... for ... in ... **\"\n",
188 | "\n",
189 | "or\n",
190 | "\n",
191 | "\"** for ... in ... if ...**\""
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": 3,
197 | "metadata": {
198 | "collapsed": false
199 | },
200 | "outputs": [
201 | {
202 | "data": {
203 | "text/plain": [
204 | "[0, 2, 4, 6, 8, 10]"
205 | ]
206 | },
207 | "execution_count": 3,
208 | "metadata": {},
209 | "output_type": "execute_result"
210 | }
211 | ],
212 | "source": [
213 | "a = [0,1,2,3,4,5,6,7,8,9,10]\n",
214 | "b = [num for num in a if num%2 == 0]\n",
215 | "b"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": 6,
221 | "metadata": {
222 | "collapsed": true
223 | },
224 | "outputs": [
225 | {
226 | "data": {
227 | "text/plain": [
228 | "[0, 10, -2, 30, -4, 50, -6, 70, -8, 90, -10]"
229 | ]
230 | },
231 | "execution_count": 6,
232 | "metadata": {},
233 | "output_type": "execute_result"
234 | }
235 | ],
236 | "source": [
237 | "c = [-num if num%2 ==0 else num*10 for num in a]\n",
238 | "c"
239 | ]
240 | },
241 | {
242 | "cell_type": "markdown",
243 | "metadata": {},
244 | "source": [
245 | "Just like a for loop, a **for.. range(..)** syntax can also be used in list comprehension"
246 | ]
247 | },
248 | {
249 | "cell_type": "code",
250 | "execution_count": 1,
251 | "metadata": {
252 | "collapsed": false
253 | },
254 | "outputs": [
255 | {
256 | "data": {
257 | "text/plain": [
258 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
259 | ]
260 | },
261 | "execution_count": 1,
262 | "metadata": {},
263 | "output_type": "execute_result"
264 | }
265 | ],
266 | "source": [
267 | "d = [a for a in range(10)]\n",
268 | "d"
269 | ]
270 | },
271 | {
272 | "cell_type": "markdown",
273 | "metadata": {},
274 | "source": [
275 | "You can also create a list of different data structures in list comprehension\n",
276 | "Here, we creates a list of lists:"
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": 2,
282 | "metadata": {
283 | "collapsed": true
284 | },
285 | "outputs": [
286 | {
287 | "data": {
288 | "text/plain": [
289 | "[[0, 0, 0],\n",
290 | " [1, 10, 100],\n",
291 | " [2, 20, 200],\n",
292 | " [3, 30, 300],\n",
293 | " [4, 40, 400],\n",
294 | " [5, 50, 500],\n",
295 | " [6, 60, 600],\n",
296 | " [7, 70, 700],\n",
297 | " [8, 80, 800],\n",
298 | " [9, 90, 900],\n",
299 | " [10, 100, 1000],\n",
300 | " [11, 110, 1100],\n",
301 | " [12, 120, 1200],\n",
302 | " [13, 130, 1300],\n",
303 | " [14, 140, 1400],\n",
304 | " [15, 150, 1500],\n",
305 | " [16, 160, 1600],\n",
306 | " [17, 170, 1700],\n",
307 | " [18, 180, 1800],\n",
308 | " [19, 190, 1900]]"
309 | ]
310 | },
311 | "execution_count": 2,
312 | "metadata": {},
313 | "output_type": "execute_result"
314 | }
315 | ],
316 | "source": [
317 | "e = [[a*1, a*10, a*100] for a in range(20)]\n",
318 | "e"
319 | ]
320 | },
321 | {
322 | "cell_type": "markdown",
323 | "metadata": {},
324 | "source": [
325 | "the **zip** syntax can be useful when you want to do list comprehension on two lists"
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "execution_count": 3,
331 | "metadata": {
332 | "collapsed": true
333 | },
334 | "outputs": [
335 | {
336 | "data": {
337 | "text/plain": [
338 | "[('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]"
339 | ]
340 | },
341 | "execution_count": 3,
342 | "metadata": {},
343 | "output_type": "execute_result"
344 | }
345 | ],
346 | "source": [
347 | "t1 = [\"a\",\"b\",\"c\",\"d\"]\n",
348 | "t2 = [\"1\",\"2\",\"3\",\"4\"]\n",
349 | "f = [(a,b) for a,b in zip(t1,t2)]\n",
350 | "f"
351 | ]
352 | },
353 | {
354 | "cell_type": "markdown",
355 | "metadata": {},
356 | "source": [
357 | "a dictionary can also be made with using list comprehension's syntax"
358 | ]
359 | },
360 | {
361 | "cell_type": "code",
362 | "execution_count": 4,
363 | "metadata": {
364 | "collapsed": true
365 | },
366 | "outputs": [
367 | {
368 | "name": "stdout",
369 | "output_type": "stream",
370 | "text": [
371 | "d 4\n",
372 | "a 1\n",
373 | "c 3\n",
374 | "b 2\n"
375 | ]
376 | }
377 | ],
378 | "source": [
379 | "g = {a:b for a,b in zip(t1,t2)}\n",
380 | "for key,val in g.items():\n",
381 | " print(key,val)"
382 | ]
383 | },
384 | {
385 | "cell_type": "markdown",
386 | "metadata": {},
387 | "source": [
388 | "you can even open a file and store it's contents in a list with the syntax: \n",
389 | "\n",
390 | "[ **for** ... **in** **open(...)**]"
391 | ]
392 | },
393 | {
394 | "cell_type": "code",
395 | "execution_count": 5,
396 | "metadata": {
397 | "collapsed": false
398 | },
399 | "outputs": [
400 | {
401 | "data": {
402 | "text/plain": [
403 | "['line1\\n', 'line2\\n', 'line3\\n', 'line4\\n', 'line5\\n']"
404 | ]
405 | },
406 | "execution_count": 5,
407 | "metadata": {},
408 | "output_type": "execute_result"
409 | }
410 | ],
411 | "source": [
412 | "h = [a for a in open(\"practice3.txt\")]\n",
413 | "h"
414 | ]
415 | },
416 | {
417 | "cell_type": "code",
418 | "execution_count": null,
419 | "metadata": {
420 | "collapsed": true
421 | },
422 | "outputs": [],
423 | "source": [
424 | "#Exercise: remove the '\\n' symbol \n",
425 | "e1 = [a for a in open(\"practice3.txt\")] # <- make changes to this line\n",
426 | "e1"
427 | ]
428 | },
429 | {
430 | "cell_type": "code",
431 | "execution_count": 18,
432 | "metadata": {
433 | "collapsed": true
434 | },
435 | "outputs": [
436 | {
437 | "data": {
438 | "text/plain": [
439 | "['THE',\n",
440 | " 'MAIN',\n",
441 | " 'co',\n",
442 | " 'FOR',\n",
443 | " 'THE',\n",
444 | " 'ac',\n",
445 | " 'OF',\n",
446 | " 'LOVE',\n",
447 | " 'IS',\n",
448 | " 'THE',\n",
449 | " 'ov',\n",
450 | " 'OF',\n",
451 | " 'on',\n",
452 | " 'na',\n",
453 | " 'THE',\n",
454 | " 'na',\n",
455 | " 'or',\n",
456 | " 'IS',\n",
457 | " 'ONE',\n",
458 | " 'IN',\n",
459 | " 'wh',\n",
460 | " 'ONE',\n",
461 | " 'ex',\n",
462 | " 'AS',\n",
463 | " 'REAL',\n",
464 | " 'ONLY',\n",
465 | " 'THAT',\n",
466 | " 'wh',\n",
467 | " 'ex',\n",
468 | " 'wi',\n",
469 | " 'on',\n",
470 | " 'wh',\n",
471 | " 'THE',\n",
472 | " 'ph',\n",
473 | " 'IN',\n",
474 | " 'THE',\n",
475 | " 'ou',\n",
476 | " 'wo',\n",
477 | " 'HAVE',\n",
478 | " 'NO',\n",
479 | " 're',\n",
480 | " 'IN',\n",
481 | " 'th',\n",
482 | " 'BUT',\n",
483 | " 'ARE',\n",
484 | " 'ex',\n",
485 | " 'ONLY',\n",
486 | " 'FROM',\n",
487 | " 'THE',\n",
488 | " 'vi',\n",
489 | " 'OF',\n",
490 | " 'th',\n",
491 | " 'be',\n",
492 | " 'us',\n",
493 | " 'OR',\n",
494 | " 'da',\n",
495 | " 'TO',\n",
496 | " 'ONE.',\n",
497 | " 'THE',\n",
498 | " 'op',\n",
499 | " 'POLE',\n",
500 | " 'TO',\n",
501 | " 'na',\n",
502 | " 'IS',\n",
503 | " 'ob',\n",
504 | " 'IT',\n",
505 | " 'IS',\n",
506 | " 'THE',\n",
507 | " 'fa',\n",
508 | " 'TO',\n",
509 | " 'SEE',\n",
510 | " 'ot',\n",
511 | " 'pe',\n",
512 | " 'AND',\n",
513 | " 'th',\n",
514 | " 'AS',\n",
515 | " 'THEY',\n",
516 | " 'ARE,',\n",
517 | " 'ob',\n",
518 | " 'AND',\n",
519 | " 'TO',\n",
520 | " 'BE',\n",
521 | " 'ABLE',\n",
522 | " 'TO',\n",
523 | " 'se',\n",
524 | " 'THIS',\n",
525 | " 'ob',\n",
526 | " 'pi',\n",
527 | " 'FROM',\n",
528 | " 'A',\n",
529 | " 'pi',\n",
530 | " 'wh',\n",
531 | " 'IS',\n",
532 | " 'fo',\n",
533 | " 'BY',\n",
534 | " 'on',\n",
535 | " 'de',\n",
536 | " 'AND',\n",
537 | " 'fe']"
538 | ]
539 | },
540 | "execution_count": 18,
541 | "metadata": {},
542 | "output_type": "execute_result"
543 | }
544 | ],
545 | "source": [
546 | "#Exercise: read in file \"practice2.txt\". parse each word as an element in the list. \n",
547 | "#return only the first 2 characters for words longer than 5 characters\n",
548 | "#captialize words than are shorter than 5 characters\n",
549 | "#Use list comprehension!\n",
550 | "#eg: ['THE','MAIN','co','FOR','THE','ac','OF','LOVE','IS','THE','ov','OF'.........\n"
551 | ]
552 | },
553 | {
554 | "cell_type": "code",
555 | "execution_count": null,
556 | "metadata": {
557 | "collapsed": true
558 | },
559 | "outputs": [],
560 | "source": [
561 | "#Exercise: Open file \"names.txt\" and \"emails.txt\" and store each pair into a dictionary\n",
562 | "#the names and emails are in order for both files"
563 | ]
564 | },
565 | {
566 | "cell_type": "markdown",
567 | "metadata": {
568 | "collapsed": true
569 | },
570 | "source": [
571 | "# Applying List Comprehension\n",
572 | "back on our cars.csv example"
573 | ]
574 | },
575 | {
576 | "cell_type": "code",
577 | "execution_count": null,
578 | "metadata": {
579 | "collapsed": true
580 | },
581 | "outputs": [],
582 | "source": [
583 | "###Exercise: calculate the average electricity consumption while driving in the city\n",
584 | "#Using list comprehension"
585 | ]
586 | },
587 | {
588 | "cell_type": "code",
589 | "execution_count": null,
590 | "metadata": {
591 | "collapsed": true
592 | },
593 | "outputs": [],
594 | "source": [
595 | "##Exercise2: count how many different brands of cars there are, and print out all the different brands once\n",
596 | "#Using list comprehension"
597 | ]
598 | },
599 | {
600 | "cell_type": "markdown",
601 | "metadata": {},
602 | "source": [
603 | "# Lambda Functions: writing functions in 1 line"
604 | ]
605 | },
606 | {
607 | "cell_type": "markdown",
608 | "metadata": {},
609 | "source": [
610 | "lambda __variable__ : __returnValue__"
611 | ]
612 | },
613 | {
614 | "cell_type": "code",
615 | "execution_count": 11,
616 | "metadata": {
617 | "collapsed": true
618 | },
619 | "outputs": [
620 | {
621 | "data": {
622 | "text/plain": [
623 | "4"
624 | ]
625 | },
626 | "execution_count": 11,
627 | "metadata": {},
628 | "output_type": "execute_result"
629 | }
630 | ],
631 | "source": [
632 | "a = lambda x: x*2\n",
633 | "a(2)"
634 | ]
635 | },
636 | {
637 | "cell_type": "code",
638 | "execution_count": 12,
639 | "metadata": {
640 | "collapsed": true
641 | },
642 | "outputs": [
643 | {
644 | "data": {
645 | "text/plain": [
646 | "5"
647 | ]
648 | },
649 | "execution_count": 12,
650 | "metadata": {},
651 | "output_type": "execute_result"
652 | }
653 | ],
654 | "source": [
655 | "b = lambda x,y: x+y\n",
656 | "b(2,3)"
657 | ]
658 | },
659 | {
660 | "cell_type": "code",
661 | "execution_count": 1,
662 | "metadata": {
663 | "collapsed": true
664 | },
665 | "outputs": [],
666 | "source": [
667 | "#Exercise: make a lambda function named \"c\" that returns the product of two numbers\n"
668 | ]
669 | },
670 | {
671 | "cell_type": "markdown",
672 | "metadata": {},
673 | "source": [
674 | "Lambda functions can also take in conditional statements\n",
675 | "\n",
676 | "*lambda* **variable**: **returnValue1** if *conidtion* else **returnVlaue2**"
677 | ]
678 | },
679 | {
680 | "cell_type": "code",
681 | "execution_count": 5,
682 | "metadata": {
683 | "collapsed": true
684 | },
685 | "outputs": [
686 | {
687 | "name": "stdout",
688 | "output_type": "stream",
689 | "text": [
690 | "1\n",
691 | "-1\n"
692 | ]
693 | }
694 | ],
695 | "source": [
696 | "b = lambda x: -1 if x >= 0 else 1\n",
697 | "print(b(-20))\n",
698 | "print(b(99))"
699 | ]
700 | },
701 | {
702 | "cell_type": "markdown",
703 | "metadata": {},
704 | "source": [
705 | "# Using lambda functions as variables\n",
706 | "There are some standard python functions that takes in lambda functions as a variable. In this tutorial, we are going to talk about 4 of them: \n",
707 | "\n",
708 | "**Filter:**\n",
709 | "\n",
710 | "Filter calls the lambda function on it's second input (usually a list), and return only those that are *true* under the lambda condition"
711 | ]
712 | },
713 | {
714 | "cell_type": "code",
715 | "execution_count": 7,
716 | "metadata": {
717 | "collapsed": true
718 | },
719 | "outputs": [
720 | {
721 | "name": "stdout",
722 | "output_type": "stream",
723 | "text": [
724 | "\n"
725 | ]
726 | }
727 | ],
728 | "source": [
729 | "nums = [-5,2,200,-1,-19,-20]\n",
730 | "r = filter(lambda x: x >= 0, nums)\n",
731 | "print(r)"
732 | ]
733 | },
734 | {
735 | "cell_type": "markdown",
736 | "metadata": {},
737 | "source": [
738 | "As you can see, the return value of **filter** is a **filter object**, we have to manually convert the results to a list"
739 | ]
740 | },
741 | {
742 | "cell_type": "code",
743 | "execution_count": null,
744 | "metadata": {
745 | "collapsed": true
746 | },
747 | "outputs": [],
748 | "source": [
749 | "print(list(r))\n",
750 | "#or\n",
751 | "print(list(filter(lambda x: x >= 0, nums)))"
752 | ]
753 | },
754 | {
755 | "cell_type": "markdown",
756 | "metadata": {},
757 | "source": [
758 | "**Map:**\n",
759 | "\n",
760 | "The map function simply applies the lambda function to a list"
761 | ]
762 | },
763 | {
764 | "cell_type": "code",
765 | "execution_count": 8,
766 | "metadata": {
767 | "collapsed": true
768 | },
769 | "outputs": [
770 | {
771 | "name": "stdout",
772 | "output_type": "stream",
773 | "text": [
774 | "[1, 4, 9, 16, 25]\n"
775 | ]
776 | }
777 | ],
778 | "source": [
779 | "nums = [1,2,3,4,5]\n",
780 | "print(list(map(lambda x: x**2, nums)))"
781 | ]
782 | },
783 | {
784 | "cell_type": "markdown",
785 | "metadata": {},
786 | "source": [
787 | "**Reduce:**\n",
788 | "\n",
789 | "The reduce function takes in two variables, thus it's lambda function has to have two parameters as well. The function take in the first two elements from the list and return a value. Then, the value is reused again as the first parameter, with the next element of the list being the second. This process is repeated until the end of the input list.\n",
790 | "\n",
791 | "*reduce has to be imported from functools*\n",
792 | "\n",
793 | "*returns a value, not stored in reduce variable*"
794 | ]
795 | },
796 | {
797 | "cell_type": "code",
798 | "execution_count": 11,
799 | "metadata": {
800 | "collapsed": true
801 | },
802 | "outputs": [
803 | {
804 | "name": "stdout",
805 | "output_type": "stream",
806 | "text": [
807 | "20\n"
808 | ]
809 | }
810 | ],
811 | "source": [
812 | "from functools import reduce\n",
813 | "nums = [0,1,1,2,3,5,8]\n",
814 | "print(reduce(lambda a,b: a+b,nums))"
815 | ]
816 | },
817 | {
818 | "cell_type": "markdown",
819 | "metadata": {
820 | "collapsed": true
821 | },
822 | "source": [
823 | "Some regular python functions can also apply the lambda functinos. \n",
824 | "\n",
825 | "For example the **sorted** function usually sorts a list:"
826 | ]
827 | },
828 | {
829 | "cell_type": "code",
830 | "execution_count": 6,
831 | "metadata": {
832 | "collapsed": true
833 | },
834 | "outputs": [
835 | {
836 | "data": {
837 | "text/plain": [
838 | "[0, 1, 2, 3, 3, 5, 6, 8, 12, 34]"
839 | ]
840 | },
841 | "execution_count": 6,
842 | "metadata": {},
843 | "output_type": "execute_result"
844 | }
845 | ],
846 | "source": [
847 | "a = [5,3,6,1,2,3,8,12,34,0]\n",
848 | "sorted(a)"
849 | ]
850 | },
851 | {
852 | "cell_type": "markdown",
853 | "metadata": {},
854 | "source": [
855 | "Sometimes you might need to provide special conditions for your sorted list\n",
856 | "\n",
857 | "For example, if you have a list of tuples with peoples name and age, you might want to sort the list by the second value of each tuple:\n",
858 | "\n",
859 | "Remember to use the **key** parameter to specify your lambda function. "
860 | ]
861 | },
862 | {
863 | "cell_type": "code",
864 | "execution_count": 10,
865 | "metadata": {
866 | "collapsed": false,
867 | "scrolled": true
868 | },
869 | "outputs": [
870 | {
871 | "name": "stdout",
872 | "output_type": "stream",
873 | "text": [
874 | "[('Mark', 18), ('Patty', 34), ('Rob', 87), ('Tom', 22)]\n",
875 | "[('Mark', 18), ('Tom', 22), ('Patty', 34), ('Rob', 87)]\n"
876 | ]
877 | }
878 | ],
879 | "source": [
880 | "age = [(\"Mark\",18),(\"Tom\",22),(\"Rob\",87),(\"Patty\",34)]\n",
881 | "#Without the lambda function, the list will automatically be sorted by the first values of the tuples,\n",
882 | "#In this case, the first values are Strings, so they will be sorted by alphabetical order\n",
883 | "print(sorted(age))\n",
884 | "print(sorted(age,key = lambda x: x[1]))"
885 | ]
886 | },
887 | {
888 | "cell_type": "code",
889 | "execution_count": null,
890 | "metadata": {
891 | "collapsed": true
892 | },
893 | "outputs": [],
894 | "source": []
895 | }
896 | ],
897 | "metadata": {
898 | "anaconda-cloud": {},
899 | "kernelspec": {
900 | "display_name": "Python [default]",
901 | "language": "python",
902 | "name": "python3"
903 | },
904 | "language_info": {
905 | "codemirror_mode": {
906 | "name": "ipython",
907 | "version": 3
908 | },
909 | "file_extension": ".py",
910 | "mimetype": "text/x-python",
911 | "name": "python",
912 | "nbconvert_exporter": "python",
913 | "pygments_lexer": "ipython3",
914 | "version": "3.5.2"
915 | }
916 | },
917 | "nbformat": 4,
918 | "nbformat_minor": 0
919 | }
920 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/cars.csv:
--------------------------------------------------------------------------------
1 | YEAR,Make,Model,Size,(kW),Unnamed: 5,TYPE,CITY (kWh/100 km),HWY (kWh/100 km),COMB (kWh/100 km),CITY (Le/100 km),HWY (Le/100 km),COMB (Le/100 km),(g/km),RATING,(km),TIME (h)
2 | 2012,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
3 | 2012,NISSAN,LEAF,MID-SIZE,80,A1,B,19.3,23.0,21.1,2.2,2.6,2.4,0,n/a,117,7
4 | 2013,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
5 | 2013,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
6 | 2013,NISSAN,LEAF,MID-SIZE,80,A1,B,19.3,23.0,21.1,2.2,2.6,2.4,0,n/a,117,7
7 | 2013,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
8 | 2013,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
9 | 2013,TESLA,MODEL S (40 kWh battery),FULL-SIZE,270,A1,B,22.4,21.9,22.2,2.5,2.5,2.5,0,n/a,224,6
10 | 2013,TESLA,MODEL S (60 kWh battery),FULL-SIZE,270,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
11 | 2013,TESLA,MODEL S (85 kWh battery),FULL-SIZE,270,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
12 | 2013,TESLA,MODEL S PERFORMANCE,FULL-SIZE,310,A1,B,23.9,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
13 | 2014,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,n/a,131,7
14 | 2014,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
15 | 2014,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
16 | 2014,NISSAN,LEAF,MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,n/a,135,5
17 | 2014,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
18 | 2014,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
19 | 2014,TESLA,MODEL S (60 kWh battery),FULL-SIZE,225,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
20 | 2014,TESLA,MODEL S (85 kWh battery),FULL-SIZE,270,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
21 | 2014,TESLA,MODEL S PERFORMANCE,FULL-SIZE,310,A1,B,23.9,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
22 | 2015,BMW,i3,SUBCOMPACT,125,A1,B,15.2,18.8,16.8,1.7,2.1,1.9,0,n/a,130,4
23 | 2015,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,n/a,131,7
24 | 2015,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,n/a,122,4
25 | 2015,KIA,SOUL EV,STATION WAGON - SMALL,81,A1,B,17.5,22.7,19.9,2.0,2.6,2.2,0,n/a,149,4
26 | 2015,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,n/a,100,7
27 | 2015,NISSAN,LEAF,MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,n/a,135,5
28 | 2015,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
29 | 2015,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,n/a,109,8
30 | 2015,TESLA,MODEL S (60 kWh battery),FULL-SIZE,283,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,n/a,335,10
31 | 2015,TESLA,MODEL S (70 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,377,12
32 | 2015,TESLA,MODEL S (85/90 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,n/a,426,12
33 | 2015,TESLA,MODEL S 70D,FULL-SIZE,280,A1,B,20.8,20.6,20.7,2.3,2.3,2.3,0,n/a,386,12
34 | 2015,TESLA,MODEL S 85D/90D,FULL-SIZE,280,A1,B,22.0,19.8,21.0,2.5,2.2,2.4,0,n/a,435,12
35 | 2015,TESLA,MODEL S P85D/P90D,FULL-SIZE,515,A1,B,23.4,21.5,22.5,2.6,2.4,2.5,0,n/a,407,12
36 | 2016,BMW,i3,SUBCOMPACT,125,A1,B,15.2,18.8,16.8,1.7,2.1,1.9,0,10,130,4
37 | 2016,CHEVROLET,SPARK EV,SUBCOMPACT,104,A1,B,16.0,19.6,17.8,1.8,2.2,2.0,0,10,131,7
38 | 2016,FORD,FOCUS ELECTRIC,COMPACT,107,A1,B,19.0,21.1,20.0,2.1,2.4,2.2,0,10,122,4
39 | 2016,KIA,SOUL EV,STATION WAGON - SMALL,81,A1,B,17.5,22.7,19.9,2.0,2.6,2.2,0,10,149,4
40 | 2016,MITSUBISHI,i-MiEV,SUBCOMPACT,49,A1,B,16.9,21.4,18.7,1.9,2.4,2.1,0,10,100,7
41 | 2016,NISSAN,LEAF (24 kWh battery),MID-SIZE,80,A1,B,16.5,20.8,18.4,1.9,2.3,2.1,0,10,135,5
42 | 2016,NISSAN,LEAF (30 kWh battery),MID-SIZE,80,A1,B,17.0,20.7,18.6,1.9,2.3,2.1,0,10,172,6
43 | 2016,SMART,FORTWO ELECTRIC DRIVE CABRIOLET,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,10,109,8
44 | 2016,SMART,FORTWO ELECTRIC DRIVE COUPE,TWO-SEATER,35,A1,B,17.2,22.5,19.6,1.9,2.5,2.2,0,10,109,8
45 | 2016,TESLA,MODEL S (60 kWh battery),FULL-SIZE,283,A1,B,22.2,21.7,21.9,2.5,2.4,2.5,0,10,335,10
46 | 2016,TESLA,MODEL S (70 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,10,377,12
47 | 2016,TESLA,MODEL S (85/90 kWh battery),FULL-SIZE,283,A1,B,23.8,23.2,23.6,2.7,2.6,2.6,0,10,426,12
48 | 2016,TESLA,MODEL S 70D,FULL-SIZE,386,A1,B,20.8,20.6,20.7,2.3,2.3,2.3,0,10,386,12
49 | 2016,TESLA,MODEL S 85D/90D,FULL-SIZE,386,A1,B,22.0,19.8,21.0,2.5,2.2,2.4,0,10,435,12
50 | 2016,TESLA,MODEL S 90D (Refresh),FULL-SIZE,386,A1,B,20.8,19.7,20.3,2.3,2.2,2.3,0,10,473,12
51 | 2016,TESLA,MODEL S P85D/P90D,FULL-SIZE,568,A1,B,23.4,21.5,22.5,2.6,2.4,2.5,0,10,407,12
52 | 2016,TESLA,MODEL S P90D (Refresh),FULL-SIZE,568,A1,B,22.9,21.0,22.1,2.6,2.4,2.5,0,10,435,12
53 | 2016,TESLA,MODEL X 90D,SUV - STANDARD,386,A1,B,23.2,22.2,22.7,2.6,2.5,2.6,0,10,414,12
54 | 2016,TESLA,MODEL X P90D,SUV - STANDARD,568,A1,B,23.6,23.3,23.5,2.7,2.6,2.6,0,10,402,12
55 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/emails.txt:
--------------------------------------------------------------------------------
1 | YannLeCun@gmail.com
2 | GeoffreyHinton@gmail.com
3 | AndrewNg@Baidu.com
4 | IanGoodfellow@gmail.com
5 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/names.txt:
--------------------------------------------------------------------------------
1 | Yann LeCun
2 | Geoffrey Hinton
3 | Andrew Ng
4 | Ian Goodfellow
5 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/practice2.txt:
--------------------------------------------------------------------------------
1 | The main condition for the achievement of love is the overcoming of one's narcissism. The narcissistic orientation is one in which one experiences as real only that which exists within oneself, while the phenomena in the outside world have no reality in themselves, but are experienced only from the viewpoint of their being useful or dangerous to one. The opposite pole to narcissism is objectivity; it is the faculty to see other people and things as they are, objectively, and to be able to separate this objective picture from a picture which is formed by one's desires and fears.
2 |
--------------------------------------------------------------------------------
/Tutorial_5_Pythonista/practice3.txt:
--------------------------------------------------------------------------------
1 | line1
2 | line2
3 | line3
4 | line4
5 | line5
6 |
--------------------------------------------------------------------------------