文稿语言编程_第1页
文稿语言编程_第2页
文稿语言编程_第3页
文稿语言编程_第4页
文稿语言编程_第5页
免费预览已结束,剩余23页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、PHP 简介Python编程冯勇 提纲简介语法框架&类库实践Why Python?自从 1954 FORTRAN语言发明至今已有2500多种计算机开发语言存在开发语言历史研究站版本图谱Adrian: “Our response was: With PHP people learn that because they want to get jobs. With Java they learn that because they take computer science courses. With Python you learn it because you love it. Because

2、 you want to experience the beauty. Im sure its the same way for Ruby. If Im hiring a Python programmer the chances are that the person is good.” David: “I totally agree.” Adrian create Ruby on Rails!Why Python?轻易实现快速学习的梦想 深入体会动态语言的妙处 立即体验快速开发的快感 获得快速实现想法的能力Top LanguagePython应用领域桌面GUI软件开发,网络应用开发2/3D

3、图形处理,游戏开发文档处理,Web应用开发科学计算,移动设备应用开发嵌入其它应用开发.可以说除了OS和驱动程序,Python 可以干一切Python Web应用目录简介语法框架&类库实践使用Python平台Linux/BSD/Macwindows命令行直接敲入python进入交互命令行执行.py文件Ex: python hello.pyEx: ./hello.pyhello.py#!/usr/bin/env pythonprint “Hello World”特色缩进以:作为开始,不必使用;作为结束#作为单行注释 作为多行注释动态类型函数式编程(lambda Map Reduce Filter

4、Eval)生成器yield装饰器AOPCallable (class,object,function,Lambda)高级数据结构(list tuple dict)丰富标准库变量表达式3 + 53 + (5 * 4)3 * 2Hello + World变量a = 4 3b = a * 4.5c = (a+b)/2.5a = “Hello World”动态类型,根据运行时决定基本类型Numbers a = 3# Integerb = 4.5# Float pointc = 51728888333L# Long Integerd = 4 + 3j# Complex numberStrings a =

5、 Hello# Single quotesb = “World”# Double quotesc = “Bob said hey there.”# A mix of bothd = A triple qouted stringcan span multiple lineslike thise = “”Also works for double quotes”语法-ListLista = 2, 3, 4 # A list of integerb = 2, 7, 3.5, “Hello” # A mixed listc = # An empty listd = 2, a, b # A list c

6、ontaining a liste = a + b # Join two listsList的操作x = a1 # Get 2nd element (0 is first)y = b1:3 # Return a sub-listz = d102 # Nested listsb0 = 42 # Change an elementTupleTuplesf = (2,3,4,5) # A tuple of integersg = (,) # An empty tupleh = (2, 3,4, (10,11,12) # A tuple containing mixed objectsTuples的操

7、作x = f1 # Element access. x = 3y = f1:3 # Slices. y = (3,4)z = h11 # Nesting. z = 4特色与list类似,最大的不同tuple是只读且不可变化,类xrange不可取代tuple中的任意一元素DictDictionariesa = # An empty dictionaryb = x: 3, y: 4 c = uid: 105, login: beazley, name : David Beazley Dictionaries的存取u = cuid # Get an elementcshell = /bin/sh #

8、 Set an elementif c.has_key(directory): # Check for presence of an memberd = cdirectoryelse:d = Noned = c.get(directory,None) # Same thing, more compactIF-ELSEif-elseif a b:z = belse:z = apass 不做任何事if a = a and b = c:print b is between a and cif not (b c):print b is still between a and cLoopwhilewhi

9、le a b:# Do somethinga = a + 1for (遍历序列的元素)for i in 3, 4, 10, 25:print i# Print characters one at a timefor c in Hello World:print c# Loop over a range of numbersfor i in range(0,100):print iFunctiondef# Return the remainder of a/bdef remainder(a,b):q = a/br = a - q*breturn r# Now use ita = remainde

10、r(42,5) # a = 2返回值def divide(a,b):q = a/br = a - q*breturn q,rx,y = divide(42,5) # x = 8, y = 2Classclassclass Account:def _init_(self, initial):self.balance = initialdef deposit(self, amt):self.balance = self.balance + amtdef withdraw(self,amt):self.balance = self.balance - amtdef getbalance(self):

11、return self.balance使用classa = Account(1000.00)a.deposit(550.23)a.deposit(100)a.withdraw(50)print a.getbalance()异常trytry:f = open(foo)except IOError:print Couldnt open foo. Sorry.raisedef factorial(n):if n 0:raise ValueError,Expected non-negative numberif (n factorial(-1)Traceback (innermost last):Fi

12、le , line 1, in ?File , line 3, in factorialValueError: Expected non-negative numberPackage/Module组织方式# numbers.pydef divide(a,b):q = a/br = a - q*breturn q,rdef gcd(x,y):g = ywhile x 0:g = xx = y % xy = greturn gimportimport numbersx,y = numbers.divide(42,5)n = numbers.gcd(7291823, 5683)目录简介语法类库&框架实践Standard libPython本身就包含了丰富标准库String processingOperating system interf

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论