Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python分享

658 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Python分享

  1. 1. 从JavaScript到Python<br />yingjie.wuyj@alibaba-inc.com<br />
  2. 2. Content<br />什么是Python?<br />与众不同之处<br />一点点语法<br />好玩的例子<br />Pythonic<br />
  3. 3. Content<br />什么是Python?<br />与众不同之处<br />一点点语法<br />好玩的例子<br />Pythonic<br />
  4. 4. 什么是Python?<br />Python语言是在1989年由 Guido van Rossum开发的<br />免费、开源、跨平台<br />全球10大编程语言之一<br />
  5. 5. 什么是Python?<br />Python特性<br />易用性<br />免费、开源<br />跨平台<br />动态<br />面向对象<br />易于扩展<br />
  6. 6. 什么是Python?<br />Python可以做什么<br />计算器<br />处理文本<br />网络编程<br />数据库编程<br />科学计算<br />多媒体处理<br />图形界面<br />Windows编程<br />…<br />
  7. 7. 什么是Python?<br />If programming languages are cars..<br />Python is a great beginner’s car; you can drive it without a license. Unless you want to drive really fast or on really treacherous terrain, you may never need another car.<br />
  8. 8. 排名<br />
  9. 9. 谁在用Python?<br />
  10. 10. Content<br />什么是Python?<br />与众不同之处<br />一点点语法<br />好玩的例子<br />Pythonic<br />
  11. 11. 与众不同之处<br />排版<br />几个例子<br />奇技淫巧<br />
  12. 12. 与众不同之处<br />排版格式,缩进有语法意义<br />变量不需要定义<br />
  13. 13. 交换变量的值<br />JavaScript<br />var a = 1, b = 2;<br />vartmp = a;<br />a = b;<br />b = tmp;<br />Python<br />a = 1<br />b = 2<br />a, b = b, a<br />
  14. 14. 判断数字范围<br />JavaScript<br />if (a > 3 && a < 6) { … }<br />Python<br />if 3 < a < 6: …<br />
  15. 15. 计算阶乘 n!<br />JavaScript<br />function factorial(n) {<br /> return n != 1 ? n * factorial(n - 1) : 1;<br />}<br />factorial(n);<br />Python<br />reduce(lambda x, y: x * y, range(1, n + 1))<br />
  16. 16. 找出一个数组中的所有偶数<br />JavaScript<br />var a = [12, 5, 2, 60, 55, 6, 33, 9], b = [];<br />for (vari = 0; i < a.length; i ++) {<br /> if (a[i] % 2 == 0)<br />b.push(a[i]);<br />}<br />Python<br />a = [12, 5, 2, 60, 55, 6, 33, 9]<br />b = [i for i in a if i % 2 == 0]<br />
  17. 17. 大数处理<br />计算2的10000次方<br />>>> 2 ** 10000<br />19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376<br />共3011位<br />
  18. 18. 一个函数返回多个值<br />def func():<br />......<br />return val_1, val_2<br />a, b = func()<br />
  19. 19. 计算一个文本文件中最长的一行有多长<br />max(len(x.strip()) for x in open('test.txt'))<br />
  20. 20. Content<br />什么是Python?<br />与众不同之处<br />一点点语法<br />好玩的例子<br />Pythonic<br />
  21. 21. 一点点语法<br />变量<br />语句<br />函数<br />
  22. 22. 一点点语法<br />变量<br />数字<br />字符串<br />列表和元组(相当于JS中的Array)<br />字典(相当于JS中的Object)<br />集合<br />
  23. 23. 一点点语法<br />变量<br />数字<br />实数<br />整数<br />10000位的大整数<br />2 的 10000 次方<br />浮点小数<br />虚数<br />3 + 4j<br />
  24. 24. 一点点语法<br />变量<br />字符串<br />字符串不可改变<br />具有类似数组的功能<br />s = “abcdefg”<br />s[0] “a”<br />
  25. 25. 一点点语法<br />变量<br />列表和元组(相当于JS中的Array)<br />列表<br />[1, 2, 3, “a”, “bbc”]<br />[3.14, [1, 3, 5], “aa”]<br />元组<br />(1, 2, 3, “a”, “bbc”)<br />元组不可改变<br />
  26. 26. 一点点语法<br />变量<br />字典(相当于JS中的Object)<br />dic = {<br /> “a”: 1,<br /> “b”: 2,<br /> “c”: 3,<br />}<br />
  27. 27. 一点点语法<br />变量<br />集合<br />介于元组和字典之间<br />>>> s = set()<br />>>> s.add("a")<br />>>> s.add("b")<br />>>> s.add("c")<br />>>> s.add("a")<br />>>> s<br />set(['a', 'c', 'b'])<br />
  28. 28. 一点点语法<br />语句<br />if<br />for<br />while<br />
  29. 29. 一点点语法<br />语句<br />没有“?:”三元操作符<br />
  30. 30. 一点点语法<br />函数<br />def<br />lambda<br />闭包<br />
  31. 31. 一点点语法<br />函数<br />def<br />>>> def f(a):<br />... b = a * 2<br />... return b<br />...<br />>>> f(3)<br />6<br />
  32. 32. 一点点语法<br />函数<br />lambda<br />匿名函数<br />>>> f = lambda x: x * 2<br />>>> f(3)<br />6<br />
  33. 33. 一点点语法<br />函数<br />闭包<br />>>> def f(a):<br />... b = a * 2<br />... def g():<br />... c = b * 2<br />... return c<br />... return g()<br />...<br />>>> f(3)<br />12<br />
  34. 34. 一点点语法<br />函数<br />闭包<br />>>> def f(a):<br />... b = a * 2<br />... def g():<br />... b = b * 2<br />... return b<br />... return g()<br />...<br />>>> f(3)<br />Traceback (most recent call last):<br /> File "<stdin>", line 1, in <module><br /> File "<stdin>", line 6, in f<br /> File "<stdin>", line 4, in g<br />UnboundLocalError: local variable 'b' referenced before assignment<br />
  35. 35. Content<br />什么是Python?<br />与众不同之处<br />一点点语法<br />好玩的例子<br />Pythonic<br />
  36. 36. 好玩的例子<br />小爬虫<br />图片批量下载<br />树<br />
  37. 37. 好玩的例子<br />小爬虫<br />下载指定页面的HTML<br />>>> import urllib<br />>>> u = urllib.urlopen("http://china.alibaba.com/")<br />>>> html = u.read()<br />>>> print html<br /><!doctype html public "-//w3c//dtdxhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /> <head><br /> <title>阿里巴巴是全球领先的B2B电子商务网上贸易平台</title><br /> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/><br /> <meta http-equiv="X-UA-Compatible" content="IE=7" /><br /><meta name="verify-v1" content="fY3J3JK21Xt98skDWznPEIQE+Vle91rbRHkvUk07/WE=" /><br /> <meta name="description" content=...<br />>>> u.close()<br />
  38. 38. 好玩的例子<br />图片批量下载<br />import urllib<br />import re<br />import os<br />u = urllib.urlopen("http://china.alibaba.com/")<br />html = u.read()<br />u.close()<br />imgs = re.findall("http://[^s"]*?.(?:jpg|gif|png)",<br /> html, re.I)<br />for img_src in imgs:<br /> fn = os.path.split(img_src)[1]<br />img_bin = urllib.urlopen(img_src).read()<br /> f = open(fn, “wb”)<br />f.wirte(img_bin)<br />f.close()<br />
  39. 39. 好玩的例子<br />树<br />
  40. 40. Content<br />什么是Python?<br />与众不同之处<br />一点点语法<br />好玩的例子<br />Pythonic<br />
  41. 41. Pythonic<br />Python is powerful... and fast<br />Python plays well with others<br />Python runs everywhere<br />Python is friendly... and easy to learn<br />Python is Open<br />
  42. 42. Pythonic<br />>>> import this<br />The Zen of Python, by Tim Peters<br />Beautiful is better than ugly.<br />Explicit is better than implicit.<br />Simple is better than complex.<br />Complex is better than complicated.<br />Flat is better than nested.<br />Sparse is better than dense.<br />Readability counts.<br />Special cases aren't special enough to break the rules.<br />Although practicality beats purity.<br />Errors should never pass silently.<br />Unless explicitly silenced.<br />In the face of ambiguity, refuse the temptation to guess.<br />There should be one-- and preferably only one --obvious way to do it.<br />Although that way may not be obvious at first unless you're Dutch.<br />Now is better than never.<br />Although never is often better than *right* now.<br />If the implementation is hard to explain, it's a bad idea.<br />If the implementation is easy to explain, it may be a good idea.<br />Namespaces are one honking great idea -- let's do more of those!<br />
  43. 43. Tnanks!<br />:-)<br />

×