├── .gitignore
├── Highlight.html
├── LICENSE
├── README.md
├── index.backup.html
├── index.html
├── notebooks
├── .ipynb_checkpoints
│ └── py3-in-one-pic-checkpoint.ipynb
├── MyModule
│ └── SubModuleOne
│ │ ├── __init__.py
│ │ └── smo.py
├── README.md
└── py3-in-one-pic.ipynb
├── py3 in one pic.mindnode
├── QuickLook
│ └── Preview.jpg
├── contents.xml
├── style.mindnodestyle
│ ├── contents.xml
│ └── metadata.plist
└── viewState.plist
├── py3 in one pic.mm
└── py3 in one pic.png
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | venv3/
3 | .DS_Store
4 |
--------------------------------------------------------------------------------
/Highlight.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | ACE in Action
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | class Animal:
15 | """This is an Animal"""
16 | def __init__(self, can_fly = False):
17 | self.can_fly = can_fly
18 | def fly(self):
19 | if self.can_fly:
20 | print("I CAN fly!")
21 | else:
22 | print("I can not fly!")
23 | class Dog(Animal):
24 | """This is a Dog"""
25 | def bark(self):
26 | print("Woof!")
27 | d = Dog()
28 | d.fly() # I can not fly!
29 | d.bark() # Woof!
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Yusheng
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Python3 in one pic
2 |
3 | [ Languages: [English](README.md) ]
4 |
5 |
6 | [](https://gitter.im/coodict/python3-in-one-pic)
7 | [](https://www.coinbase.com/rainyear)
8 |
9 |
10 | # Online Version
11 |
12 | [https://git.io/Coo-py3](https://git.io/Coo-py3)
13 |
14 | ## Notebook
15 |
16 | [IPython Notebook Version](https://github.com/coodict/python3-in-one-pic/blob/master/notebooks/py3-in-one-pic.ipynb)
17 |
18 | ## Preview
19 |
20 | 
21 |
22 | ## Releated projects
23 |
24 | * [Javascript in one pic](https://github.com/coodict/javascript-in-one-pic)
25 | * Go in one pic (in preparation)
26 |
27 | ## TODO
28 | - [X] Use IPython notebook, it's really very cool!
29 |
30 | - [X] `import this`
31 | - [X] Basic Syntax
32 | - [X] Native Datatypes
33 | - [X] Number
34 | - [X] String
35 | - [X] Boolean
36 | - [X] None
37 | - [X] Byte
38 | - [X] List
39 | - [X] Tuple
40 | - [X] Set
41 | - [X] Dict
42 | - [X] Operators & Casting
43 | - [X] Flow Control
44 | - [X] `if/elif/else`
45 | - [X] `for...in...`
46 | - [X] `while`
47 | - [X] `break` & `continue`
48 | - [X] Iterators & Generators
49 | - [X] Comprehensions
50 | - [X] Function
51 | - [X] Definition
52 | - [X] Arguments
53 | - [X] Lambda
54 | - [X] Documentation
55 | - [X] @decorator
56 | - [X] Class(OOP)
57 | - [X] `class`
58 | - [X] `__init__()` & `self`
59 | - [X] Instance
60 | - [X] Inheritance
61 | - [X] Override
62 | - [X] Module
63 | - [X] `import`
64 | - [X] Search Path
65 | - [X] Package
66 | - [ ] Pythonic
67 | - [ ] Standard Libraries
68 | - [ ] `os, sys`
69 | - [ ] `datetime`
70 |
71 | ## Donation
72 |
73 | If you find this project helpful, please consider making a donation with [bitcoin](https://www.coinbase.com/rainyear) or [other way](https://github.com/rainyear/lolita/wiki/Donation) :beers:
74 |
75 | ## References
76 |
77 | 1. [Python 3.4.3 documentation](https://docs.python.org/3/index.html)
78 | 2. [Dive Into Python 3](http://www.diveintopython3.net/table-of-contents.html)
79 | 3. Writing Idiomatic Python 3.3
80 | 4. [Google Python Style Guide](http://google.github.io/styleguide/pyguide.html)
81 | 5. [廖雪峰的Python教程](http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000)
82 |
83 | ## License
84 | See the [LICENSE](LICENSE) file for license rights and limitations (MIT).
85 |
--------------------------------------------------------------------------------
/index.backup.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
12 |
13 | Python3 in one picture
14 |
15 |
16 |
17 |
18 |
19 |
20 |
45 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 | Python3 in one picture
10 |
11 |
15 |
16 |
17 |
18 |
19 |
22 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/notebooks/.ipynb_checkpoints/py3-in-one-pic-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 20,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [
10 | {
11 | "name": "stdout",
12 | "output_type": "stream",
13 | "text": [
14 | "The Zen of Python, by Tim Peters\n",
15 | "\n",
16 | "Beautiful is better than ugly.\n",
17 | "Explicit is better than implicit.\n",
18 | "Simple is better than complex.\n",
19 | "Complex is better than complicated.\n",
20 | "Flat is better than nested.\n",
21 | "Sparse is better than dense.\n",
22 | "Readability counts.\n",
23 | "Special cases aren't special enough to break the rules.\n",
24 | "Although practicality beats purity.\n",
25 | "Errors should never pass silently.\n",
26 | "Unless explicitly silenced.\n",
27 | "In the face of ambiguity, refuse the temptation to guess.\n",
28 | "There should be one-- and preferably only one --obvious way to do it.\n",
29 | "Although that way may not be obvious at first unless you're Dutch.\n",
30 | "Now is better than never.\n",
31 | "Although never is often better than *right* now.\n",
32 | "If the implementation is hard to explain, it's a bad idea.\n",
33 | "If the implementation is easy to explain, it may be a good idea.\n",
34 | "Namespaces are one honking great idea -- let's do more of those!\n"
35 | ]
36 | }
37 | ],
38 | "source": [
39 | "import this"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "### I. Native DataTypes\n",
47 | "#### Number"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": 2,
53 | "metadata": {
54 | "collapsed": false
55 | },
56 | "outputs": [
57 | {
58 | "name": "stdout",
59 | "output_type": "stream",
60 | "text": [
61 | "\n"
62 | ]
63 | }
64 | ],
65 | "source": [
66 | "## integer\n",
67 | "a = 1\n",
68 | "b = 0x10 # 16\n",
69 | "print(type(a)) # "
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 3,
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [
79 | {
80 | "name": "stdout",
81 | "output_type": "stream",
82 | "text": [
83 | "\n"
84 | ]
85 | }
86 | ],
87 | "source": [
88 | "## float\n",
89 | "c = 1.2\n",
90 | "d = .5 # 0.5\n",
91 | "g = .314e1 # 3.14\n",
92 | "print(type(g)) # "
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 4,
98 | "metadata": {
99 | "collapsed": false
100 | },
101 | "outputs": [
102 | {
103 | "name": "stdout",
104 | "output_type": "stream",
105 | "text": [
106 | "\n",
107 | "True\n"
108 | ]
109 | }
110 | ],
111 | "source": [
112 | "## complex\n",
113 | "e = 1+2j\n",
114 | "f = complex(1, 2)\n",
115 | "print(type(e)) # \n",
116 | "print(f == e) # True"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 21,
122 | "metadata": {
123 | "collapsed": false
124 | },
125 | "outputs": [
126 | {
127 | "name": "stdout",
128 | "output_type": "stream",
129 | "text": [
130 | "2\n",
131 | "0\n",
132 | "9\n",
133 | "1.25\n",
134 | "1024\n",
135 | "1\n",
136 | "1\n"
137 | ]
138 | }
139 | ],
140 | "source": [
141 | "## Operators: + - * / ** // %\n",
142 | "print(1 + 1)\n",
143 | "\n",
144 | "print(2 - 2)\n",
145 | "\n",
146 | "print(3 * 3)\n",
147 | "\n",
148 | "print(5 / 4)\n",
149 | "\n",
150 | "print(2 ** 10)\n",
151 | "\n",
152 | "print(5 // 4)\n",
153 | "\n",
154 | "print(5 % 4)"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 27,
160 | "metadata": {
161 | "collapsed": false
162 | },
163 | "outputs": [
164 | {
165 | "name": "stdout",
166 | "output_type": "stream",
167 | "text": [
168 | "float(x) -> floating point number\n",
169 | "\n",
170 | "Convert a string or number to a floating point number, if possible.\n",
171 | "3.0\n",
172 | "3.0\n",
173 | "3.14\n"
174 | ]
175 | }
176 | ],
177 | "source": [
178 | "## Casting\n",
179 | "### Integer -> Float\n",
180 | "print(float.__doc__)\n",
181 | "\n",
182 | "print(float(3))\n",
183 | "print(3 / 1)\n",
184 | "print(float(\"3.14\"))"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 28,
190 | "metadata": {
191 | "collapsed": false
192 | },
193 | "outputs": [
194 | {
195 | "name": "stdout",
196 | "output_type": "stream",
197 | "text": [
198 | "int(x=0) -> integer\n",
199 | "int(x, base=10) -> integer\n",
200 | "\n",
201 | "Convert a number or string to an integer, or return 0 if no arguments\n",
202 | "are given. If x is a number, return x.__int__(). For floating point\n",
203 | "numbers, this truncates towards zero.\n",
204 | "\n",
205 | "If x is not a number or if base is given, then x must be a string,\n",
206 | "bytes, or bytearray instance representing an integer literal in the\n",
207 | "given base. The literal can be preceded by '+' or '-' and be surrounded\n",
208 | "by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n",
209 | "Base 0 means to interpret the base from the string as an integer literal.\n",
210 | ">>> int('0b100', base=0)\n",
211 | "4\n"
212 | ]
213 | }
214 | ],
215 | "source": [
216 | "### Float -> Integer\n",
217 | "print(int.__doc__)"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": 41,
223 | "metadata": {
224 | "collapsed": false
225 | },
226 | "outputs": [
227 | {
228 | "name": "stdout",
229 | "output_type": "stream",
230 | "text": [
231 | "3\n",
232 | "3\n",
233 | "10\n",
234 | "10\n"
235 | ]
236 | }
237 | ],
238 | "source": [
239 | "print(int(3.14)) # 3\n",
240 | "print(int(\"3\", base = 10)) # 3\n",
241 | "print(int(\"1010\", base = 2)) # 10\n",
242 | "print(int(\"0b1010\", base = 0)) # 10"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "#### String"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": 5,
255 | "metadata": {
256 | "collapsed": false
257 | },
258 | "outputs": [
259 | {
260 | "name": "stdout",
261 | "output_type": "stream",
262 | "text": [
263 | "\n",
264 | "🐶\n",
265 | ", Dogge's home, \n",
266 | "Hello,\n",
267 | "Dogge!\n",
268 | "\n"
269 | ]
270 | }
271 | ],
272 | "source": [
273 | "s1 = '🐶\\n'\n",
274 | "s2 = \"Dogge's home\"\n",
275 | "s3 = \"\"\"\n",
276 | "Hello,\n",
277 | "Dogge!\n",
278 | "\"\"\"\n",
279 | "print(type(s1)) # \n",
280 | "print(\"%s, %s, %s\" % (s1, s2, s3))"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": 6,
286 | "metadata": {
287 | "collapsed": false
288 | },
289 | "outputs": [
290 | {
291 | "name": "stdout",
292 | "output_type": "stream",
293 | "text": [
294 | "2\n"
295 | ]
296 | }
297 | ],
298 | "source": [
299 | "## Length\n",
300 | "print(len(s1)) # 2"
301 | ]
302 | },
303 | {
304 | "cell_type": "code",
305 | "execution_count": 7,
306 | "metadata": {
307 | "collapsed": false
308 | },
309 | "outputs": [
310 | {
311 | "name": "stdout",
312 | "output_type": "stream",
313 | "text": [
314 | "学:习\n"
315 | ]
316 | }
317 | ],
318 | "source": [
319 | "## Slicing\n",
320 | "s = '学而时习之'\n",
321 | "print('{0}:{1}'.format(s[0], s[-2])) # 学:习"
322 | ]
323 | },
324 | {
325 | "cell_type": "code",
326 | "execution_count": 48,
327 | "metadata": {
328 | "collapsed": false
329 | },
330 | "outputs": [
331 | {
332 | "name": "stdout",
333 | "output_type": "stream",
334 | "text": [
335 | "abc.xyz\n"
336 | ]
337 | }
338 | ],
339 | "source": [
340 | "## Operator: +\n",
341 | "print(\"abc\" + \".\" + \"xyz\")"
342 | ]
343 | },
344 | {
345 | "cell_type": "code",
346 | "execution_count": 47,
347 | "metadata": {
348 | "collapsed": false
349 | },
350 | "outputs": [
351 | {
352 | "name": "stdout",
353 | "output_type": "stream",
354 | "text": [
355 | "str(object='') -> str\n",
356 | "str(bytes_or_buffer[, encoding[, errors]]) -> str\n",
357 | "\n",
358 | "Create a new string object from the given object. If encoding or\n",
359 | "errors is specified, then the object must expose a data buffer\n",
360 | "that will be decoded using the given encoding and error handler.\n",
361 | "Otherwise, returns the result of object.__str__() (if defined)\n",
362 | "or repr(object).\n",
363 | "encoding defaults to sys.getdefaultencoding().\n",
364 | "errors defaults to 'strict'.\n",
365 | "3.14\n",
366 | "3\n",
367 | "[1, 2, 3]\n",
368 | "(1, 2, 3)\n",
369 | "{1, 2, 3}\n",
370 | "{'python': '*.py', 'javascript': '*.js'}\n"
371 | ]
372 | }
373 | ],
374 | "source": [
375 | "## Casting\n",
376 | "print(str.__doc__)\n",
377 | "\n",
378 | "print(str(3.14))\n",
379 | "print(str(3))\n",
380 | "print(str([1,2,3]))\n",
381 | "print(str((1,2,3)))\n",
382 | "print(str({1,2,3}))\n",
383 | "print(str({'python': '*.py', 'javascript': '*.js'}))"
384 | ]
385 | },
386 | {
387 | "cell_type": "markdown",
388 | "metadata": {},
389 | "source": [
390 | "#### Byte"
391 | ]
392 | },
393 | {
394 | "cell_type": "code",
395 | "execution_count": 8,
396 | "metadata": {
397 | "collapsed": false
398 | },
399 | "outputs": [
400 | {
401 | "name": "stdout",
402 | "output_type": "stream",
403 | "text": [
404 | "\n",
405 | "False\n",
406 | "True\n"
407 | ]
408 | }
409 | ],
410 | "source": [
411 | "# Byte\n",
412 | "## 0-255/x00-xff\n",
413 | "byt = b'abc'\n",
414 | "print(type(byt)) # \n",
415 | "print(byt[0] == 'a')# False\n",
416 | "print(byt[0] == 97) # True"
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": 9,
422 | "metadata": {
423 | "collapsed": false
424 | },
425 | "outputs": [
426 | {
427 | "name": "stdout",
428 | "output_type": "stream",
429 | "text": [
430 | "3\n"
431 | ]
432 | }
433 | ],
434 | "source": [
435 | "## Length\n",
436 | "print(len(byt)) # 3"
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {},
442 | "source": [
443 | "#### Boolean"
444 | ]
445 | },
446 | {
447 | "cell_type": "code",
448 | "execution_count": 10,
449 | "metadata": {
450 | "collapsed": false
451 | },
452 | "outputs": [
453 | {
454 | "name": "stdout",
455 | "output_type": "stream",
456 | "text": [
457 | "\n"
458 | ]
459 | }
460 | ],
461 | "source": [
462 | "True\n",
463 | "False\n",
464 | "print(type(True)) # "
465 | ]
466 | },
467 | {
468 | "cell_type": "markdown",
469 | "metadata": {},
470 | "source": [
471 | "#### None"
472 | ]
473 | },
474 | {
475 | "cell_type": "code",
476 | "execution_count": 11,
477 | "metadata": {
478 | "collapsed": false
479 | },
480 | "outputs": [
481 | {
482 | "name": "stdout",
483 | "output_type": "stream",
484 | "text": [
485 | "True\n",
486 | "\n"
487 | ]
488 | }
489 | ],
490 | "source": [
491 | "print(None is None) # True\n",
492 | "print(type(None)) # "
493 | ]
494 | },
495 | {
496 | "cell_type": "markdown",
497 | "metadata": {},
498 | "source": [
499 | "#### List"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": 12,
505 | "metadata": {
506 | "collapsed": false
507 | },
508 | "outputs": [
509 | {
510 | "name": "stdout",
511 | "output_type": "stream",
512 | "text": [
513 | "\n"
514 | ]
515 | }
516 | ],
517 | "source": [
518 | "l = ['python', 3, 'in', 'one']\n",
519 | "print(type(l)) # "
520 | ]
521 | },
522 | {
523 | "cell_type": "code",
524 | "execution_count": 13,
525 | "metadata": {
526 | "collapsed": false
527 | },
528 | "outputs": [
529 | {
530 | "name": "stdout",
531 | "output_type": "stream",
532 | "text": [
533 | "4\n"
534 | ]
535 | }
536 | ],
537 | "source": [
538 | "## Length\n",
539 | "print(len(l)) # 4"
540 | ]
541 | },
542 | {
543 | "cell_type": "code",
544 | "execution_count": 14,
545 | "metadata": {
546 | "collapsed": false
547 | },
548 | "outputs": [
549 | {
550 | "name": "stdout",
551 | "output_type": "stream",
552 | "text": [
553 | "python\n",
554 | "one\n",
555 | "[3, 'in']\n"
556 | ]
557 | }
558 | ],
559 | "source": [
560 | "## Slicing\n",
561 | "print(l[0]) # 'python'\n",
562 | "print(l[-1]) # 'one'\n",
563 | "print(l[1:-1]) # [3, 'in']"
564 | ]
565 | },
566 | {
567 | "cell_type": "code",
568 | "execution_count": 15,
569 | "metadata": {
570 | "collapsed": false
571 | },
572 | "outputs": [
573 | {
574 | "name": "stdout",
575 | "output_type": "stream",
576 | "text": [
577 | "['python', 3, 'in', 'one', 'pic']\n",
578 | "['python', 3, '.4.1', 'in', 'one', 'pic']\n",
579 | "['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']\n"
580 | ]
581 | }
582 | ],
583 | "source": [
584 | "## Alter\n",
585 | "l.append('pic') # None\n",
586 | "print(l)\n",
587 | "# l == ['python', 3, 'in', 'one', 'pic']\n",
588 | "\n",
589 | "l.insert(2, '.4.1') # None\n",
590 | "print(l)\n",
591 | "# l == ['python', 3, '.4.1', 'in', 'one', 'pic']\n",
592 | "\n",
593 | "l.extend(['!', '!'])\n",
594 | "print(l)\n",
595 | "# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']"
596 | ]
597 | },
598 | {
599 | "cell_type": "code",
600 | "execution_count": 16,
601 | "metadata": {
602 | "collapsed": false
603 | },
604 | "outputs": [
605 | {
606 | "name": "stdout",
607 | "output_type": "stream",
608 | "text": [
609 | "!\n",
610 | "['python', 3, '.4.1', 'in', 'one', 'pic', '!']\n",
611 | ".4.1\n",
612 | "['python', 3, 'in', 'one', 'pic', '!']\n",
613 | "['python', 3, 'one', 'pic', '!']\n",
614 | "['python', 3, 'pic', '!']\n"
615 | ]
616 | }
617 | ],
618 | "source": [
619 | "print(l.pop()) # '!'\n",
620 | "print(l)\n",
621 | "# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!']\n",
622 | "\n",
623 | "print(l.pop(2)) # '.4.1'\n",
624 | "print(l)\n",
625 | "# l == ['python', 3, 'in', 'one', 'pic', '!']\n",
626 | "\n",
627 | "l.remove(\"in\")\n",
628 | "print(l)\n",
629 | "# l == ['python', 3, 'one', 'pic', '!']\n",
630 | "\n",
631 | "del l[2]\n",
632 | "print(l)\n",
633 | "# l == ['python', 3, 'pic', '!']"
634 | ]
635 | },
636 | {
637 | "cell_type": "code",
638 | "execution_count": 17,
639 | "metadata": {
640 | "collapsed": false
641 | },
642 | "outputs": [
643 | {
644 | "name": "stdout",
645 | "output_type": "stream",
646 | "text": [
647 | "2\n"
648 | ]
649 | }
650 | ],
651 | "source": [
652 | "print(l.index('pic')) # 2"
653 | ]
654 | },
655 | {
656 | "cell_type": "markdown",
657 | "metadata": {},
658 | "source": [
659 | "#### Tuple"
660 | ]
661 | },
662 | {
663 | "cell_type": "code",
664 | "execution_count": 18,
665 | "metadata": {
666 | "collapsed": false
667 | },
668 | "outputs": [
669 | {
670 | "name": "stdout",
671 | "output_type": "stream",
672 | "text": [
673 | "\n"
674 | ]
675 | }
676 | ],
677 | "source": [
678 | "tp = (1, 2, 3, [4, 5])\n",
679 | "print(type(tp)) # "
680 | ]
681 | },
682 | {
683 | "cell_type": "code",
684 | "execution_count": 19,
685 | "metadata": {
686 | "collapsed": false
687 | },
688 | "outputs": [
689 | {
690 | "name": "stdout",
691 | "output_type": "stream",
692 | "text": [
693 | "4\n",
694 | "3\n",
695 | "(1, 2, 3, [4, 6])\n"
696 | ]
697 | }
698 | ],
699 | "source": [
700 | "## Length\n",
701 | "print(len(tp)) # 4\n",
702 | "\n",
703 | "print(tp[2]) # 3\n",
704 | "tp[3][1] = 6\n",
705 | "print(tp) # (1, 2, 3, [4, 6])"
706 | ]
707 | },
708 | {
709 | "cell_type": "code",
710 | "execution_count": 20,
711 | "metadata": {
712 | "collapsed": false
713 | },
714 | "outputs": [
715 | {
716 | "name": "stdout",
717 | "output_type": "stream",
718 | "text": [
719 | "(1,)\n"
720 | ]
721 | }
722 | ],
723 | "source": [
724 | "## Single element\n",
725 | "tp = (1, ) # Not tp = (1)\n",
726 | "print(tp)"
727 | ]
728 | },
729 | {
730 | "cell_type": "code",
731 | "execution_count": 21,
732 | "metadata": {
733 | "collapsed": false
734 | },
735 | "outputs": [
736 | {
737 | "name": "stdout",
738 | "output_type": "stream",
739 | "text": [
740 | "a 2 3\n"
741 | ]
742 | }
743 | ],
744 | "source": [
745 | "## Assign multiple values at once\n",
746 | "v = (3, 2, 'a')\n",
747 | "(c, b, a) = v\n",
748 | "print(a, b, c) # a 2 3"
749 | ]
750 | },
751 | {
752 | "cell_type": "markdown",
753 | "metadata": {},
754 | "source": [
755 | "#### Set"
756 | ]
757 | },
758 | {
759 | "cell_type": "code",
760 | "execution_count": 22,
761 | "metadata": {
762 | "collapsed": false
763 | },
764 | "outputs": [
765 | {
766 | "name": "stdout",
767 | "output_type": "stream",
768 | "text": [
769 | "\n"
770 | ]
771 | }
772 | ],
773 | "source": [
774 | "st = {'s', 'e', 'T'}\n",
775 | "print(type(st)) # "
776 | ]
777 | },
778 | {
779 | "cell_type": "code",
780 | "execution_count": 23,
781 | "metadata": {
782 | "collapsed": false
783 | },
784 | "outputs": [
785 | {
786 | "name": "stdout",
787 | "output_type": "stream",
788 | "text": [
789 | "3\n"
790 | ]
791 | }
792 | ],
793 | "source": [
794 | "## Length\n",
795 | "print(len(st)) # 3"
796 | ]
797 | },
798 | {
799 | "cell_type": "code",
800 | "execution_count": 24,
801 | "metadata": {
802 | "collapsed": false
803 | },
804 | "outputs": [
805 | {
806 | "name": "stdout",
807 | "output_type": "stream",
808 | "text": [
809 | "0\n"
810 | ]
811 | }
812 | ],
813 | "source": [
814 | "## Empty\n",
815 | "st = set()\n",
816 | "print(len(st)) # 0"
817 | ]
818 | },
819 | {
820 | "cell_type": "code",
821 | "execution_count": 25,
822 | "metadata": {
823 | "collapsed": false
824 | },
825 | "outputs": [
826 | {
827 | "name": "stdout",
828 | "output_type": "stream",
829 | "text": [
830 | "\n"
831 | ]
832 | }
833 | ],
834 | "source": [
835 | "st = {}\n",
836 | "print(type(st)) # "
837 | ]
838 | },
839 | {
840 | "cell_type": "code",
841 | "execution_count": 5,
842 | "metadata": {
843 | "collapsed": false
844 | },
845 | "outputs": [
846 | {
847 | "name": "stdout",
848 | "output_type": "stream",
849 | "text": [
850 | "{'T', '!', 's', 't', 'e'}\n",
851 | "{'s', 'e'}\n",
852 | "set()\n"
853 | ]
854 | }
855 | ],
856 | "source": [
857 | "## Alter\n",
858 | "st = set(['s', 'e', 'T'])\n",
859 | "st.add('t') # st == {'s', 'e', 't', 'T'}\n",
860 | "st.add('t') # st == {'s', 'e', 't', 'T'}\n",
861 | "st.update(['!', '!'])\n",
862 | "print(st)\n",
863 | "# st == {'s', 'e', 't', 'T', '!'}\n",
864 | "\n",
865 | "st.discard('t') # st == {'T', '!', 's', 'e'} # No Error\n",
866 | "st.remove('T') # st == {'s', 'e', '!'} # KeyError\n",
867 | "st.pop() # 's'\n",
868 | "print(st)\n",
869 | "# st == {'e'}\n",
870 | "\n",
871 | "st.clear() # st == set()\n",
872 | "print(st)"
873 | ]
874 | },
875 | {
876 | "cell_type": "markdown",
877 | "metadata": {},
878 | "source": [
879 | "#### Dict"
880 | ]
881 | },
882 | {
883 | "cell_type": "code",
884 | "execution_count": 2,
885 | "metadata": {
886 | "collapsed": false
887 | },
888 | "outputs": [
889 | {
890 | "name": "stdout",
891 | "output_type": "stream",
892 | "text": [
893 | "\n",
894 | "{'k2': 'v2', 'k1': 'v1'}\n"
895 | ]
896 | }
897 | ],
898 | "source": [
899 | "dic = {}\n",
900 | "print(type(dic)) # \n",
901 | "\n",
902 | "dic = {'k1': 'v1', 'k2': 'v2'}\n",
903 | "print(dic)"
904 | ]
905 | },
906 | {
907 | "cell_type": "code",
908 | "execution_count": 3,
909 | "metadata": {
910 | "collapsed": false
911 | },
912 | "outputs": [
913 | {
914 | "name": "stdout",
915 | "output_type": "stream",
916 | "text": [
917 | "2\n"
918 | ]
919 | }
920 | ],
921 | "source": [
922 | "## Length\n",
923 | "print(len(dic)) # 2"
924 | ]
925 | },
926 | {
927 | "cell_type": "code",
928 | "execution_count": 4,
929 | "metadata": {
930 | "collapsed": false
931 | },
932 | "outputs": [
933 | {
934 | "name": "stdout",
935 | "output_type": "stream",
936 | "text": [
937 | "v2\n",
938 | "v1\n",
939 | "v0\n",
940 | "{'k2': 'v3', 'k1': 'v1'}\n",
941 | "True\n",
942 | "False\n"
943 | ]
944 | }
945 | ],
946 | "source": [
947 | "print(dic['k2']) # 'v2'\n",
948 | "print(dic.get('k1')) # 'v1'\n",
949 | "print(dic.get('k3', 'v0')) # 'v0'\n",
950 | "\n",
951 | "dic['k2'] = 'v3'\n",
952 | "print(dic) # {'k1': 'v1', 'k2': 'v3'}\n",
953 | "\n",
954 | "print('k2' in dic) # True\n",
955 | "print('v1' in dic) # False"
956 | ]
957 | },
958 | {
959 | "cell_type": "markdown",
960 | "metadata": {},
961 | "source": [
962 | "### III. Flow Control\n",
963 | "\n",
964 | "#### If"
965 | ]
966 | },
967 | {
968 | "cell_type": "code",
969 | "execution_count": 6,
970 | "metadata": {
971 | "collapsed": false
972 | },
973 | "outputs": [
974 | {
975 | "name": "stdout",
976 | "output_type": "stream",
977 | "text": [
978 | "Version 3.X\n"
979 | ]
980 | }
981 | ],
982 | "source": [
983 | "import sys\n",
984 | "if sys.version_info.major < 3:\n",
985 | " print(\"Version 2.X\")\n",
986 | "elif sys.version_info.major > 3:\n",
987 | " print(\"Future\")\n",
988 | "else:\n",
989 | " print(\"Version 3.X\")"
990 | ]
991 | },
992 | {
993 | "cell_type": "markdown",
994 | "metadata": {},
995 | "source": [
996 | "#### Loop"
997 | ]
998 | },
999 | {
1000 | "cell_type": "markdown",
1001 | "metadata": {},
1002 | "source": [
1003 | "**for**"
1004 | ]
1005 | },
1006 | {
1007 | "cell_type": "code",
1008 | "execution_count": 49,
1009 | "metadata": {
1010 | "collapsed": false
1011 | },
1012 | "outputs": [
1013 | {
1014 | "name": "stdout",
1015 | "output_type": "stream",
1016 | "text": [
1017 | "H\n",
1018 | "e\n",
1019 | "l\n",
1020 | "l\n",
1021 | "o\n"
1022 | ]
1023 | }
1024 | ],
1025 | "source": [
1026 | "for i in \"Hello\":\n",
1027 | " print(i)"
1028 | ]
1029 | },
1030 | {
1031 | "cell_type": "markdown",
1032 | "metadata": {},
1033 | "source": [
1034 | "**while**"
1035 | ]
1036 | },
1037 | {
1038 | "cell_type": "code",
1039 | "execution_count": 8,
1040 | "metadata": {
1041 | "collapsed": false
1042 | },
1043 | "outputs": [
1044 | {
1045 | "name": "stdout",
1046 | "output_type": "stream",
1047 | "text": [
1048 | "362880\n"
1049 | ]
1050 | }
1051 | ],
1052 | "source": [
1053 | "prod = 1\n",
1054 | "i = 1\n",
1055 | "while i < 10:\n",
1056 | " prod = prod * i\n",
1057 | " i += 1\n",
1058 | "print(prod)"
1059 | ]
1060 | },
1061 | {
1062 | "cell_type": "code",
1063 | "execution_count": 59,
1064 | "metadata": {
1065 | "collapsed": false
1066 | },
1067 | "outputs": [
1068 | {
1069 | "name": "stdout",
1070 | "output_type": "stream",
1071 | "text": [
1072 | "Found an even number 2\n",
1073 | "Found an even number 4\n",
1074 | "Found an even number 6\n",
1075 | "n > 5!\n"
1076 | ]
1077 | }
1078 | ],
1079 | "source": [
1080 | "## break & continue\n",
1081 | "for n in range(2, 10):\n",
1082 | " if n % 2 == 0:\n",
1083 | " print(\"Found an even number \", n)\n",
1084 | " continue\n",
1085 | " if n > 5:\n",
1086 | " print(\"n > 5!\")\n",
1087 | " break"
1088 | ]
1089 | },
1090 | {
1091 | "cell_type": "code",
1092 | "execution_count": 57,
1093 | "metadata": {
1094 | "collapsed": false
1095 | },
1096 | "outputs": [
1097 | {
1098 | "name": "stdout",
1099 | "output_type": "stream",
1100 | "text": [
1101 | "Found an even number 2\n",
1102 | "Found a number 3\n",
1103 | "Found an even number 4\n",
1104 | "Found a number 5\n",
1105 | "Found an even number 6\n",
1106 | "Found a number 7\n",
1107 | "Found an even number 8\n",
1108 | "Found a number 9\n"
1109 | ]
1110 | }
1111 | ],
1112 | "source": [
1113 | "## continue\n",
1114 | "for num in range(2, 10):\n",
1115 | " if num % 2 == 0:\n",
1116 | " print(\"Found an even number\", num)\n",
1117 | " continue\n",
1118 | " print(\"Found a number\", num)"
1119 | ]
1120 | },
1121 | {
1122 | "cell_type": "markdown",
1123 | "metadata": {},
1124 | "source": [
1125 | "#### Comprehension"
1126 | ]
1127 | },
1128 | {
1129 | "cell_type": "markdown",
1130 | "metadata": {},
1131 | "source": [
1132 | "**List**"
1133 | ]
1134 | },
1135 | {
1136 | "cell_type": "code",
1137 | "execution_count": 18,
1138 | "metadata": {
1139 | "collapsed": false
1140 | },
1141 | "outputs": [
1142 | {
1143 | "name": "stdout",
1144 | "output_type": "stream",
1145 | "text": [
1146 | "[4, 6, 8, 10, 12, 14, 16, 18]\n",
1147 | "[(0, 0), (0, 1), (1, 0), (1, 1)]\n"
1148 | ]
1149 | }
1150 | ],
1151 | "source": [
1152 | "s = [2 * x for x in range(10) if x ** 2 > 3]\n",
1153 | "print(s)\n",
1154 | "\n",
1155 | "pairs = [(x, y) for x in range(2) for y in range(2)]\n",
1156 | "print(pairs)"
1157 | ]
1158 | },
1159 | {
1160 | "cell_type": "markdown",
1161 | "metadata": {},
1162 | "source": [
1163 | "**Set**"
1164 | ]
1165 | },
1166 | {
1167 | "cell_type": "code",
1168 | "execution_count": 17,
1169 | "metadata": {
1170 | "collapsed": false
1171 | },
1172 | "outputs": [
1173 | {
1174 | "name": "stdout",
1175 | "output_type": "stream",
1176 | "text": [
1177 | "{4, 6, 8, 10, 12, 14, 16, 18}\n",
1178 | "{(0, 1), (1, 0), (0, 0), (1, 1)}\n"
1179 | ]
1180 | }
1181 | ],
1182 | "source": [
1183 | "s = {2 * x for x in range(10) if x ** 2 > 3}\n",
1184 | "print(s)\n",
1185 | "\n",
1186 | "pairs = set([(x, y) for x in range(2) for y in range(2)])\n",
1187 | "print(pairs)"
1188 | ]
1189 | },
1190 | {
1191 | "cell_type": "markdown",
1192 | "metadata": {},
1193 | "source": [
1194 | "**Dict**"
1195 | ]
1196 | },
1197 | {
1198 | "cell_type": "code",
1199 | "execution_count": 15,
1200 | "metadata": {
1201 | "collapsed": false
1202 | },
1203 | "outputs": [
1204 | {
1205 | "name": "stdout",
1206 | "output_type": "stream",
1207 | "text": [
1208 | "{'Python': 6, 'Javascript': 10, 'Golang': 6}\n",
1209 | "{10: 'Javascript', 6: 'Golang'}\n"
1210 | ]
1211 | }
1212 | ],
1213 | "source": [
1214 | "ls = {s: len(s) for s in [\"Python\", \"Javascript\", \"Golang\"]}\n",
1215 | "print(ls)\n",
1216 | "\n",
1217 | "sl = {v: k for k, v in ls.items()}\n",
1218 | "print(sl)"
1219 | ]
1220 | },
1221 | {
1222 | "cell_type": "markdown",
1223 | "metadata": {},
1224 | "source": [
1225 | "#### Iterators & Generators"
1226 | ]
1227 | },
1228 | {
1229 | "cell_type": "code",
1230 | "execution_count": 30,
1231 | "metadata": {
1232 | "collapsed": false
1233 | },
1234 | "outputs": [
1235 | {
1236 | "name": "stdout",
1237 | "output_type": "stream",
1238 | "text": [
1239 | "\n",
1240 | "P\n",
1241 | "y\n",
1242 | "t\n",
1243 | "h\n",
1244 | "o\n",
1245 | "n\n"
1246 | ]
1247 | }
1248 | ],
1249 | "source": [
1250 | "python = iter(\"Python\")\n",
1251 | "print(python)\n",
1252 | "for i in python:\n",
1253 | " print(i)"
1254 | ]
1255 | },
1256 | {
1257 | "cell_type": "code",
1258 | "execution_count": 32,
1259 | "metadata": {
1260 | "collapsed": false
1261 | },
1262 | "outputs": [
1263 | {
1264 | "name": "stdout",
1265 | "output_type": "stream",
1266 | "text": [
1267 | "\n",
1268 | "n\n",
1269 | "o\n",
1270 | "h\n",
1271 | "t\n",
1272 | "y\n",
1273 | "P\n"
1274 | ]
1275 | }
1276 | ],
1277 | "source": [
1278 | "def reverse(data):\n",
1279 | " for index in range(len(data)-1, -1, -1):\n",
1280 | " yield data[index]\n",
1281 | "nohtyp = reverse(\"Python\")\n",
1282 | "print(nohtyp)\n",
1283 | "for i in nohtyp:\n",
1284 | " print(i)"
1285 | ]
1286 | },
1287 | {
1288 | "cell_type": "markdown",
1289 | "metadata": {},
1290 | "source": [
1291 | "### IV. Function\n",
1292 | "#### Definition"
1293 | ]
1294 | },
1295 | {
1296 | "cell_type": "code",
1297 | "execution_count": 64,
1298 | "metadata": {
1299 | "collapsed": false
1300 | },
1301 | "outputs": [
1302 | {
1303 | "name": "stdout",
1304 | "output_type": "stream",
1305 | "text": [
1306 | "Hello, World!\n",
1307 | "return 'Hello, World!'\n"
1308 | ]
1309 | }
1310 | ],
1311 | "source": [
1312 | "def f():\n",
1313 | " \"\"\"return 'Hello, World!'\"\"\"\n",
1314 | " return \"Hello, World!\"\n",
1315 | "\n",
1316 | "print(f())\n",
1317 | "print(f.__doc__)"
1318 | ]
1319 | },
1320 | {
1321 | "cell_type": "markdown",
1322 | "metadata": {},
1323 | "source": [
1324 | "#### Arguments"
1325 | ]
1326 | },
1327 | {
1328 | "cell_type": "code",
1329 | "execution_count": 69,
1330 | "metadata": {
1331 | "collapsed": false
1332 | },
1333 | "outputs": [
1334 | {
1335 | "name": "stdout",
1336 | "output_type": "stream",
1337 | "text": [
1338 | "Hello, World!\n",
1339 | "Hello, Python!\n"
1340 | ]
1341 | }
1342 | ],
1343 | "source": [
1344 | "## default arguments\n",
1345 | "def f(name = \"World\"):\n",
1346 | " \"\"\"return 'Hello, $name'\"\"\"\n",
1347 | " return \"Hello, {}!\".format(name)\n",
1348 | "print(f())\n",
1349 | "print(f(\"Python\"))"
1350 | ]
1351 | },
1352 | {
1353 | "cell_type": "code",
1354 | "execution_count": 74,
1355 | "metadata": {
1356 | "collapsed": false
1357 | },
1358 | "outputs": [
1359 | {
1360 | "name": "stdout",
1361 | "output_type": "stream",
1362 | "text": [
1363 | "Hello, Python!\n",
1364 | "Bye, C/C++!\n"
1365 | ]
1366 | }
1367 | ],
1368 | "source": [
1369 | "## keyword arguments\n",
1370 | "def f(v, l = \"Python\"):\n",
1371 | " \"\"\"return '$v, $l'\"\"\"\n",
1372 | " return \"{}, {}!\".format(v, l)\n",
1373 | "print(f(\"Hello\"))\n",
1374 | "print(f(\"Bye\", \"C/C++\"))"
1375 | ]
1376 | },
1377 | {
1378 | "cell_type": "code",
1379 | "execution_count": 102,
1380 | "metadata": {
1381 | "collapsed": false
1382 | },
1383 | "outputs": [
1384 | {
1385 | "name": "stdout",
1386 | "output_type": "stream",
1387 | "text": [
1388 | "True\n",
1389 | "Hello Python/C/C++\n"
1390 | ]
1391 | }
1392 | ],
1393 | "source": [
1394 | "## arbitrary arguments\n",
1395 | "def f(*args, con = \" & \"):\n",
1396 | " print(isinstance(args, tuple))\n",
1397 | " print(\"Hello\", con.join(args))\n",
1398 | "\n",
1399 | "f(\"Python\", \"C\", \"C++\", con = \"/\")"
1400 | ]
1401 | },
1402 | {
1403 | "cell_type": "code",
1404 | "execution_count": 107,
1405 | "metadata": {
1406 | "collapsed": false
1407 | },
1408 | "outputs": [
1409 | {
1410 | "name": "stdout",
1411 | "output_type": "stream",
1412 | "text": [
1413 | "args ('Python', 'Javascript')\n",
1414 | "kargs {'ms': 'C++', 'fp': 'Haskell'}\n",
1415 | "FP: Haskell & Scripts: Python/Javascript\n"
1416 | ]
1417 | }
1418 | ],
1419 | "source": [
1420 | "def f(*args, **kargs):\n",
1421 | " print(\"args \", args)\n",
1422 | " print(\"kargs \", kargs)\n",
1423 | " print(\"FP: {} & Scripts: {}\".format(kargs.get(\"fp\"), \"/\".join(args)))\n",
1424 | " \n",
1425 | "f(\"Python\", \"Javascript\", ms = \"C++\", fp = \"Haskell\")"
1426 | ]
1427 | },
1428 | {
1429 | "cell_type": "markdown",
1430 | "metadata": {},
1431 | "source": [
1432 | "#### Lambda"
1433 | ]
1434 | },
1435 | {
1436 | "cell_type": "code",
1437 | "execution_count": 112,
1438 | "metadata": {
1439 | "collapsed": false
1440 | },
1441 | "outputs": [
1442 | {
1443 | "name": "stdout",
1444 | "output_type": "stream",
1445 | "text": [
1446 | "[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]\n",
1447 | "[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n"
1448 | ]
1449 | }
1450 | ],
1451 | "source": [
1452 | "pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n",
1453 | "pairs.sort(key=lambda pair: pair[1])\n",
1454 | "print(pairs)\n",
1455 | "pairs.sort(key=lambda pair: pair[0])\n",
1456 | "print(pairs)"
1457 | ]
1458 | },
1459 | {
1460 | "cell_type": "markdown",
1461 | "metadata": {},
1462 | "source": [
1463 | "#### @decorator"
1464 | ]
1465 | },
1466 | {
1467 | "cell_type": "code",
1468 | "execution_count": 120,
1469 | "metadata": {
1470 | "collapsed": false
1471 | },
1472 | "outputs": [
1473 | {
1474 | "name": "stdout",
1475 | "output_type": "stream",
1476 | "text": [
1477 | "Hey log~\n",
1478 | "This is fa!\n",
1479 | "Bye log~\n",
1480 | "**********\n",
1481 | "Hey log~\n",
1482 | "This is fb!\n",
1483 | "Bye log~\n"
1484 | ]
1485 | }
1486 | ],
1487 | "source": [
1488 | "def log(f):\n",
1489 | " def wrapper():\n",
1490 | " print(\"Hey log~\")\n",
1491 | " f()\n",
1492 | " print(\"Bye log~\")\n",
1493 | " return wrapper\n",
1494 | "\n",
1495 | "@log\n",
1496 | "def fa():\n",
1497 | " print(\"This is fa!\")\n",
1498 | "\n",
1499 | "# Equal to...\n",
1500 | "def fb():\n",
1501 | " print(\"This is fb!\")\n",
1502 | "fb = log(fb)\n",
1503 | "\n",
1504 | "fa()\n",
1505 | "print(\"*\"*10)\n",
1506 | "fb()"
1507 | ]
1508 | },
1509 | {
1510 | "cell_type": "markdown",
1511 | "metadata": {},
1512 | "source": [
1513 | "### V. Class(OOP)"
1514 | ]
1515 | },
1516 | {
1517 | "cell_type": "markdown",
1518 | "metadata": {},
1519 | "source": [
1520 | "### `class`"
1521 | ]
1522 | },
1523 | {
1524 | "cell_type": "code",
1525 | "execution_count": 10,
1526 | "metadata": {
1527 | "collapsed": false
1528 | },
1529 | "outputs": [
1530 | {
1531 | "name": "stdout",
1532 | "output_type": "stream",
1533 | "text": [
1534 | "I can fly!\n",
1535 | "This is an Animal\n"
1536 | ]
1537 | }
1538 | ],
1539 | "source": [
1540 | "class Animal:\n",
1541 | " \"\"\"This is an Animal\"\"\"\n",
1542 | " def fly(_):\n",
1543 | " print(\"I can fly!\")\n",
1544 | "a = Animal()\n",
1545 | "a.fly() # I can fly!\n",
1546 | "print(a.__doc__) # This is an Animal"
1547 | ]
1548 | },
1549 | {
1550 | "cell_type": "markdown",
1551 | "metadata": {},
1552 | "source": [
1553 | "### `__init__` & `self`"
1554 | ]
1555 | },
1556 | {
1557 | "cell_type": "code",
1558 | "execution_count": 15,
1559 | "metadata": {
1560 | "collapsed": false
1561 | },
1562 | "outputs": [
1563 | {
1564 | "name": "stdout",
1565 | "output_type": "stream",
1566 | "text": [
1567 | "Calling __init__() when instantiation!\n",
1568 | "I can not fly!\n",
1569 | "Calling __init__() when instantiation!\n",
1570 | "I CAN fly!\n"
1571 | ]
1572 | }
1573 | ],
1574 | "source": [
1575 | "class Animal:\n",
1576 | " \"\"\"This is an Animal\"\"\"\n",
1577 | " def __init__(self, can_fly = False):\n",
1578 | " print(\"Calling __init__() when instantiation!\")\n",
1579 | " self.can_fly = can_fly\n",
1580 | " def fly(self):\n",
1581 | " if self.can_fly:\n",
1582 | " print(\"I CAN fly!\")\n",
1583 | " else:\n",
1584 | " print(\"I can not fly!\")\n",
1585 | "a = Animal() # Calling __init__() when instantiation!\n",
1586 | "a.fly() # I can not fly!\n",
1587 | "\n",
1588 | "b = Animal(can_fly = True) # Calling __init__() when instantiation!\n",
1589 | "b.fly() # I CAN fly!"
1590 | ]
1591 | },
1592 | {
1593 | "cell_type": "markdown",
1594 | "metadata": {},
1595 | "source": [
1596 | "### Instance"
1597 | ]
1598 | },
1599 | {
1600 | "cell_type": "code",
1601 | "execution_count": 19,
1602 | "metadata": {
1603 | "collapsed": false
1604 | },
1605 | "outputs": [
1606 | {
1607 | "name": "stdout",
1608 | "output_type": "stream",
1609 | "text": [
1610 | "True\n",
1611 | "False\n"
1612 | ]
1613 | }
1614 | ],
1615 | "source": [
1616 | "class Animal:\n",
1617 | " pass\n",
1618 | "class Human:\n",
1619 | " pass\n",
1620 | "a = Animal()\n",
1621 | "h = Human()\n",
1622 | "print(isinstance(a, Animal))\n",
1623 | "print(isinstance(h, Animal))"
1624 | ]
1625 | },
1626 | {
1627 | "cell_type": "markdown",
1628 | "metadata": {},
1629 | "source": [
1630 | "### Inheritance"
1631 | ]
1632 | },
1633 | {
1634 | "cell_type": "code",
1635 | "execution_count": 22,
1636 | "metadata": {
1637 | "collapsed": false
1638 | },
1639 | "outputs": [
1640 | {
1641 | "name": "stdout",
1642 | "output_type": "stream",
1643 | "text": [
1644 | "I can not fly!\n",
1645 | "汪汪!\n"
1646 | ]
1647 | }
1648 | ],
1649 | "source": [
1650 | "class Animal:\n",
1651 | " \"\"\"This is an Animal\"\"\"\n",
1652 | " def __init__(self, can_fly = False):\n",
1653 | " self.can_fly = can_fly\n",
1654 | " def fly(self):\n",
1655 | " if self.can_fly:\n",
1656 | " print(\"I CAN fly!\")\n",
1657 | " else:\n",
1658 | " print(\"I can not fly!\")\n",
1659 | "class Dog(Animal):\n",
1660 | " \"\"\"This is a Dog\"\"\"\n",
1661 | " def bark(self):\n",
1662 | " print(\"汪汪!\")\n",
1663 | "d = Dog()\n",
1664 | "d.fly()\n",
1665 | "d.bark()"
1666 | ]
1667 | },
1668 | {
1669 | "cell_type": "markdown",
1670 | "metadata": {},
1671 | "source": [
1672 | "### Override"
1673 | ]
1674 | },
1675 | {
1676 | "cell_type": "code",
1677 | "execution_count": 25,
1678 | "metadata": {
1679 | "collapsed": false
1680 | },
1681 | "outputs": [
1682 | {
1683 | "name": "stdout",
1684 | "output_type": "stream",
1685 | "text": [
1686 | "I'm flying high!\n"
1687 | ]
1688 | }
1689 | ],
1690 | "source": [
1691 | "class Animal:\n",
1692 | " \"\"\"This is an Animal\"\"\"\n",
1693 | " def __init__(self, can_fly = False):\n",
1694 | " self.can_fly = can_fly\n",
1695 | " def fly(self):\n",
1696 | " if self.can_fly:\n",
1697 | " print(\"I CAN fly!\")\n",
1698 | " else:\n",
1699 | " print(\"I can not fly!\")\n",
1700 | "class Bird:\n",
1701 | " \"\"\"This is a Bird\"\"\"\n",
1702 | " def fly(self):\n",
1703 | " print(\"I'm flying high!\")\n",
1704 | "bird = Bird()\n",
1705 | "bird.fly() # I'm flying high!"
1706 | ]
1707 | },
1708 | {
1709 | "cell_type": "markdown",
1710 | "metadata": {},
1711 | "source": [
1712 | "### VI. Module"
1713 | ]
1714 | },
1715 | {
1716 | "cell_type": "markdown",
1717 | "metadata": {},
1718 | "source": [
1719 | "#### `import`"
1720 | ]
1721 | },
1722 | {
1723 | "cell_type": "code",
1724 | "execution_count": 37,
1725 | "metadata": {
1726 | "collapsed": false
1727 | },
1728 | "outputs": [
1729 | {
1730 | "name": "stdout",
1731 | "output_type": "stream",
1732 | "text": [
1733 | "posix\n",
1734 | "VERSON: 3.5\n",
1735 | "3.141592653589793\n"
1736 | ]
1737 | }
1738 | ],
1739 | "source": [
1740 | "import os\n",
1741 | "print(os.name)\n",
1742 | "\n",
1743 | "from sys import version_info as PY_VERSION\n",
1744 | "print(\"VERSON: {}.{}\".format(PY_VERSION.major, PY_VERSION.minor))\n",
1745 | "\n",
1746 | "from math import *\n",
1747 | "print(pi)"
1748 | ]
1749 | },
1750 | {
1751 | "cell_type": "markdown",
1752 | "metadata": {},
1753 | "source": [
1754 | "#### Search Path\n",
1755 | "\n",
1756 | "1. current directory\n",
1757 | "2. `echo $PYTHONPATH`\n",
1758 | "3. `sys.path`"
1759 | ]
1760 | },
1761 | {
1762 | "cell_type": "markdown",
1763 | "metadata": {},
1764 | "source": [
1765 | "#### Package"
1766 | ]
1767 | },
1768 | {
1769 | "cell_type": "code",
1770 | "execution_count": 40,
1771 | "metadata": {
1772 | "collapsed": false
1773 | },
1774 | "outputs": [
1775 | {
1776 | "ename": "ImportError",
1777 | "evalue": "No module named 'MyModule.SubModule'",
1778 | "output_type": "error",
1779 | "traceback": [
1780 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1781 | "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
1782 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Running MyModule.SubModuleOne.smo!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \"\"\"\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mMyModule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSubModule\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msmo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0msmo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;31m# Running MyModule.SubModuleOne.smo!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1783 | "\u001b[0;31mImportError\u001b[0m: No module named 'MyModule.SubModule'"
1784 | ]
1785 | }
1786 | ],
1787 | "source": [
1788 | "\"\"\"\n",
1789 | "MyModule/\n",
1790 | "|--SubModuleOne/\n",
1791 | " |--__init__.py\n",
1792 | " |--smo.py\n",
1793 | "\n",
1794 | "# smo.py\n",
1795 | "def run():\n",
1796 | " print(\"Running MyModule.SubModuleOne.smo!\")\n",
1797 | "\"\"\"\n",
1798 | "from MyModule.SubModule import smo\n",
1799 | "smo.run()\n",
1800 | "# Running MyModule.SubModuleOne.smo!"
1801 | ]
1802 | },
1803 | {
1804 | "cell_type": "markdown",
1805 | "metadata": {},
1806 | "source": [
1807 | "### VII. Pythonic\n",
1808 | "\n",
1809 | "### VIII. Standard Libraries"
1810 | ]
1811 | },
1812 | {
1813 | "cell_type": "code",
1814 | "execution_count": null,
1815 | "metadata": {
1816 | "collapsed": true
1817 | },
1818 | "outputs": [],
1819 | "source": []
1820 | }
1821 | ],
1822 | "metadata": {
1823 | "kernelspec": {
1824 | "display_name": "Python 3",
1825 | "language": "python",
1826 | "name": "python3"
1827 | },
1828 | "language_info": {
1829 | "codemirror_mode": {
1830 | "name": "ipython",
1831 | "version": 3
1832 | },
1833 | "file_extension": ".py",
1834 | "mimetype": "text/x-python",
1835 | "name": "python",
1836 | "nbconvert_exporter": "python",
1837 | "pygments_lexer": "ipython3",
1838 | "version": "3.5.0"
1839 | }
1840 | },
1841 | "nbformat": 4,
1842 | "nbformat_minor": 0
1843 | }
1844 |
--------------------------------------------------------------------------------
/notebooks/MyModule/SubModuleOne/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/notebooks/MyModule/SubModuleOne/__init__.py
--------------------------------------------------------------------------------
/notebooks/MyModule/SubModuleOne/smo.py:
--------------------------------------------------------------------------------
1 | def run():
2 | print("Running MyModule.SubModuleOne.smo!")
3 |
--------------------------------------------------------------------------------
/notebooks/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/notebooks/README.md
--------------------------------------------------------------------------------
/notebooks/py3-in-one-pic.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 20,
6 | "metadata": {
7 | "collapsed": false
8 | },
9 | "outputs": [
10 | {
11 | "name": "stdout",
12 | "output_type": "stream",
13 | "text": [
14 | "The Zen of Python, by Tim Peters\n",
15 | "\n",
16 | "Beautiful is better than ugly.\n",
17 | "Explicit is better than implicit.\n",
18 | "Simple is better than complex.\n",
19 | "Complex is better than complicated.\n",
20 | "Flat is better than nested.\n",
21 | "Sparse is better than dense.\n",
22 | "Readability counts.\n",
23 | "Special cases aren't special enough to break the rules.\n",
24 | "Although practicality beats purity.\n",
25 | "Errors should never pass silently.\n",
26 | "Unless explicitly silenced.\n",
27 | "In the face of ambiguity, refuse the temptation to guess.\n",
28 | "There should be one-- and preferably only one --obvious way to do it.\n",
29 | "Although that way may not be obvious at first unless you're Dutch.\n",
30 | "Now is better than never.\n",
31 | "Although never is often better than *right* now.\n",
32 | "If the implementation is hard to explain, it's a bad idea.\n",
33 | "If the implementation is easy to explain, it may be a good idea.\n",
34 | "Namespaces are one honking great idea -- let's do more of those!\n"
35 | ]
36 | }
37 | ],
38 | "source": [
39 | "import this"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "### I. Native DataTypes\n",
47 | "#### Number"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": 2,
53 | "metadata": {
54 | "collapsed": false
55 | },
56 | "outputs": [
57 | {
58 | "name": "stdout",
59 | "output_type": "stream",
60 | "text": [
61 | "\n"
62 | ]
63 | }
64 | ],
65 | "source": [
66 | "## integer\n",
67 | "a = 1\n",
68 | "b = 0x10 # 16\n",
69 | "print(type(a)) # "
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 3,
75 | "metadata": {
76 | "collapsed": false
77 | },
78 | "outputs": [
79 | {
80 | "name": "stdout",
81 | "output_type": "stream",
82 | "text": [
83 | "\n"
84 | ]
85 | }
86 | ],
87 | "source": [
88 | "## float\n",
89 | "c = 1.2\n",
90 | "d = .5 # 0.5\n",
91 | "g = .314e1 # 3.14\n",
92 | "print(type(g)) # "
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 4,
98 | "metadata": {
99 | "collapsed": false
100 | },
101 | "outputs": [
102 | {
103 | "name": "stdout",
104 | "output_type": "stream",
105 | "text": [
106 | "\n",
107 | "True\n"
108 | ]
109 | }
110 | ],
111 | "source": [
112 | "## complex\n",
113 | "e = 1+2j\n",
114 | "f = complex(1, 2)\n",
115 | "print(type(e)) # \n",
116 | "print(f == e) # True"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 21,
122 | "metadata": {
123 | "collapsed": false
124 | },
125 | "outputs": [
126 | {
127 | "name": "stdout",
128 | "output_type": "stream",
129 | "text": [
130 | "2\n",
131 | "0\n",
132 | "9\n",
133 | "1.25\n",
134 | "1024\n",
135 | "1\n",
136 | "1\n"
137 | ]
138 | }
139 | ],
140 | "source": [
141 | "## Operators: + - * / ** // %\n",
142 | "print(1 + 1)\n",
143 | "\n",
144 | "print(2 - 2)\n",
145 | "\n",
146 | "print(3 * 3)\n",
147 | "\n",
148 | "print(5 / 4)\n",
149 | "\n",
150 | "print(2 ** 10)\n",
151 | "\n",
152 | "print(5 // 4)\n",
153 | "\n",
154 | "print(5 % 4)"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 27,
160 | "metadata": {
161 | "collapsed": false
162 | },
163 | "outputs": [
164 | {
165 | "name": "stdout",
166 | "output_type": "stream",
167 | "text": [
168 | "float(x) -> floating point number\n",
169 | "\n",
170 | "Convert a string or number to a floating point number, if possible.\n",
171 | "3.0\n",
172 | "3.0\n",
173 | "3.14\n"
174 | ]
175 | }
176 | ],
177 | "source": [
178 | "## Casting\n",
179 | "### Integer -> Float\n",
180 | "print(float.__doc__)\n",
181 | "\n",
182 | "print(float(3))\n",
183 | "print(3 / 1)\n",
184 | "print(float(\"3.14\"))"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 28,
190 | "metadata": {
191 | "collapsed": false
192 | },
193 | "outputs": [
194 | {
195 | "name": "stdout",
196 | "output_type": "stream",
197 | "text": [
198 | "int(x=0) -> integer\n",
199 | "int(x, base=10) -> integer\n",
200 | "\n",
201 | "Convert a number or string to an integer, or return 0 if no arguments\n",
202 | "are given. If x is a number, return x.__int__(). For floating point\n",
203 | "numbers, this truncates towards zero.\n",
204 | "\n",
205 | "If x is not a number or if base is given, then x must be a string,\n",
206 | "bytes, or bytearray instance representing an integer literal in the\n",
207 | "given base. The literal can be preceded by '+' or '-' and be surrounded\n",
208 | "by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n",
209 | "Base 0 means to interpret the base from the string as an integer literal.\n",
210 | ">>> int('0b100', base=0)\n",
211 | "4\n"
212 | ]
213 | }
214 | ],
215 | "source": [
216 | "### Float -> Integer\n",
217 | "print(int.__doc__)"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": 41,
223 | "metadata": {
224 | "collapsed": false
225 | },
226 | "outputs": [
227 | {
228 | "name": "stdout",
229 | "output_type": "stream",
230 | "text": [
231 | "3\n",
232 | "3\n",
233 | "10\n",
234 | "10\n"
235 | ]
236 | }
237 | ],
238 | "source": [
239 | "print(int(3.14)) # 3\n",
240 | "print(int(\"3\", base = 10)) # 3\n",
241 | "print(int(\"1010\", base = 2)) # 10\n",
242 | "print(int(\"0b1010\", base = 0)) # 10"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "#### String"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "execution_count": 1,
255 | "metadata": {
256 | "collapsed": false
257 | },
258 | "outputs": [
259 | {
260 | "name": "stdout",
261 | "output_type": "stream",
262 | "text": [
263 | "\n",
264 | ":dog:\n",
265 | ", Dogge's home, \n",
266 | "Hello,\n",
267 | "Dogge!\n",
268 | "\n"
269 | ]
270 | }
271 | ],
272 | "source": [
273 | "s1 = ':dog:\\n'\n",
274 | "s2 = \"Dogge's home\"\n",
275 | "s3 = \"\"\"\n",
276 | "Hello,\n",
277 | "Dogge!\n",
278 | "\"\"\"\n",
279 | "print(type(s1)) # \n",
280 | "print(\"%s, %s, %s\" % (s1, s2, s3))"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": 6,
286 | "metadata": {
287 | "collapsed": false
288 | },
289 | "outputs": [
290 | {
291 | "name": "stdout",
292 | "output_type": "stream",
293 | "text": [
294 | "2\n"
295 | ]
296 | }
297 | ],
298 | "source": [
299 | "## Length\n",
300 | "print(len(s1)) # 2"
301 | ]
302 | },
303 | {
304 | "cell_type": "code",
305 | "execution_count": 4,
306 | "metadata": {
307 | "collapsed": false
308 | },
309 | "outputs": [
310 | {
311 | "name": "stdout",
312 | "output_type": "stream",
313 | "text": [
314 | "study:practice\n"
315 | ]
316 | }
317 | ],
318 | "source": [
319 | "## Slicing\n",
320 | "s = 'study and practice'\n",
321 | "print('{0}:{1}'.format(s[:5], s[-8:])) # study:practice"
322 | ]
323 | },
324 | {
325 | "cell_type": "code",
326 | "execution_count": 48,
327 | "metadata": {
328 | "collapsed": false
329 | },
330 | "outputs": [
331 | {
332 | "name": "stdout",
333 | "output_type": "stream",
334 | "text": [
335 | "abc.xyz\n"
336 | ]
337 | }
338 | ],
339 | "source": [
340 | "## Operator: +\n",
341 | "print(\"abc\" + \".\" + \"xyz\")"
342 | ]
343 | },
344 | {
345 | "cell_type": "code",
346 | "execution_count": 47,
347 | "metadata": {
348 | "collapsed": false
349 | },
350 | "outputs": [
351 | {
352 | "name": "stdout",
353 | "output_type": "stream",
354 | "text": [
355 | "str(object='') -> str\n",
356 | "str(bytes_or_buffer[, encoding[, errors]]) -> str\n",
357 | "\n",
358 | "Create a new string object from the given object. If encoding or\n",
359 | "errors is specified, then the object must expose a data buffer\n",
360 | "that will be decoded using the given encoding and error handler.\n",
361 | "Otherwise, returns the result of object.__str__() (if defined)\n",
362 | "or repr(object).\n",
363 | "encoding defaults to sys.getdefaultencoding().\n",
364 | "errors defaults to 'strict'.\n",
365 | "3.14\n",
366 | "3\n",
367 | "[1, 2, 3]\n",
368 | "(1, 2, 3)\n",
369 | "{1, 2, 3}\n",
370 | "{'python': '*.py', 'javascript': '*.js'}\n"
371 | ]
372 | }
373 | ],
374 | "source": [
375 | "## Casting\n",
376 | "print(str.__doc__)\n",
377 | "\n",
378 | "print(str(3.14))\n",
379 | "print(str(3))\n",
380 | "print(str([1,2,3]))\n",
381 | "print(str((1,2,3)))\n",
382 | "print(str({1,2,3}))\n",
383 | "print(str({'python': '*.py', 'javascript': '*.js'}))"
384 | ]
385 | },
386 | {
387 | "cell_type": "markdown",
388 | "metadata": {},
389 | "source": [
390 | "#### Byte"
391 | ]
392 | },
393 | {
394 | "cell_type": "code",
395 | "execution_count": 8,
396 | "metadata": {
397 | "collapsed": false
398 | },
399 | "outputs": [
400 | {
401 | "name": "stdout",
402 | "output_type": "stream",
403 | "text": [
404 | "\n",
405 | "False\n",
406 | "True\n"
407 | ]
408 | }
409 | ],
410 | "source": [
411 | "# Byte\n",
412 | "## 0-255/x00-xff\n",
413 | "byt = b'abc'\n",
414 | "print(type(byt)) # \n",
415 | "print(byt[0] == 'a')# False\n",
416 | "print(byt[0] == 97) # True"
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": 9,
422 | "metadata": {
423 | "collapsed": false
424 | },
425 | "outputs": [
426 | {
427 | "name": "stdout",
428 | "output_type": "stream",
429 | "text": [
430 | "3\n"
431 | ]
432 | }
433 | ],
434 | "source": [
435 | "## Length\n",
436 | "print(len(byt)) # 3"
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {},
442 | "source": [
443 | "#### Boolean"
444 | ]
445 | },
446 | {
447 | "cell_type": "code",
448 | "execution_count": 10,
449 | "metadata": {
450 | "collapsed": false
451 | },
452 | "outputs": [
453 | {
454 | "name": "stdout",
455 | "output_type": "stream",
456 | "text": [
457 | "\n"
458 | ]
459 | }
460 | ],
461 | "source": [
462 | "True\n",
463 | "False\n",
464 | "print(type(True)) # "
465 | ]
466 | },
467 | {
468 | "cell_type": "markdown",
469 | "metadata": {},
470 | "source": [
471 | "#### None"
472 | ]
473 | },
474 | {
475 | "cell_type": "code",
476 | "execution_count": 11,
477 | "metadata": {
478 | "collapsed": false
479 | },
480 | "outputs": [
481 | {
482 | "name": "stdout",
483 | "output_type": "stream",
484 | "text": [
485 | "True\n",
486 | "\n"
487 | ]
488 | }
489 | ],
490 | "source": [
491 | "print(None is None) # True\n",
492 | "print(type(None)) # "
493 | ]
494 | },
495 | {
496 | "cell_type": "markdown",
497 | "metadata": {},
498 | "source": [
499 | "#### List"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": 12,
505 | "metadata": {
506 | "collapsed": false
507 | },
508 | "outputs": [
509 | {
510 | "name": "stdout",
511 | "output_type": "stream",
512 | "text": [
513 | "\n"
514 | ]
515 | }
516 | ],
517 | "source": [
518 | "l = ['python', 3, 'in', 'one']\n",
519 | "print(type(l)) # "
520 | ]
521 | },
522 | {
523 | "cell_type": "code",
524 | "execution_count": 13,
525 | "metadata": {
526 | "collapsed": false
527 | },
528 | "outputs": [
529 | {
530 | "name": "stdout",
531 | "output_type": "stream",
532 | "text": [
533 | "4\n"
534 | ]
535 | }
536 | ],
537 | "source": [
538 | "## Length\n",
539 | "print(len(l)) # 4"
540 | ]
541 | },
542 | {
543 | "cell_type": "code",
544 | "execution_count": 14,
545 | "metadata": {
546 | "collapsed": false
547 | },
548 | "outputs": [
549 | {
550 | "name": "stdout",
551 | "output_type": "stream",
552 | "text": [
553 | "python\n",
554 | "one\n",
555 | "[3, 'in']\n"
556 | ]
557 | }
558 | ],
559 | "source": [
560 | "## Slicing\n",
561 | "print(l[0]) # 'python'\n",
562 | "print(l[-1]) # 'one'\n",
563 | "print(l[1:-1]) # [3, 'in']"
564 | ]
565 | },
566 | {
567 | "cell_type": "code",
568 | "execution_count": 15,
569 | "metadata": {
570 | "collapsed": false
571 | },
572 | "outputs": [
573 | {
574 | "name": "stdout",
575 | "output_type": "stream",
576 | "text": [
577 | "['python', 3, 'in', 'one', 'pic']\n",
578 | "['python', 3, '.4.1', 'in', 'one', 'pic']\n",
579 | "['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']\n"
580 | ]
581 | }
582 | ],
583 | "source": [
584 | "## Alter\n",
585 | "l.append('pic') # None\n",
586 | "print(l)\n",
587 | "# l == ['python', 3, 'in', 'one', 'pic']\n",
588 | "\n",
589 | "l.insert(2, '.4.1') # None\n",
590 | "print(l)\n",
591 | "# l == ['python', 3, '.4.1', 'in', 'one', 'pic']\n",
592 | "\n",
593 | "l.extend(['!', '!'])\n",
594 | "print(l)\n",
595 | "# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!', '!']"
596 | ]
597 | },
598 | {
599 | "cell_type": "code",
600 | "execution_count": 16,
601 | "metadata": {
602 | "collapsed": false
603 | },
604 | "outputs": [
605 | {
606 | "name": "stdout",
607 | "output_type": "stream",
608 | "text": [
609 | "!\n",
610 | "['python', 3, '.4.1', 'in', 'one', 'pic', '!']\n",
611 | ".4.1\n",
612 | "['python', 3, 'in', 'one', 'pic', '!']\n",
613 | "['python', 3, 'one', 'pic', '!']\n",
614 | "['python', 3, 'pic', '!']\n"
615 | ]
616 | }
617 | ],
618 | "source": [
619 | "print(l.pop()) # '!'\n",
620 | "print(l)\n",
621 | "# l == ['python', 3, '.4.1', 'in', 'one', 'pic', '!']\n",
622 | "\n",
623 | "print(l.pop(2)) # '.4.1'\n",
624 | "print(l)\n",
625 | "# l == ['python', 3, 'in', 'one', 'pic', '!']\n",
626 | "\n",
627 | "l.remove(\"in\")\n",
628 | "print(l)\n",
629 | "# l == ['python', 3, 'one', 'pic', '!']\n",
630 | "\n",
631 | "del l[2]\n",
632 | "print(l)\n",
633 | "# l == ['python', 3, 'pic', '!']"
634 | ]
635 | },
636 | {
637 | "cell_type": "code",
638 | "execution_count": 17,
639 | "metadata": {
640 | "collapsed": false
641 | },
642 | "outputs": [
643 | {
644 | "name": "stdout",
645 | "output_type": "stream",
646 | "text": [
647 | "2\n"
648 | ]
649 | }
650 | ],
651 | "source": [
652 | "print(l.index('pic')) # 2"
653 | ]
654 | },
655 | {
656 | "cell_type": "markdown",
657 | "metadata": {},
658 | "source": [
659 | "#### Tuple"
660 | ]
661 | },
662 | {
663 | "cell_type": "code",
664 | "execution_count": 18,
665 | "metadata": {
666 | "collapsed": false
667 | },
668 | "outputs": [
669 | {
670 | "name": "stdout",
671 | "output_type": "stream",
672 | "text": [
673 | "\n"
674 | ]
675 | }
676 | ],
677 | "source": [
678 | "tp = (1, 2, 3, [4, 5])\n",
679 | "print(type(tp)) # "
680 | ]
681 | },
682 | {
683 | "cell_type": "code",
684 | "execution_count": 19,
685 | "metadata": {
686 | "collapsed": false
687 | },
688 | "outputs": [
689 | {
690 | "name": "stdout",
691 | "output_type": "stream",
692 | "text": [
693 | "4\n",
694 | "3\n",
695 | "(1, 2, 3, [4, 6])\n"
696 | ]
697 | }
698 | ],
699 | "source": [
700 | "## Length\n",
701 | "print(len(tp)) # 4\n",
702 | "\n",
703 | "print(tp[2]) # 3\n",
704 | "tp[3][1] = 6\n",
705 | "print(tp) # (1, 2, 3, [4, 6])"
706 | ]
707 | },
708 | {
709 | "cell_type": "code",
710 | "execution_count": 20,
711 | "metadata": {
712 | "collapsed": false
713 | },
714 | "outputs": [
715 | {
716 | "name": "stdout",
717 | "output_type": "stream",
718 | "text": [
719 | "(1,)\n"
720 | ]
721 | }
722 | ],
723 | "source": [
724 | "## Single element\n",
725 | "tp = (1, ) # Not tp = (1)\n",
726 | "print(tp)"
727 | ]
728 | },
729 | {
730 | "cell_type": "code",
731 | "execution_count": 21,
732 | "metadata": {
733 | "collapsed": false
734 | },
735 | "outputs": [
736 | {
737 | "name": "stdout",
738 | "output_type": "stream",
739 | "text": [
740 | "a 2 3\n"
741 | ]
742 | }
743 | ],
744 | "source": [
745 | "## Assign multiple values at once\n",
746 | "v = (3, 2, 'a')\n",
747 | "(c, b, a) = v\n",
748 | "print(a, b, c) # a 2 3"
749 | ]
750 | },
751 | {
752 | "cell_type": "markdown",
753 | "metadata": {},
754 | "source": [
755 | "#### Set"
756 | ]
757 | },
758 | {
759 | "cell_type": "code",
760 | "execution_count": 22,
761 | "metadata": {
762 | "collapsed": false
763 | },
764 | "outputs": [
765 | {
766 | "name": "stdout",
767 | "output_type": "stream",
768 | "text": [
769 | "\n"
770 | ]
771 | }
772 | ],
773 | "source": [
774 | "st = {'s', 'e', 'T'}\n",
775 | "print(type(st)) # "
776 | ]
777 | },
778 | {
779 | "cell_type": "code",
780 | "execution_count": 23,
781 | "metadata": {
782 | "collapsed": false
783 | },
784 | "outputs": [
785 | {
786 | "name": "stdout",
787 | "output_type": "stream",
788 | "text": [
789 | "3\n"
790 | ]
791 | }
792 | ],
793 | "source": [
794 | "## Length\n",
795 | "print(len(st)) # 3"
796 | ]
797 | },
798 | {
799 | "cell_type": "code",
800 | "execution_count": 24,
801 | "metadata": {
802 | "collapsed": false
803 | },
804 | "outputs": [
805 | {
806 | "name": "stdout",
807 | "output_type": "stream",
808 | "text": [
809 | "0\n"
810 | ]
811 | }
812 | ],
813 | "source": [
814 | "## Empty\n",
815 | "st = set()\n",
816 | "print(len(st)) # 0"
817 | ]
818 | },
819 | {
820 | "cell_type": "code",
821 | "execution_count": 25,
822 | "metadata": {
823 | "collapsed": false
824 | },
825 | "outputs": [
826 | {
827 | "name": "stdout",
828 | "output_type": "stream",
829 | "text": [
830 | "\n"
831 | ]
832 | }
833 | ],
834 | "source": [
835 | "st = {}\n",
836 | "print(type(st)) # "
837 | ]
838 | },
839 | {
840 | "cell_type": "code",
841 | "execution_count": 5,
842 | "metadata": {
843 | "collapsed": false
844 | },
845 | "outputs": [
846 | {
847 | "name": "stdout",
848 | "output_type": "stream",
849 | "text": [
850 | "{'T', '!', 's', 't', 'e'}\n",
851 | "{'s', 'e'}\n",
852 | "set()\n"
853 | ]
854 | }
855 | ],
856 | "source": [
857 | "## Alter\n",
858 | "st = set(['s', 'e', 'T'])\n",
859 | "st.add('t') # st == {'s', 'e', 't', 'T'}\n",
860 | "st.add('t') # st == {'s', 'e', 't', 'T'}\n",
861 | "st.update(['!', '!'])\n",
862 | "print(st)\n",
863 | "# st == {'s', 'e', 't', 'T', '!'}\n",
864 | "\n",
865 | "st.discard('t') # st == {'T', '!', 's', 'e'} # No Error\n",
866 | "st.remove('T') # st == {'s', 'e', '!'} # KeyError\n",
867 | "st.pop() # 's'\n",
868 | "print(st)\n",
869 | "# st == {'e'}\n",
870 | "\n",
871 | "st.clear() # st == set()\n",
872 | "print(st)"
873 | ]
874 | },
875 | {
876 | "cell_type": "markdown",
877 | "metadata": {},
878 | "source": [
879 | "#### Dict"
880 | ]
881 | },
882 | {
883 | "cell_type": "code",
884 | "execution_count": 2,
885 | "metadata": {
886 | "collapsed": false
887 | },
888 | "outputs": [
889 | {
890 | "name": "stdout",
891 | "output_type": "stream",
892 | "text": [
893 | "\n",
894 | "{'k2': 'v2', 'k1': 'v1'}\n"
895 | ]
896 | }
897 | ],
898 | "source": [
899 | "dic = {}\n",
900 | "print(type(dic)) # \n",
901 | "\n",
902 | "dic = {'k1': 'v1', 'k2': 'v2'}\n",
903 | "print(dic)"
904 | ]
905 | },
906 | {
907 | "cell_type": "code",
908 | "execution_count": 3,
909 | "metadata": {
910 | "collapsed": false
911 | },
912 | "outputs": [
913 | {
914 | "name": "stdout",
915 | "output_type": "stream",
916 | "text": [
917 | "2\n"
918 | ]
919 | }
920 | ],
921 | "source": [
922 | "## Length\n",
923 | "print(len(dic)) # 2"
924 | ]
925 | },
926 | {
927 | "cell_type": "code",
928 | "execution_count": 4,
929 | "metadata": {
930 | "collapsed": false
931 | },
932 | "outputs": [
933 | {
934 | "name": "stdout",
935 | "output_type": "stream",
936 | "text": [
937 | "v2\n",
938 | "v1\n",
939 | "v0\n",
940 | "{'k2': 'v3', 'k1': 'v1'}\n",
941 | "True\n",
942 | "False\n"
943 | ]
944 | }
945 | ],
946 | "source": [
947 | "print(dic['k2']) # 'v2'\n",
948 | "print(dic.get('k1')) # 'v1'\n",
949 | "print(dic.get('k3', 'v0')) # 'v0'\n",
950 | "\n",
951 | "dic['k2'] = 'v3'\n",
952 | "print(dic) # {'k1': 'v1', 'k2': 'v3'}\n",
953 | "\n",
954 | "print('k2' in dic) # True\n",
955 | "print('v1' in dic) # False"
956 | ]
957 | },
958 | {
959 | "cell_type": "markdown",
960 | "metadata": {},
961 | "source": [
962 | "### III. Flow Control\n",
963 | "\n",
964 | "#### If"
965 | ]
966 | },
967 | {
968 | "cell_type": "code",
969 | "execution_count": 6,
970 | "metadata": {
971 | "collapsed": false
972 | },
973 | "outputs": [
974 | {
975 | "name": "stdout",
976 | "output_type": "stream",
977 | "text": [
978 | "Version 3.X\n"
979 | ]
980 | }
981 | ],
982 | "source": [
983 | "import sys\n",
984 | "if sys.version_info.major < 3:\n",
985 | " print(\"Version 2.X\")\n",
986 | "elif sys.version_info.major > 3:\n",
987 | " print(\"Future\")\n",
988 | "else:\n",
989 | " print(\"Version 3.X\")"
990 | ]
991 | },
992 | {
993 | "cell_type": "markdown",
994 | "metadata": {},
995 | "source": [
996 | "#### Loop"
997 | ]
998 | },
999 | {
1000 | "cell_type": "markdown",
1001 | "metadata": {},
1002 | "source": [
1003 | "**for**"
1004 | ]
1005 | },
1006 | {
1007 | "cell_type": "code",
1008 | "execution_count": 49,
1009 | "metadata": {
1010 | "collapsed": false
1011 | },
1012 | "outputs": [
1013 | {
1014 | "name": "stdout",
1015 | "output_type": "stream",
1016 | "text": [
1017 | "H\n",
1018 | "e\n",
1019 | "l\n",
1020 | "l\n",
1021 | "o\n"
1022 | ]
1023 | }
1024 | ],
1025 | "source": [
1026 | "for i in \"Hello\":\n",
1027 | " print(i)"
1028 | ]
1029 | },
1030 | {
1031 | "cell_type": "markdown",
1032 | "metadata": {},
1033 | "source": [
1034 | "**while**"
1035 | ]
1036 | },
1037 | {
1038 | "cell_type": "code",
1039 | "execution_count": 8,
1040 | "metadata": {
1041 | "collapsed": false
1042 | },
1043 | "outputs": [
1044 | {
1045 | "name": "stdout",
1046 | "output_type": "stream",
1047 | "text": [
1048 | "362880\n"
1049 | ]
1050 | }
1051 | ],
1052 | "source": [
1053 | "prod = 1\n",
1054 | "i = 1\n",
1055 | "while i < 10:\n",
1056 | " prod = prod * i\n",
1057 | " i += 1\n",
1058 | "print(prod)"
1059 | ]
1060 | },
1061 | {
1062 | "cell_type": "code",
1063 | "execution_count": 59,
1064 | "metadata": {
1065 | "collapsed": false
1066 | },
1067 | "outputs": [
1068 | {
1069 | "name": "stdout",
1070 | "output_type": "stream",
1071 | "text": [
1072 | "Found an even number 2\n",
1073 | "Found an even number 4\n",
1074 | "Found an even number 6\n",
1075 | "n > 5!\n"
1076 | ]
1077 | }
1078 | ],
1079 | "source": [
1080 | "## break & continue\n",
1081 | "for n in range(2, 10):\n",
1082 | " if n % 2 == 0:\n",
1083 | " print(\"Found an even number \", n)\n",
1084 | " continue\n",
1085 | " if n > 5:\n",
1086 | " print(\"n > 5!\")\n",
1087 | " break"
1088 | ]
1089 | },
1090 | {
1091 | "cell_type": "code",
1092 | "execution_count": 57,
1093 | "metadata": {
1094 | "collapsed": false
1095 | },
1096 | "outputs": [
1097 | {
1098 | "name": "stdout",
1099 | "output_type": "stream",
1100 | "text": [
1101 | "Found an even number 2\n",
1102 | "Found a number 3\n",
1103 | "Found an even number 4\n",
1104 | "Found a number 5\n",
1105 | "Found an even number 6\n",
1106 | "Found a number 7\n",
1107 | "Found an even number 8\n",
1108 | "Found a number 9\n"
1109 | ]
1110 | }
1111 | ],
1112 | "source": [
1113 | "## continue\n",
1114 | "for num in range(2, 10):\n",
1115 | " if num % 2 == 0:\n",
1116 | " print(\"Found an even number\", num)\n",
1117 | " continue\n",
1118 | " print(\"Found a number\", num)"
1119 | ]
1120 | },
1121 | {
1122 | "cell_type": "markdown",
1123 | "metadata": {},
1124 | "source": [
1125 | "#### Comprehension"
1126 | ]
1127 | },
1128 | {
1129 | "cell_type": "markdown",
1130 | "metadata": {},
1131 | "source": [
1132 | "**List**"
1133 | ]
1134 | },
1135 | {
1136 | "cell_type": "code",
1137 | "execution_count": 18,
1138 | "metadata": {
1139 | "collapsed": false
1140 | },
1141 | "outputs": [
1142 | {
1143 | "name": "stdout",
1144 | "output_type": "stream",
1145 | "text": [
1146 | "[4, 6, 8, 10, 12, 14, 16, 18]\n",
1147 | "[(0, 0), (0, 1), (1, 0), (1, 1)]\n"
1148 | ]
1149 | }
1150 | ],
1151 | "source": [
1152 | "s = [2 * x for x in range(10) if x ** 2 > 3]\n",
1153 | "print(s)\n",
1154 | "\n",
1155 | "pairs = [(x, y) for x in range(2) for y in range(2)]\n",
1156 | "print(pairs)"
1157 | ]
1158 | },
1159 | {
1160 | "cell_type": "markdown",
1161 | "metadata": {},
1162 | "source": [
1163 | "**Set**"
1164 | ]
1165 | },
1166 | {
1167 | "cell_type": "code",
1168 | "execution_count": 17,
1169 | "metadata": {
1170 | "collapsed": false
1171 | },
1172 | "outputs": [
1173 | {
1174 | "name": "stdout",
1175 | "output_type": "stream",
1176 | "text": [
1177 | "{4, 6, 8, 10, 12, 14, 16, 18}\n",
1178 | "{(0, 1), (1, 0), (0, 0), (1, 1)}\n"
1179 | ]
1180 | }
1181 | ],
1182 | "source": [
1183 | "s = {2 * x for x in range(10) if x ** 2 > 3}\n",
1184 | "print(s)\n",
1185 | "\n",
1186 | "pairs = set([(x, y) for x in range(2) for y in range(2)])\n",
1187 | "print(pairs)"
1188 | ]
1189 | },
1190 | {
1191 | "cell_type": "markdown",
1192 | "metadata": {},
1193 | "source": [
1194 | "**Dict**"
1195 | ]
1196 | },
1197 | {
1198 | "cell_type": "code",
1199 | "execution_count": 15,
1200 | "metadata": {
1201 | "collapsed": false
1202 | },
1203 | "outputs": [
1204 | {
1205 | "name": "stdout",
1206 | "output_type": "stream",
1207 | "text": [
1208 | "{'Python': 6, 'Javascript': 10, 'Golang': 6}\n",
1209 | "{10: 'Javascript', 6: 'Golang'}\n"
1210 | ]
1211 | }
1212 | ],
1213 | "source": [
1214 | "ls = {s: len(s) for s in [\"Python\", \"Javascript\", \"Golang\"]}\n",
1215 | "print(ls)\n",
1216 | "\n",
1217 | "sl = {v: k for k, v in ls.items()}\n",
1218 | "print(sl)"
1219 | ]
1220 | },
1221 | {
1222 | "cell_type": "markdown",
1223 | "metadata": {},
1224 | "source": [
1225 | "#### Iterators & Generators"
1226 | ]
1227 | },
1228 | {
1229 | "cell_type": "code",
1230 | "execution_count": 30,
1231 | "metadata": {
1232 | "collapsed": false
1233 | },
1234 | "outputs": [
1235 | {
1236 | "name": "stdout",
1237 | "output_type": "stream",
1238 | "text": [
1239 | "\n",
1240 | "P\n",
1241 | "y\n",
1242 | "t\n",
1243 | "h\n",
1244 | "o\n",
1245 | "n\n"
1246 | ]
1247 | }
1248 | ],
1249 | "source": [
1250 | "python = iter(\"Python\")\n",
1251 | "print(python)\n",
1252 | "for i in python:\n",
1253 | " print(i)"
1254 | ]
1255 | },
1256 | {
1257 | "cell_type": "code",
1258 | "execution_count": 32,
1259 | "metadata": {
1260 | "collapsed": false
1261 | },
1262 | "outputs": [
1263 | {
1264 | "name": "stdout",
1265 | "output_type": "stream",
1266 | "text": [
1267 | "\n",
1268 | "n\n",
1269 | "o\n",
1270 | "h\n",
1271 | "t\n",
1272 | "y\n",
1273 | "P\n"
1274 | ]
1275 | }
1276 | ],
1277 | "source": [
1278 | "def reverse(data):\n",
1279 | " for index in range(len(data)-1, -1, -1):\n",
1280 | " yield data[index]\n",
1281 | "nohtyp = reverse(\"Python\")\n",
1282 | "print(nohtyp)\n",
1283 | "for i in nohtyp:\n",
1284 | " print(i)"
1285 | ]
1286 | },
1287 | {
1288 | "cell_type": "markdown",
1289 | "metadata": {},
1290 | "source": [
1291 | "### IV. Function\n",
1292 | "#### Definition"
1293 | ]
1294 | },
1295 | {
1296 | "cell_type": "code",
1297 | "execution_count": 64,
1298 | "metadata": {
1299 | "collapsed": false
1300 | },
1301 | "outputs": [
1302 | {
1303 | "name": "stdout",
1304 | "output_type": "stream",
1305 | "text": [
1306 | "Hello, World!\n",
1307 | "return 'Hello, World!'\n"
1308 | ]
1309 | }
1310 | ],
1311 | "source": [
1312 | "def f():\n",
1313 | " \"\"\"return 'Hello, World!'\"\"\"\n",
1314 | " return \"Hello, World!\"\n",
1315 | "\n",
1316 | "print(f())\n",
1317 | "print(f.__doc__)"
1318 | ]
1319 | },
1320 | {
1321 | "cell_type": "markdown",
1322 | "metadata": {},
1323 | "source": [
1324 | "#### Arguments"
1325 | ]
1326 | },
1327 | {
1328 | "cell_type": "code",
1329 | "execution_count": 69,
1330 | "metadata": {
1331 | "collapsed": false
1332 | },
1333 | "outputs": [
1334 | {
1335 | "name": "stdout",
1336 | "output_type": "stream",
1337 | "text": [
1338 | "Hello, World!\n",
1339 | "Hello, Python!\n"
1340 | ]
1341 | }
1342 | ],
1343 | "source": [
1344 | "## default arguments\n",
1345 | "def f(name = \"World\"):\n",
1346 | " \"\"\"return 'Hello, $name'\"\"\"\n",
1347 | " return \"Hello, {}!\".format(name)\n",
1348 | "print(f())\n",
1349 | "print(f(\"Python\"))"
1350 | ]
1351 | },
1352 | {
1353 | "cell_type": "code",
1354 | "execution_count": 74,
1355 | "metadata": {
1356 | "collapsed": false
1357 | },
1358 | "outputs": [
1359 | {
1360 | "name": "stdout",
1361 | "output_type": "stream",
1362 | "text": [
1363 | "Hello, Python!\n",
1364 | "Bye, C/C++!\n"
1365 | ]
1366 | }
1367 | ],
1368 | "source": [
1369 | "## keyword arguments\n",
1370 | "def f(v, l = \"Python\"):\n",
1371 | " \"\"\"return '$v, $l'\"\"\"\n",
1372 | " return \"{}, {}!\".format(v, l)\n",
1373 | "print(f(\"Hello\"))\n",
1374 | "print(f(\"Bye\", \"C/C++\"))"
1375 | ]
1376 | },
1377 | {
1378 | "cell_type": "code",
1379 | "execution_count": 102,
1380 | "metadata": {
1381 | "collapsed": false
1382 | },
1383 | "outputs": [
1384 | {
1385 | "name": "stdout",
1386 | "output_type": "stream",
1387 | "text": [
1388 | "True\n",
1389 | "Hello Python/C/C++\n"
1390 | ]
1391 | }
1392 | ],
1393 | "source": [
1394 | "## arbitrary arguments\n",
1395 | "def f(*args, con = \" & \"):\n",
1396 | " print(isinstance(args, tuple))\n",
1397 | " print(\"Hello\", con.join(args))\n",
1398 | "\n",
1399 | "f(\"Python\", \"C\", \"C++\", con = \"/\")"
1400 | ]
1401 | },
1402 | {
1403 | "cell_type": "code",
1404 | "execution_count": 107,
1405 | "metadata": {
1406 | "collapsed": false
1407 | },
1408 | "outputs": [
1409 | {
1410 | "name": "stdout",
1411 | "output_type": "stream",
1412 | "text": [
1413 | "args ('Python', 'Javascript')\n",
1414 | "kargs {'ms': 'C++', 'fp': 'Haskell'}\n",
1415 | "FP: Haskell & Scripts: Python/Javascript\n"
1416 | ]
1417 | }
1418 | ],
1419 | "source": [
1420 | "def f(*args, **kargs):\n",
1421 | " print(\"args \", args)\n",
1422 | " print(\"kargs \", kargs)\n",
1423 | " print(\"FP: {} & Scripts: {}\".format(kargs.get(\"fp\"), \"/\".join(args)))\n",
1424 | " \n",
1425 | "f(\"Python\", \"Javascript\", ms = \"C++\", fp = \"Haskell\")"
1426 | ]
1427 | },
1428 | {
1429 | "cell_type": "markdown",
1430 | "metadata": {},
1431 | "source": [
1432 | "#### Lambda"
1433 | ]
1434 | },
1435 | {
1436 | "cell_type": "code",
1437 | "execution_count": 112,
1438 | "metadata": {
1439 | "collapsed": false
1440 | },
1441 | "outputs": [
1442 | {
1443 | "name": "stdout",
1444 | "output_type": "stream",
1445 | "text": [
1446 | "[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]\n",
1447 | "[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n"
1448 | ]
1449 | }
1450 | ],
1451 | "source": [
1452 | "pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n",
1453 | "pairs.sort(key=lambda pair: pair[1])\n",
1454 | "print(pairs)\n",
1455 | "pairs.sort(key=lambda pair: pair[0])\n",
1456 | "print(pairs)"
1457 | ]
1458 | },
1459 | {
1460 | "cell_type": "markdown",
1461 | "metadata": {},
1462 | "source": [
1463 | "#### @decorator"
1464 | ]
1465 | },
1466 | {
1467 | "cell_type": "code",
1468 | "execution_count": 120,
1469 | "metadata": {
1470 | "collapsed": false
1471 | },
1472 | "outputs": [
1473 | {
1474 | "name": "stdout",
1475 | "output_type": "stream",
1476 | "text": [
1477 | "Hey log~\n",
1478 | "This is fa!\n",
1479 | "Bye log~\n",
1480 | "**********\n",
1481 | "Hey log~\n",
1482 | "This is fb!\n",
1483 | "Bye log~\n"
1484 | ]
1485 | }
1486 | ],
1487 | "source": [
1488 | "def log(f):\n",
1489 | " def wrapper():\n",
1490 | " print(\"Hey log~\")\n",
1491 | " f()\n",
1492 | " print(\"Bye log~\")\n",
1493 | " return wrapper\n",
1494 | "\n",
1495 | "@log\n",
1496 | "def fa():\n",
1497 | " print(\"This is fa!\")\n",
1498 | "\n",
1499 | "# Equal to...\n",
1500 | "def fb():\n",
1501 | " print(\"This is fb!\")\n",
1502 | "fb = log(fb)\n",
1503 | "\n",
1504 | "fa()\n",
1505 | "print(\"*\"*10)\n",
1506 | "fb()"
1507 | ]
1508 | },
1509 | {
1510 | "cell_type": "markdown",
1511 | "metadata": {},
1512 | "source": [
1513 | "### V. Class(OOP)"
1514 | ]
1515 | },
1516 | {
1517 | "cell_type": "markdown",
1518 | "metadata": {},
1519 | "source": [
1520 | "### `class`"
1521 | ]
1522 | },
1523 | {
1524 | "cell_type": "code",
1525 | "execution_count": 10,
1526 | "metadata": {
1527 | "collapsed": false
1528 | },
1529 | "outputs": [
1530 | {
1531 | "name": "stdout",
1532 | "output_type": "stream",
1533 | "text": [
1534 | "I can fly!\n",
1535 | "This is an Animal\n"
1536 | ]
1537 | }
1538 | ],
1539 | "source": [
1540 | "class Animal:\n",
1541 | " \"\"\"This is an Animal\"\"\"\n",
1542 | " def fly(_):\n",
1543 | " print(\"I can fly!\")\n",
1544 | "a = Animal()\n",
1545 | "a.fly() # I can fly!\n",
1546 | "print(a.__doc__) # This is an Animal"
1547 | ]
1548 | },
1549 | {
1550 | "cell_type": "markdown",
1551 | "metadata": {},
1552 | "source": [
1553 | "### `__init__` & `self`"
1554 | ]
1555 | },
1556 | {
1557 | "cell_type": "code",
1558 | "execution_count": 15,
1559 | "metadata": {
1560 | "collapsed": false
1561 | },
1562 | "outputs": [
1563 | {
1564 | "name": "stdout",
1565 | "output_type": "stream",
1566 | "text": [
1567 | "Calling __init__() when instantiation!\n",
1568 | "I can not fly!\n",
1569 | "Calling __init__() when instantiation!\n",
1570 | "I CAN fly!\n"
1571 | ]
1572 | }
1573 | ],
1574 | "source": [
1575 | "class Animal:\n",
1576 | " \"\"\"This is an Animal\"\"\"\n",
1577 | " def __init__(self, can_fly = False):\n",
1578 | " print(\"Calling __init__() when instantiation!\")\n",
1579 | " self.can_fly = can_fly\n",
1580 | " def fly(self):\n",
1581 | " if self.can_fly:\n",
1582 | " print(\"I CAN fly!\")\n",
1583 | " else:\n",
1584 | " print(\"I can not fly!\")\n",
1585 | "a = Animal() # Calling __init__() when instantiation!\n",
1586 | "a.fly() # I can not fly!\n",
1587 | "\n",
1588 | "b = Animal(can_fly = True) # Calling __init__() when instantiation!\n",
1589 | "b.fly() # I CAN fly!"
1590 | ]
1591 | },
1592 | {
1593 | "cell_type": "markdown",
1594 | "metadata": {},
1595 | "source": [
1596 | "### Instance"
1597 | ]
1598 | },
1599 | {
1600 | "cell_type": "code",
1601 | "execution_count": 19,
1602 | "metadata": {
1603 | "collapsed": false
1604 | },
1605 | "outputs": [
1606 | {
1607 | "name": "stdout",
1608 | "output_type": "stream",
1609 | "text": [
1610 | "True\n",
1611 | "False\n"
1612 | ]
1613 | }
1614 | ],
1615 | "source": [
1616 | "class Animal:\n",
1617 | " pass\n",
1618 | "class Human:\n",
1619 | " pass\n",
1620 | "a = Animal()\n",
1621 | "h = Human()\n",
1622 | "print(isinstance(a, Animal))\n",
1623 | "print(isinstance(h, Animal))"
1624 | ]
1625 | },
1626 | {
1627 | "cell_type": "markdown",
1628 | "metadata": {},
1629 | "source": [
1630 | "### Inheritance"
1631 | ]
1632 | },
1633 | {
1634 | "cell_type": "code",
1635 | "execution_count": 5,
1636 | "metadata": {
1637 | "collapsed": false
1638 | },
1639 | "outputs": [
1640 | {
1641 | "name": "stdout",
1642 | "output_type": "stream",
1643 | "text": [
1644 | "I can not fly!\n",
1645 | "Woof!\n"
1646 | ]
1647 | }
1648 | ],
1649 | "source": [
1650 | "class Animal:\n",
1651 | " \"\"\"This is an Animal\"\"\"\n",
1652 | " def __init__(self, can_fly = False):\n",
1653 | " self.can_fly = can_fly\n",
1654 | " def fly(self):\n",
1655 | " if self.can_fly:\n",
1656 | " print(\"I CAN fly!\")\n",
1657 | " else:\n",
1658 | " print(\"I can not fly!\")\n",
1659 | "class Dog(Animal):\n",
1660 | " \"\"\"This is a Dog\"\"\"\n",
1661 | " def bark(self):\n",
1662 | " print(\"Woof!\")\n",
1663 | "d = Dog()\n",
1664 | "d.fly()\n",
1665 | "d.bark()"
1666 | ]
1667 | },
1668 | {
1669 | "cell_type": "markdown",
1670 | "metadata": {},
1671 | "source": [
1672 | "### Override"
1673 | ]
1674 | },
1675 | {
1676 | "cell_type": "code",
1677 | "execution_count": 25,
1678 | "metadata": {
1679 | "collapsed": false
1680 | },
1681 | "outputs": [
1682 | {
1683 | "name": "stdout",
1684 | "output_type": "stream",
1685 | "text": [
1686 | "I'm flying high!\n"
1687 | ]
1688 | }
1689 | ],
1690 | "source": [
1691 | "class Animal:\n",
1692 | " \"\"\"This is an Animal\"\"\"\n",
1693 | " def __init__(self, can_fly = False):\n",
1694 | " self.can_fly = can_fly\n",
1695 | " def fly(self):\n",
1696 | " if self.can_fly:\n",
1697 | " print(\"I CAN fly!\")\n",
1698 | " else:\n",
1699 | " print(\"I can not fly!\")\n",
1700 | "class Bird:\n",
1701 | " \"\"\"This is a Bird\"\"\"\n",
1702 | " def fly(self):\n",
1703 | " print(\"I'm flying high!\")\n",
1704 | "bird = Bird()\n",
1705 | "bird.fly() # I'm flying high!"
1706 | ]
1707 | },
1708 | {
1709 | "cell_type": "markdown",
1710 | "metadata": {},
1711 | "source": [
1712 | "### VI. Module"
1713 | ]
1714 | },
1715 | {
1716 | "cell_type": "markdown",
1717 | "metadata": {},
1718 | "source": [
1719 | "#### `import`"
1720 | ]
1721 | },
1722 | {
1723 | "cell_type": "code",
1724 | "execution_count": 37,
1725 | "metadata": {
1726 | "collapsed": false
1727 | },
1728 | "outputs": [
1729 | {
1730 | "name": "stdout",
1731 | "output_type": "stream",
1732 | "text": [
1733 | "posix\n",
1734 | "VERSON: 3.5\n",
1735 | "3.141592653589793\n"
1736 | ]
1737 | }
1738 | ],
1739 | "source": [
1740 | "import os\n",
1741 | "print(os.name)\n",
1742 | "\n",
1743 | "from sys import version_info as PY_VERSION\n",
1744 | "print(\"VERSON: {}.{}\".format(PY_VERSION.major, PY_VERSION.minor))\n",
1745 | "\n",
1746 | "from math import *\n",
1747 | "print(pi)"
1748 | ]
1749 | },
1750 | {
1751 | "cell_type": "markdown",
1752 | "metadata": {},
1753 | "source": [
1754 | "#### Search Path\n",
1755 | "\n",
1756 | "1. current directory\n",
1757 | "2. `echo $PYTHONPATH`\n",
1758 | "3. `sys.path`"
1759 | ]
1760 | },
1761 | {
1762 | "cell_type": "markdown",
1763 | "metadata": {},
1764 | "source": [
1765 | "#### Package"
1766 | ]
1767 | },
1768 | {
1769 | "cell_type": "code",
1770 | "execution_count": 40,
1771 | "metadata": {
1772 | "collapsed": false
1773 | },
1774 | "outputs": [
1775 | {
1776 | "ename": "ImportError",
1777 | "evalue": "No module named 'MyModule.SubModule'",
1778 | "output_type": "error",
1779 | "traceback": [
1780 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1781 | "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
1782 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Running MyModule.SubModuleOne.smo!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \"\"\"\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mMyModule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSubModule\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msmo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0msmo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;31m# Running MyModule.SubModuleOne.smo!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
1783 | "\u001b[0;31mImportError\u001b[0m: No module named 'MyModule.SubModule'"
1784 | ]
1785 | }
1786 | ],
1787 | "source": [
1788 | "\"\"\"\n",
1789 | "MyModule/\n",
1790 | "|--SubModuleOne/\n",
1791 | " |--__init__.py\n",
1792 | " |--smo.py\n",
1793 | "\n",
1794 | "# smo.py\n",
1795 | "def run():\n",
1796 | " print(\"Running MyModule.SubModuleOne.smo!\")\n",
1797 | "\"\"\"\n",
1798 | "from MyModule.SubModule import smo\n",
1799 | "smo.run()\n",
1800 | "# Running MyModule.SubModuleOne.smo!"
1801 | ]
1802 | },
1803 | {
1804 | "cell_type": "markdown",
1805 | "metadata": {},
1806 | "source": [
1807 | "### VII. Pythonic\n",
1808 | "\n",
1809 | "### VIII. Standard Libraries"
1810 | ]
1811 | },
1812 | {
1813 | "cell_type": "code",
1814 | "execution_count": null,
1815 | "metadata": {
1816 | "collapsed": true
1817 | },
1818 | "outputs": [],
1819 | "source": []
1820 | }
1821 | ],
1822 | "metadata": {
1823 | "kernelspec": {
1824 | "display_name": "Python 3",
1825 | "language": "python",
1826 | "name": "python3"
1827 | },
1828 | "language_info": {
1829 | "codemirror_mode": {
1830 | "name": "ipython",
1831 | "version": 3
1832 | },
1833 | "file_extension": ".py",
1834 | "mimetype": "text/x-python",
1835 | "name": "python",
1836 | "nbconvert_exporter": "python",
1837 | "pygments_lexer": "ipython3",
1838 | "version": "3.5.0"
1839 | }
1840 | },
1841 | "nbformat": 4,
1842 | "nbformat_minor": 0
1843 | }
1844 |
--------------------------------------------------------------------------------
/py3 in one pic.mindnode/QuickLook/Preview.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/py3 in one pic.mindnode/QuickLook/Preview.jpg
--------------------------------------------------------------------------------
/py3 in one pic.mindnode/contents.xml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/py3 in one pic.mindnode/contents.xml
--------------------------------------------------------------------------------
/py3 in one pic.mindnode/style.mindnodestyle/contents.xml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/py3 in one pic.mindnode/style.mindnodestyle/contents.xml
--------------------------------------------------------------------------------
/py3 in one pic.mindnode/style.mindnodestyle/metadata.plist:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/py3 in one pic.mindnode/style.mindnodestyle/metadata.plist
--------------------------------------------------------------------------------
/py3 in one pic.mindnode/viewState.plist:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/py3 in one pic.mindnode/viewState.plist
--------------------------------------------------------------------------------
/py3 in one pic.mm:
--------------------------------------------------------------------------------
1 |
480 |
--------------------------------------------------------------------------------
/py3 in one pic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rainyear/python3-in-one-pic/5eba3cafda8945e607ce9f198f01e9ae8c292a74/py3 in one pic.png
--------------------------------------------------------------------------------