Python入门36例
zhangning876
这个作者很懒,什么都没留下…
展开
-
Python入门的36个例子——06 逻辑行与物理行
# 006# 物理行是程序员在写程序的时候看到的一行# 逻辑行是Python的解释器在运行程序是进行处理的最小单位(其实不是最小)# 在一个物理行中写多个逻辑行:i = 33; print i;# 或者这样写(Python真是体贴入微了)j = 44; print j# 把一个逻辑行写道多个物理行中:print/ i# 另一个例子print原创 2009-09-12 01:22:00 · 1128 阅读 · 0 评论 -
Python入门的36个例子——13 Function(def)—— 使功能模块化的好方法
# 013# 设计上的精心是语言达到最大限度的简化# Python的函数定义不需要指定返回类型# 无参数的函数def sayHello(): print Hello! Everyone.# end of defsayHello()# 有参数的函数def sayHelloForCertainTimes(times): for i in rang原创 2009-09-12 01:29:00 · 1762 阅读 · 0 评论 -
Python入门的36个例子——15 默认参数
# 015# 默认参数的本质是:# 不论你是否提供给我这个参数,我都是要用它,# 那么我就要想好在你不向我提供参数的时候我该使用什么。# 或者,这样来理解:# 有一些参数在一般情况下是约定俗成的,# 但,在极少情况下会有一些很有个性的人会打破传统而按照自己的习惯来做事def theFirstDayInAWeek(theDay = Sunday): prin原创 2009-09-12 01:30:00 · 795 阅读 · 0 评论 -
Python入门的36个例子——17 Return
# 017def lifeIsAMirror(): string = raw_input() if string == I love you!: return I love you, too! elif string == Fuck you!: return else: return原创 2009-09-12 01:32:00 · 823 阅读 · 0 评论 -
Python入门的36个例子 之 18 -> Modules —— 将它们放到不同的文件里
例1: # _018# This is a module (if write Chinese in a module, there will be a error)def func1(): print This is function 1.def func2(): print This is function 2.def func3():原创 2009-09-12 14:07:00 · 687 阅读 · 0 评论 -
Python入门的36个例子 之 19 -> List
源代码下载:下载地址在这里 # 022listNum1 = [1, 3]listNum2 = [2, 4]listStr1 = [a, c]listStr2 = [b, d]# 列表的合并list1 = listNum1 + listStr1for ele in list1: print eleprint /n# 判断列原创 2009-09-12 14:13:00 · 811 阅读 · 0 评论 -
Python入门的36个例子 之 19 -> Tuple(元素组)
源代码下载:下载地址在这里 # 023# Tuple(元素组)是不可变的列表tuple1 = (1, 2, 3)print tuple1tuple2 = (tuple1, 4, 5, 6) # 一个元素组可以作为另外一个元素组的元素print tuple2 # 并且能够在存储的时候保持原始的逻辑关系for ele in tuple2: print el原创 2009-09-12 14:17:00 · 777 阅读 · 0 评论 -
Python入门的36个例子 之 21 -> Dictionary —— 通过你的ID找到你
源代码下载:下载地址在这里 # 024dict1 = { 5064001:Mememe, 5064002:tutu, 5064003:thrthr, 5064004:fofo }print dict1[5064003]原创 2009-09-12 14:20:00 · 804 阅读 · 0 评论 -
Python入门的36个例子 之 23 -> Objects and References —— 复制引用还是复制实体对象
源代码下载:下载地址在这里 # 026aList = [1,2,3,4]aListCopy = aList # 其实,这里仅仅复制了一个引用del aList[0]print aListprint aListCopy # 两个引用指向了了同一个对象,所以打印结果一样aListCopy = aList[:] # 这是复制整个对象的有效方法del原创 2009-09-12 14:25:00 · 666 阅读 · 0 评论 -
Python入门的36个例子 之 24 -> 再说说String
# 027toolName = Googleif toolName.startswith(Go): print The tool/s name starts with /"Go/".if oo in toolName: print The tool/s name contains the stirng "oo".print toolNam原创 2009-09-12 14:26:00 · 638 阅读 · 0 评论 -
Python入门的36个例子 之 26 -> File In Console Out
源代码下载:下载地址在这里 # 029aFile = file(rC:/in.txt, r)while True: line = aFile.readline() if len(line) == 0: break # end of if print line,# end of whileaF原创 2009-09-12 14:36:00 · 694 阅读 · 0 评论 -
Python入门的36个例子 之 27 -> Add Something Into A File
源代码下载:下载地址在这里 e.g.1 # 030aFile = file(rC:/temp.txt, a)aFile.write(又添加了一行。)aFile.close() output: e.g.2 # 030aFile = file(rC:/t原创 2009-09-12 14:42:00 · 939 阅读 · 0 评论 -
Python入门的36个例子 之 28 -> Pickle —— 利用文件存储对象
源代码下载:下载地址在这里 # 032# 其实cPickle这个模块起到的作用可以用“完美地协调了文件中的内容(对象)和代码中的引用”来形容import cPickle as p # 这条语句给cPickle起了个小名pobjectFileName = rC:/Data.txtaList = [1, 2, 3]f = file(objectFileName,原创 2009-09-12 14:50:00 · 1400 阅读 · 0 评论 -
Python入门的36个例子 之 29 -> OOP – 创建一个类
源代码下载:下载地址在这里 # 033class Person: age = 22 def sayHello(self): # 方法与函数的区别在于需要一个self参数 print Hello! # end of def# end of class # 这是我良好的编程风格p = Person()print p.age原创 2009-09-12 14:54:00 · 807 阅读 · 0 评论 -
Python入门的36个例子 之 32 -> OOP – Inheritance 继承
源代码下载:下载地址在这里 A Byte Of Python 中关于继承有这样的文字: Suppose you want to write a program which has to keep track of theteachers and students in a college. They have some commoncharacterist原创 2009-09-12 14:58:00 · 831 阅读 · 0 评论 -
Python入门的36个例子 之 33 -> try – 异常是不可避免的,关键是怎样处理
源代码下载:下载地址在这里 # 037fileName = while 1: fileName = raw_input("Input a file name:") if fileName == q: break try: f = file(fileName, r) print Op原创 2009-09-12 15:01:00 · 830 阅读 · 0 评论 -
Python入门的36个例子 之 34 -> Raise An Error – 自定义异常信息
源代码下载:下载地址在这里 raise有两个参数,第一个是由我们自己定义的异常类型,第二个是关于此异常的少量说明信息。 # 038def getAge(): age = input(Input your age:) if (age 160): raise BadAgeError, It is impossible!!!!!原创 2009-09-12 15:02:00 · 1372 阅读 · 0 评论 -
Python入门的36个例子——12 Continue —— 路上的跳板(躲过一劫)
# 012aList = [鲁豫, 灭绝师太, 赵雅芝]for element in aList: print 你想见 + element + 吗? string = raw_input() if string == yes: print 已经为你和 + element + 安排了一次会见 else:原创 2009-09-12 01:28:00 · 1185 阅读 · 0 评论 -
Python入门的36个例子——11 Break —— 终结者
# 011while True: string = raw_input(输入点什么吧/n) if string == quit: break else: print 你输入的是:, string# end of while output:1234567原创 2009-09-12 01:27:00 · 885 阅读 · 0 评论 -
Python入门的36个例子——08 If —— 分叉语句
# 008print 请输入1~5中的任意一个整数inputNum = raw_input()inputNum = int(inputNum)# 还可以写成这样:# inputNum = int(raw_input(请输入1~5中的任意一个整数))if inputNum == 1: print 你输入了1elif inputNum == 2:原创 2009-09-12 01:24:00 · 1004 阅读 · 0 评论 -
Python入门的36个例子——09 While —— 怎么老是你
# 009stillLooping = Truewhile stillLooping: stillLooping = False inputNum = int(raw_input(请输入1~5中的任意一个整数/n)) if inputNum == 1: print 你输入了1 elif inputNum == 2:原创 2009-09-12 01:25:00 · 887 阅读 · 0 评论 -
Python入门的36个例子——10 For —— “计数器”与“遍历器”
# 010# 做为计数器的for# 在类C语言中的样子是: for (i = 0; i < n; i++){}for i in range(1, 11): print ielse: # for 的这个else也是可选的 print for循环结束了/n# end of for# 做为遍历器的for# 相当于C#和Java中的foreac原创 2009-09-12 01:26:00 · 3206 阅读 · 0 评论 -
Python入门的36个例子——14 局部变量与全局变量
# 014# 只能修改函数内部的变量的例子:def powerOnLocal(appleEaten): print 苹果被吃了!, # 注意,这里的逗号是为了不让Python自动换行 appleEaten = True# end of defappleEaten = FalsepowerOnLocal(appleEaten)if appleEaten原创 2009-09-12 01:29:00 · 1511 阅读 · 1 评论 -
Python入门的36个例子之16: 关键参数 —— 嘿!请把这个纸条给小强
# 016def func(firstDayOfWeek = Sunday, favourateFood = apple): print The first day of a week is, firstDayOfWeek print My favourate food is, favourateFood# end of deffunc()fun原创 2009-09-12 01:31:00 · 780 阅读 · 0 评论 -
Python入门的36个例子 之 22 -> Sequences —— 统一的访问方式以及负索引
源代码下载:下载地址在这里 # 025# 序列的神奇之处在于你可以使用相同的方式tuple、list和stringse = [a, b, c, d]li = [a, b, c, d]tu = (a, b, c, d)string = abcdprint se[1]print li[1]print tu[原创 2009-09-12 14:23:00 · 714 阅读 · 0 评论 -
Python入门的36个例子 之 25 -> Console In File Out
源代码下载:下载地址在这里 # 028consoleInput = raw_input(请输入点什么吧:)aFile = file(rC:/out.txt, w)aFile.write(consoleInput + /n)aFile.write(这里是第二行)aFile.close() output: >>> 请输入点什原创 2009-09-12 14:29:00 · 823 阅读 · 0 评论 -
Python入门的36个例子 之 30 -> OOP – 初始化
源代码下载:下载地址在这里 # 034class Person: def __init__(self, name): self.name = name # 这一句中第一个name是类中包含的域,第二个name是传进来的参数 # end of def def sayHello(self): print Hello!原创 2009-09-12 14:56:00 · 741 阅读 · 0 评论 -
Python入门的36个例子 之 31 -> OOP – 属于类的变量和属于对象的变量
源代码下载:下载地址在这里 # 035class Person: population = 0 #这个变量是属于整个类的 def __init__(self, name): self.name = name print 初始化 %s % self.name Person.population += 1原创 2009-09-12 14:57:00 · 743 阅读 · 0 评论 -
Python入门的36个例子 之 36 -> finally
# 040import timetry: f = file(040_Finally.py) while True: line = f.readline() if len(line) == 0: break time.sleep(0.33) print line,原创 2009-09-12 15:05:00 · 915 阅读 · 0 评论 -
Python入门的36个例子——00 源代码下载
下载地址在这里原创 2009-09-12 00:57:00 · 4156 阅读 · 2 评论 -
Python入门的36个例子——01 我见过的最容易实现的“Hello World!”
# 001# Python 有着世界上最简单的实现“Hello World”的语法# 对了,这句话是注释print Hello World!print "Hello World!"print """Hello World!"""print Hello World!print 注意左边的引号print """"注意左边的引号"""原创 2009-09-12 01:00:00 · 1684 阅读 · 0 评论 -
Python入门的36个例子——02 Help! —— 如何获取帮助
# 002# 获取命令的帮助信息时别忘了加上引号help(print)# 对于某种类型的信息不用加引号,因为编译器不可能把它理解成一个方法(函数)help(str) Output:The “print“ statement*********************** print_stmt ::= “print” ([expression原创 2009-09-12 01:02:00 · 1249 阅读 · 0 评论 -
Python入门的36个例子——03 Python 中的数“只有”四种类型
# 003# 得到整数print 1 / 3# 得到浮点数print 1. / 3# 得到“大”整数(可以无限大)print 99999999999999999999 * 99999999999999999999# 得到复数!!!print (-5 + 4j) * (2.3 - 4.6j) output:00.333333333333原创 2009-09-12 01:04:00 · 1157 阅读 · 0 评论 -
Python入门的36个例子——04 优雅的字符串
# 004# 利用三引号( or """)可以指示多行字符串print line1line2line3# 另外,你还可以在三引号中任意使用单引号和双引号print "Whats up? ," he replied.# 否则,你好使用转义符来实现同样的效果# 还是使用三引号好,不然就破坏了视觉美了print /"What/s原创 2009-09-12 01:05:00 · 1138 阅读 · 0 评论 -
Python入门的36个例子——05 聪明的变量
# 005# 在Python中给变量赋值时不需要声明数据类型i = 33print i# 可以这样做的原因是Python把程序中遇到的任何东西都看成是对象(连int也不例外)# 这样,在使用对象时,编译器会根据上下文的环境来调用对象自身的方法完成隐式的转换# 你甚至可以把程序写成这样print 3 * haha # 但若写成这样编译器就会报错(注意3后面的点)原创 2009-09-12 01:21:00 · 989 阅读 · 0 评论 -
Python入门的36个例子——07 运算符和表达式
# 007# 得到商的整数部分print 9 // 4# 得到除法的余数print 9 % 4# 乘幂print 2 ** 10# 布尔运算# 非print not Trueprint not False# 与print True and Falseprint True and True# 或print False or Falsep原创 2009-09-12 01:23:00 · 926 阅读 · 0 评论 -
Python入门的36个例子 之 35 -> try – 一个使用的例子
源代码下载:下载地址在这里 # 039while True: try: x = int(raw_input(Input a number:)) y = int(raw_input(Input a number:)) z = x / y except ValueError, ev: p原创 2009-09-12 15:04:00 · 1046 阅读 · 0 评论