|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "collapsed": true |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Python 简介" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## **Python** 历史" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "`Python` 的创始人为荷兰人吉多·范罗苏姆(`Guido van Rossum`)。1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ABC 语言的一种继承。之所以选中 `Python` 作为程序的名字,是因为他是 BBC 电视剧——蒙提·派森的飞行马戏团(`Monty Python's Flying Circus`)的爱好者。\n", |
| 24 | + "\n", |
| 25 | + "1991年,第一个 Python 编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。\n", |
| 26 | + "\n", |
| 27 | + "`Python 2.0` 于 2000 年 10 月 16 日发布,增加了实现完整的垃圾回收,并且支持 `Unicode`。\n", |
| 28 | + "\n", |
| 29 | + "`Python 3.0` 于 2008 年 12 月 3 日发布,此版不完全兼容之前的 `Python` 源代码。不过,很多新特性后来也被移植到旧的 `Python 2.6/2.7` 版本。" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "## 第一行Python代码" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "安装好 `Python` 之后,在命令行下输入:\n", |
| 44 | + "\n", |
| 45 | + " python\n", |
| 46 | + "\n", |
| 47 | + "就可以进入 `Python` 解释器的页面。\n", |
| 48 | + "\n", |
| 49 | + "按照惯例,第一行代码应该是输出 `\"hello world!\"`:" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 1, |
| 55 | + "metadata": { |
| 56 | + "collapsed": false |
| 57 | + }, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "name": "stdout", |
| 61 | + "output_type": "stream", |
| 62 | + "text": [ |
| 63 | + "hello world!\n" |
| 64 | + ] |
| 65 | + } |
| 66 | + ], |
| 67 | + "source": [ |
| 68 | + "print \"hello world!\"" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "相对与 `Java,C` 等语言,`Python` 仅仅使用一行语句就完成的了这个任务。\n", |
| 76 | + "\n", |
| 77 | + "可以将这句话的内容保存到一个文本文件中,并使用后缀名 `.py` 结尾,例如 `hello_world.py`,在命令行下运行这个程序:\n", |
| 78 | + "\n", |
| 79 | + " python hello_world.py\n", |
| 80 | + "\n", |
| 81 | + "也会输出 `\"hello world!\"` 的结果。" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "## Python 之禅" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "在 **Python** 解释器下输入 \n", |
| 96 | + "\n", |
| 97 | + "```import this```\n", |
| 98 | + "\n", |
| 99 | + "会出来这样一首小诗:" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 2, |
| 105 | + "metadata": { |
| 106 | + "collapsed": false |
| 107 | + }, |
| 108 | + "outputs": [ |
| 109 | + { |
| 110 | + "name": "stdout", |
| 111 | + "output_type": "stream", |
| 112 | + "text": [ |
| 113 | + "The Zen of Python, by Tim Peters\n", |
| 114 | + "\n", |
| 115 | + "Beautiful is better than ugly.\n", |
| 116 | + "Explicit is better than implicit.\n", |
| 117 | + "Simple is better than complex.\n", |
| 118 | + "Complex is better than complicated.\n", |
| 119 | + "Flat is better than nested.\n", |
| 120 | + "Sparse is better than dense.\n", |
| 121 | + "Readability counts.\n", |
| 122 | + "Special cases aren't special enough to break the rules.\n", |
| 123 | + "Although practicality beats purity.\n", |
| 124 | + "Errors should never pass silently.\n", |
| 125 | + "Unless explicitly silenced.\n", |
| 126 | + "In the face of ambiguity, refuse the temptation to guess.\n", |
| 127 | + "There should be one-- and preferably only one --obvious way to do it.\n", |
| 128 | + "Although that way may not be obvious at first unless you're Dutch.\n", |
| 129 | + "Now is better than never.\n", |
| 130 | + "Although never is often better than *right* now.\n", |
| 131 | + "If the implementation is hard to explain, it's a bad idea.\n", |
| 132 | + "If the implementation is easy to explain, it may be a good idea.\n", |
| 133 | + "Namespaces are one honking great idea -- let's do more of those!\n" |
| 134 | + ] |
| 135 | + } |
| 136 | + ], |
| 137 | + "source": [ |
| 138 | + "import this" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "这首诗反映了**Python**的设计哲学——**Python**是一种追求优雅,明确,简单的编程语言,但事实上,产生这首诗的代码并没有写的那么简单易懂:" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": 3, |
| 151 | + "metadata": { |
| 152 | + "collapsed": false |
| 153 | + }, |
| 154 | + "outputs": [ |
| 155 | + { |
| 156 | + "name": "stdout", |
| 157 | + "output_type": "stream", |
| 158 | + "text": [ |
| 159 | + "The Zen of Python, by Tim Peters\n", |
| 160 | + "\n", |
| 161 | + "Beautiful is better than ugly.\n", |
| 162 | + "Explicit is better than implicit.\n", |
| 163 | + "Simple is better than complex.\n", |
| 164 | + "Complex is better than complicated.\n", |
| 165 | + "Flat is better than nested.\n", |
| 166 | + "Sparse is better than dense.\n", |
| 167 | + "Readability counts.\n", |
| 168 | + "Special cases aren't special enough to break the rules.\n", |
| 169 | + "Although practicality beats purity.\n", |
| 170 | + "Errors should never pass silently.\n", |
| 171 | + "Unless explicitly silenced.\n", |
| 172 | + "In the face of ambiguity, refuse the temptation to guess.\n", |
| 173 | + "There should be one-- and preferably only one --obvious way to do it.\n", |
| 174 | + "Although that way may not be obvious at first unless you're Dutch.\n", |
| 175 | + "Now is better than never.\n", |
| 176 | + "Although never is often better than *right* now.\n", |
| 177 | + "If the implementation is hard to explain, it's a bad idea.\n", |
| 178 | + "If the implementation is easy to explain, it may be a good idea.\n", |
| 179 | + "Namespaces are one honking great idea -- let's do more of those!\n" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "s = \"\"\"Gur Mra bs Clguba, ol Gvz Crgref\n", |
| 185 | + "\n", |
| 186 | + "Ornhgvshy vf orggre guna htyl.\n", |
| 187 | + "Rkcyvpvg vf orggre guna vzcyvpvg.\n", |
| 188 | + "Fvzcyr vf orggre guna pbzcyrk.\n", |
| 189 | + "Pbzcyrk vf orggre guna pbzcyvpngrq.\n", |
| 190 | + "Syng vf orggre guna arfgrq.\n", |
| 191 | + "Fcnefr vf orggre guna qrafr.\n", |
| 192 | + "Ernqnovyvgl pbhagf.\n", |
| 193 | + "Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.\n", |
| 194 | + "Nygubhtu cenpgvpnyvgl orngf chevgl.\n", |
| 195 | + "Reebef fubhyq arire cnff fvyragyl.\n", |
| 196 | + "Hayrff rkcyvpvgyl fvyraprq.\n", |
| 197 | + "Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.\n", |
| 198 | + "Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.\n", |
| 199 | + "Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.\n", |
| 200 | + "Abj vf orggre guna arire.\n", |
| 201 | + "Nygubhtu arire vf bsgra orggre guna *evtug* abj.\n", |
| 202 | + "Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.\n", |
| 203 | + "Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.\n", |
| 204 | + "Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!\"\"\"\n", |
| 205 | + "\n", |
| 206 | + "d = {}\n", |
| 207 | + "for c in (65, 97):\n", |
| 208 | + " for i in range(26):\n", |
| 209 | + " d[chr(i+c)] = chr((i+13) % 26 + c)\n", |
| 210 | + "\n", |
| 211 | + "print \"\".join([d.get(c, c) for c in s])" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "markdown", |
| 216 | + "metadata": {}, |
| 217 | + "source": [ |
| 218 | + "> Life is short. Use Python." |
| 219 | + ] |
| 220 | + } |
| 221 | + ], |
| 222 | + "metadata": { |
| 223 | + "kernelspec": { |
| 224 | + "display_name": "Python 2", |
| 225 | + "language": "python", |
| 226 | + "name": "python2" |
| 227 | + }, |
| 228 | + "language_info": { |
| 229 | + "codemirror_mode": { |
| 230 | + "name": "ipython", |
| 231 | + "version": 2 |
| 232 | + }, |
| 233 | + "file_extension": ".py", |
| 234 | + "mimetype": "text/x-python", |
| 235 | + "name": "python", |
| 236 | + "nbconvert_exporter": "python", |
| 237 | + "pygments_lexer": "ipython2", |
| 238 | + "version": "2.7.6" |
| 239 | + } |
| 240 | + }, |
| 241 | + "nbformat": 4, |
| 242 | + "nbformat_minor": 0 |
| 243 | +} |
0 commit comments