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

Más contenido relacionado

Similar a Python分享

GTLAB Installation Tutorial for SciDAC 2009
GTLAB Installation Tutorial for SciDAC 2009GTLAB Installation Tutorial for SciDAC 2009
GTLAB Installation Tutorial for SciDAC 2009
marpierc
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
Lei Kang
 

Similar a Python分享 (20)

Client-side JavaScript Vulnerabilities
Client-side JavaScript VulnerabilitiesClient-side JavaScript Vulnerabilities
Client-side JavaScript Vulnerabilities
 
Even Faster Web Sites at jQuery Conference '09
Even Faster Web Sites at jQuery Conference '09Even Faster Web Sites at jQuery Conference '09
Even Faster Web Sites at jQuery Conference '09
 
&lt;img src="xss.com">
&lt;img src="xss.com">&lt;img src="xss.com">
&lt;img src="xss.com">
 
Fav
FavFav
Fav
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Fast by Default
Fast by DefaultFast by Default
Fast by Default
 
Enterprise AIR Development for JavaScript Developers
Enterprise AIR Development for JavaScript DevelopersEnterprise AIR Development for JavaScript Developers
Enterprise AIR Development for JavaScript Developers
 
Accelerated Adoption: HTML5 and CSS3 for ASP.NET Developers
Accelerated Adoption: HTML5 and CSS3 for ASP.NET DevelopersAccelerated Adoption: HTML5 and CSS3 for ASP.NET Developers
Accelerated Adoption: HTML5 and CSS3 for ASP.NET Developers
 
GTLAB Installation Tutorial for SciDAC 2009
GTLAB Installation Tutorial for SciDAC 2009GTLAB Installation Tutorial for SciDAC 2009
GTLAB Installation Tutorial for SciDAC 2009
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHP
 
Teflon - Anti Stick for the browser attack surface
Teflon - Anti Stick for the browser attack surfaceTeflon - Anti Stick for the browser attack surface
Teflon - Anti Stick for the browser attack surface
 
Testing of javacript
Testing of javacriptTesting of javacript
Testing of javacript
 
Jquery 1
Jquery 1Jquery 1
Jquery 1
 
Python and MongoDB
Python and MongoDBPython and MongoDB
Python and MongoDB
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
 
Developing Gadgets
Developing GadgetsDeveloping Gadgets
Developing Gadgets
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
OSGi with the Spring Framework
OSGi with the Spring FrameworkOSGi with the Spring Framework
OSGi with the Spring Framework
 
Grails and Dojo
Grails and DojoGrails and Dojo
Grails and Dojo
 
The Basics Of Page Creation
The Basics Of Page CreationThe Basics Of Page Creation
The Basics Of Page Creation
 

Más de fangdeng

jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构
fangdeng
 
Building an event driven web
Building an event driven webBuilding an event driven web
Building an event driven web
fangdeng
 
浅尝jQuery
浅尝jQuery浅尝jQuery
浅尝jQuery
fangdeng
 
Html基础培训
Html基础培训Html基础培训
Html基础培训
fangdeng
 
前端开发之Js
前端开发之Js前端开发之Js
前端开发之Js
fangdeng
 
Javascript代码注释及文档生成
Javascript代码注释及文档生成Javascript代码注释及文档生成
Javascript代码注释及文档生成
fangdeng
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
fangdeng
 
前端单元测试初体验
前端单元测试初体验前端单元测试初体验
前端单元测试初体验
fangdeng
 
Java script测试之js unit ut
Java script测试之js unit utJava script测试之js unit ut
Java script测试之js unit ut
fangdeng
 
2011年方凳年度总结及颁奖
2011年方凳年度总结及颁奖2011年方凳年度总结及颁奖
2011年方凳年度总结及颁奖
fangdeng
 
产品线中的思考
产品线中的思考产品线中的思考
产品线中的思考
fangdeng
 
产品线中的思考
产品线中的思考产品线中的思考
产品线中的思考
fangdeng
 
Postoffer前端架构设计
Postoffer前端架构设计Postoffer前端架构设计
Postoffer前端架构设计
fangdeng
 
Varnish简介
Varnish简介Varnish简介
Varnish简介
fangdeng
 
Let's talk about date in javascript
Let's talk about  date  in javascriptLet's talk about  date  in javascript
Let's talk about date in javascript
fangdeng
 
Test driven-frontend-develop
Test driven-frontend-developTest driven-frontend-develop
Test driven-frontend-develop
fangdeng
 
方凳良品1期
方凳良品1期方凳良品1期
方凳良品1期
fangdeng
 
方凳良品2期
方凳良品2期方凳良品2期
方凳良品2期
fangdeng
 
魏琪君-重构-关于可读性、原则和模式
魏琪君-重构-关于可读性、原则和模式魏琪君-重构-关于可读性、原则和模式
魏琪君-重构-关于可读性、原则和模式
fangdeng
 

Más de fangdeng (20)

jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构
 
Building an event driven web
Building an event driven webBuilding an event driven web
Building an event driven web
 
浅尝jQuery
浅尝jQuery浅尝jQuery
浅尝jQuery
 
Html基础培训
Html基础培训Html基础培训
Html基础培训
 
前端开发之Js
前端开发之Js前端开发之Js
前端开发之Js
 
Javascript代码注释及文档生成
Javascript代码注释及文档生成Javascript代码注释及文档生成
Javascript代码注释及文档生成
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
 
前端单元测试初体验
前端单元测试初体验前端单元测试初体验
前端单元测试初体验
 
Java script测试之js unit ut
Java script测试之js unit utJava script测试之js unit ut
Java script测试之js unit ut
 
2011年方凳年度总结及颁奖
2011年方凳年度总结及颁奖2011年方凳年度总结及颁奖
2011年方凳年度总结及颁奖
 
产品线中的思考
产品线中的思考产品线中的思考
产品线中的思考
 
产品线中的思考
产品线中的思考产品线中的思考
产品线中的思考
 
Postoffer前端架构设计
Postoffer前端架构设计Postoffer前端架构设计
Postoffer前端架构设计
 
Varnish简介
Varnish简介Varnish简介
Varnish简介
 
Websocket
WebsocketWebsocket
Websocket
 
Let's talk about date in javascript
Let's talk about  date  in javascriptLet's talk about  date  in javascript
Let's talk about date in javascript
 
Test driven-frontend-develop
Test driven-frontend-developTest driven-frontend-develop
Test driven-frontend-develop
 
方凳良品1期
方凳良品1期方凳良品1期
方凳良品1期
 
方凳良品2期
方凳良品2期方凳良品2期
方凳良品2期
 
魏琪君-重构-关于可读性、原则和模式
魏琪君-重构-关于可读性、原则和模式魏琪君-重构-关于可读性、原则和模式
魏琪君-重构-关于可读性、原则和模式
 

Último

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Python分享

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