Python 简介

Python 历史

Python 的创始人为荷兰人吉多·范罗苏姆(Guido van Rossum)。1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ABC 语言的一种继承。之所以选中 Python 作为程序的名字,是因为他是 BBC 电视剧——蒙提·派森的飞行马戏团(Monty Python's Flying Circus)的爱好者。

1991年,第一个 Python 编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。

Python 2.0 于 2000 年 10 月 16 日发布,增加了实现完整的垃圾回收,并且支持 Unicode

Python 3.0 于 2008 年 12 月 3 日发布,此版不完全兼容之前的 Python 源代码。不过,很多新特性后来也被移植到旧的 Python 2.6/2.7 版本。

第一行Python代码

安装好 Python 之后,在命令行下输入:

  1. python

就可以进入 Python 解释器的页面。

按照惯例,第一行代码应该是输出 "hello world!"

In [1]:

  1. print "hello world!"
  1. hello world!

相对与 Java,C 等语言,Python 仅仅使用一行语句就完成的了这个任务。

可以将这句话的内容保存到一个文本文件中,并使用后缀名 .py 结尾,例如 hello_world.py,在命令行下运行这个程序:

  1. python hello_world.py

也会输出 "hello world!" 的结果。

Python 之禅

Python 解释器下输入

import this

会出来这样一首小诗:

In [2]:

  1. import this
  1. The Zen of Python, by Tim Peters
  2.  
  3. Beautiful is better than ugly.
  4. Explicit is better than implicit.
  5. Simple is better than complex.
  6. Complex is better than complicated.
  7. Flat is better than nested.
  8. Sparse is better than dense.
  9. Readability counts.
  10. Special cases aren't special enough to break the rules.
  11. Although practicality beats purity.
  12. Errors should never pass silently.
  13. Unless explicitly silenced.
  14. In the face of ambiguity, refuse the temptation to guess.
  15. There should be one-- and preferably only one --obvious way to do it.
  16. Although that way may not be obvious at first unless you're Dutch.
  17. Now is better than never.
  18. Although never is often better than *right* now.
  19. If the implementation is hard to explain, it's a bad idea.
  20. If the implementation is easy to explain, it may be a good idea.
  21. Namespaces are one honking great idea -- let's do more of those!

这首诗反映了Python的设计哲学——Python是一种追求优雅,明确,简单的编程语言,但事实上,产生这首诗的代码并没有写的那么简单易懂:

In [3]:

  1. s = """Gur Mra bs Clguba, ol Gvz Crgref
  2.  
  3. Ornhgvshy vf orggre guna htyl.
  4. Rkcyvpvg vf orggre guna vzcyvpvg.
  5. Fvzcyr vf orggre guna pbzcyrk.
  6. Pbzcyrk vf orggre guna pbzcyvpngrq.
  7. Syng vf orggre guna arfgrq.
  8. Fcnefr vf orggre guna qrafr.
  9. Ernqnovyvgl pbhagf.
  10. Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
  11. Nygubhtu cenpgvpnyvgl orngf chevgl.
  12. Reebef fubhyq arire cnff fvyragyl.
  13. Hayrff rkcyvpvgyl fvyraprq.
  14. Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
  15. Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
  16. Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
  17. Abj vf orggre guna arire.
  18. Nygubhtu arire vf bsgra orggre guna *evtug* abj.
  19. Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
  20. Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
  21. Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
  22.  
  23. d = {}
  24. for c in (65, 97):
  25. for i in range(26):
  26. d[chr(i+c)] = chr((i+13) % 26 + c)
  27.  
  28. print "".join([d.get(c, c) for c in s])
  1. The Zen of Python, by Tim Peters
  2.  
  3. Beautiful is better than ugly.
  4. Explicit is better than implicit.
  5. Simple is better than complex.
  6. Complex is better than complicated.
  7. Flat is better than nested.
  8. Sparse is better than dense.
  9. Readability counts.
  10. Special cases aren't special enough to break the rules.
  11. Although practicality beats purity.
  12. Errors should never pass silently.
  13. Unless explicitly silenced.
  14. In the face of ambiguity, refuse the temptation to guess.
  15. There should be one-- and preferably only one --obvious way to do it.
  16. Although that way may not be obvious at first unless you're Dutch.
  17. Now is better than never.
  18. Although never is often better than *right* now.
  19. If the implementation is hard to explain, it's a bad idea.
  20. If the implementation is easy to explain, it may be a good idea.
  21. Namespaces are one honking great idea -- let's do more of those!
Life is short. Use Python.

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/01-python-tools/01.01-python-overview.ipynb