|
| 1 | +## Know them all ! |
| 2 | +> Just when you thought, Python could not be more fun. |
| 3 | +
|
| 4 | +### 1. Hello World |
| 5 | + |
| 6 | +```py |
| 7 | +>>> import __hello__ |
| 8 | +Hello World! |
| 9 | +``` |
| 10 | + |
| 11 | +### 2. The classic |
| 12 | +``` |
| 13 | +>>> import this |
| 14 | +
|
| 15 | +The Zen of Python, by Tim Peters |
| 16 | +
|
| 17 | +Beautiful is better than ugly. |
| 18 | +Explicit is better than implicit. |
| 19 | +Simple is better than complex. |
| 20 | +Complex is better than complicated. |
| 21 | +Flat is better than nested. |
| 22 | +Sparse is better than dense. |
| 23 | +Readability counts. |
| 24 | +Special cases aren't special enough to break the rules. |
| 25 | +Although practicality beats purity. |
| 26 | +Errors should never pass silently. |
| 27 | +Unless explicitly silenced. |
| 28 | +In the face of ambiguity, refuse the temptation to guess. |
| 29 | +There should be one-- and preferably only one --obvious way to do it. |
| 30 | +Although that way may not be obvious at first unless you're Dutch. |
| 31 | +Now is better than never. |
| 32 | +Although never is often better than *right* now. |
| 33 | +If the implementation is hard to explain, it's a bad idea. |
| 34 | +If the implementation is easy to explain, it may be a good idea. |
| 35 | +Namespaces are one honking great idea -- let's do more of those! |
| 36 | +``` |
| 37 | + |
| 38 | +### 3. The missing line from the classic |
| 39 | + |
| 40 | +The Zen of Python was introduced in [PEP 20](https://www.python.org/dev/peps/pep-0020/#id2). It is supposed to be 20 aphorisms, but only 19 of which have been written down. |
| 41 | + |
| 42 | + |
| 43 | +### 4. A simple life lesson |
| 44 | +``` |
| 45 | +>>> import this |
| 46 | +... |
| 47 | +>>> love = this |
| 48 | +>>> this is love |
| 49 | +True |
| 50 | +>>> love is True |
| 51 | +False |
| 52 | +>>> love is False |
| 53 | +False |
| 54 | +>>> love is not True or False |
| 55 | +True |
| 56 | +>>> love is not True or False; love is love # FML |
| 57 | +True |
| 58 | +``` |
| 59 | + |
| 60 | +### 5. Comics, yeah. |
| 61 | +```py |
| 62 | +>>> import antigravity |
| 63 | +``` |
| 64 | + |
| 65 | +### 6. It's not a choice, it defines who we are |
| 66 | +```py |
| 67 | +>>> from __future__ import braces |
| 68 | + File "<stdin>", line 1 |
| 69 | +SyntaxError: not a chance |
| 70 | +``` |
| 71 | + |
| 72 | +### 7. Origins |
| 73 | +The name Python has nothing to do with the type of Snake. |
| 74 | + |
| 75 | +### 8. The confuscation |
| 76 | +This is how the `this.py` module looks, which prints the Zen of Python. |
| 77 | + |
| 78 | +``` |
| 79 | +s = """Gur Mra bs Clguba, ol Gvz Crgref |
| 80 | +
|
| 81 | +Ornhgvshy vf orggre guna htyl. |
| 82 | +Rkcyvpvg vf orggre guna vzcyvpvg. |
| 83 | +Fvzcyr vf orggre guna pbzcyrk. |
| 84 | +Pbzcyrk vf orggre guna pbzcyvpngrq. |
| 85 | +Syng vf orggre guna arfgrq. |
| 86 | +Fcnefr vf orggre guna qrafr. |
| 87 | +Ernqnovyvgl pbhagf. |
| 88 | +Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf. |
| 89 | +Nygubhtu cenpgvpnyvgl orngf chevgl. |
| 90 | +Reebef fubhyq arire cnff fvyragyl. |
| 91 | +Hayrff rkcyvpvgyl fvyraprq. |
| 92 | +Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff. |
| 93 | +Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg. |
| 94 | +Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu. |
| 95 | +Abj vf orggre guna arire. |
| 96 | +Nygubhtu arire vf bsgra orggre guna *evtug* abj. |
| 97 | +Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn. |
| 98 | +Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn. |
| 99 | +Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!""" |
| 100 | +
|
| 101 | +d = {} |
| 102 | +for c in (65, 97): |
| 103 | + for i in range(26): |
| 104 | + d[chr(i+c)] = chr((i+13) % 26 + c) |
| 105 | +
|
| 106 | +print("".join([d.get(c, c) for c in s])) |
| 107 | +``` |
| 108 | + |
| 109 | +The code for the Zen violates itself. It's not beautiful but ugly, not explicit but implicit. |
| 110 | +This would probably be the *only* module to go against the spirit of what it says itself. |
| 111 | +. |
| 112 | + |
| 113 | +### 9. C/C++ anyone? |
| 114 | +From the Zen again, |
| 115 | +``` |
| 116 | +There should be one-- and preferably only one --obvious way to do it. |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +## Notes |
| 122 | +1. Easiest hello world program in a language without calling any function |
| 123 | +2. Each and every line is the philosophy of Python's design and is a supreme holy guide |
| 124 | +3. Maybe just to show that there always should be a new line at the end of a file! |
| 125 | +4. Not an easter egg, a joke in the interpreter |
| 126 | +5. It opens this [xkcd comic](https://xkcd.com/353) which demonstrates how easy it is to do stuff with modules |
| 127 | +6. This is to instantly close down any conversation about introducing curly braces to Python |
| 128 | +7. Guido van Rossum is a big fan of [Monty Python's Flying Circus](https://en.wikipedia.org/wiki/Monty_Python%27s_Flying_Circus) |
| 129 | +8. It's a substitution cipher called [ROT13](https://en.wikipedia.org/wiki/ROT13) |
| 130 | +9. In many languages there are two ways to do the same thing `--no` and `no--`. The message has a hidden example in itself |
| 131 | + |
| 132 | +## |
0 commit comments