b97902HW 板


LINE

相信有上今天的计概的各位单班同学都开始接触 Python 了,TA 在 lab 快速 view 过 一堆语法和 sample code ,如果有当场记起来的那真的是非常厉害,不过搞不太清楚的同 学也不要太担心,我今天找到一份还算可以的入门放在这里,除了课程网上公布的 slide 以外,也可以参考一下这份教学,其实语法和 C 比起来应该是和善多了啦XD 用相同的观 念应该很容易可以把作业解决的。 另外建议大家可以开始养成查官方 library & document 的习惯了,这对学习一种新的语 言有很大的帮助。 The Python Standard Library http://docs.python.org/library/ Python Libray Reference http://www.python.org/doc/2.5.2/lib/lib.html Python Taiwan http://www.python.tw/ Python Tutorial Chinese http://www.freebsd.org.hk/html/python/tut_tw/tut.html Origin http://www.python.org/doc/2.5.2/tut/tut.html ------------------------------------------------------------------------------ 使用Python * 有两种主要使用python的方法 o 使用互动式命令列 + e.q. 直接键入python就会进入python的互动式命令列 o 将程式写成档案,再由python执行 + 直在将程式码写在档案内,然後再执行python去读取该档案 # Ex: python hello.py + 或是在档案的第一个行写着 #!/usr/bin/env python,然後在第二行\ 之後输入程式码,如此可以直接执行该档案 # Ex: ./hello.py * 作业平台 o Linux、FreeBSD … o Windows 第一个python程式 - Hello World * 使用互动式命令列 >>> print “Hello World” >>> * 放在档案里 #!/usr/bin/env python print “Hello World” 基本概念 * 语法特色 o 以冒号(:)做为叙述的开始 o 不必使用分号(;)做为结尾 o 井字号(#)做为注解符号,同行井字号後的任何字将被忽略 o 使用tab键做为缩排区块的依据 o 不必指定变数型态 (runtime时才会进行binding) 变数(Variables)和 表示式(Expressions) * 表示式 3 + 5 3 + (5 * 4) 3 ** 2 ‘Hello’ + ‘World’ * 变数指定 a = 4 << 3 b = a * 4.5 c = (a+b)/2.5 a = “Hello World” o 型别是动态的,会根据指定时的物件来决定型别 o 变数单纯只是物件的名称,并不会和记忆体绑在一起。 e.q.和记忆体绑在一起的是物件,而不是物件名称。 条件式叙述 (Conditional Statements) Part I * if-else if a < b: z = b else: z = a * pass 叙述 - 不做任何事时使用 if a < b: pass else: z = a 条件式叙述 (Conditional Statements) Part II * elif叙述 if a == ‘+’: op = PLUS elif a == ‘-’: op = MINUS else: op = UNKNOWN o 没有像C语言一样,有switch的语法 * 布林表示式 - and, or, not if b >= a and b <= c: print ‘b is between a and c’ if not (b < a or c > c): print ‘b is still between a and c’ 基本型态 (Numbers and String) * Numbers (数) a = 3 # Integer (整数) b = 4.5 # Float point (浮点数) c = 51728888333L # Long Integer (精准度无限) d = 4 + 3j # Complex number (复数) * Strings (字串) a = 'Hello' # Single quotes b = 'World' # Double quotes c = "Bob said 'hey there.'" # A mix of both d = '''A triple qouted string can span multiple lines like this''' e = """Also works for double quotes""" 基本型态 - 串列(Lists) * 任意物件的串列 a = [2, 3, 4] # A list of integer b = [2, 7, 3.5, “Hello”] # A mixed list c = [] # An empty list d = [2, [a, b]] # A list containing a list e = a + b # Join two lists * 串列的操作 x = a[1] # Get 2nd element (0 is first) y = b[1:3] # Return a sub-list z = d[1][0][2] # Nested lists b[0] = 42 # Change an element 基本型态 - 固定有序列(Tuples) * Tuples f = (2,3,4,5) # A tuple of integers g = (,) # An empty tuple h = (2, [3,4], (10,11,12)) # A tuple containing mixed objects * Tuples的操作 x = f[1] # Element access. x = 3 y = f[1:3] # Slices. y = (3,4) z = h[1][1] # Nesting. z = 4 * 特色 o 与list类似,最大的不同tuple是一种唯读且不可变更的资料结构 o 不可取代tuple中的任意一个元素,因为它是唯读不可变更的 基本型态 - 字典 (Dictionaries) * Dictionaries (关联阵列) a = { } # An empty dictionary b = { ’x’: 3, ’y’: 4 } c = { ’uid’: 105, ’login’: ’beazley’, ’name’ : ’David Beazley’ } * Dictionaries的存取 u = c[’uid’] # Get an element c[’shell’] = "/bin/sh" # Set an element if c.has_key("directory"): # Check for presence of an member d = c[’directory’] else: d = None d = c.get("directory",None) # Same thing, more compact 回圈 (Loops) * while叙述 while a < b: # Do something a = a + 1 * for叙述 (走访序列的元素) for i in [3, 4, 10, 25]: print i # Print characters one at a time for c in "Hello World": print c # Loop over a range of numbers for i in range(0,100): print i 函式 (Functions) * def叙述 # Return the remainder of a/b def remainder(a,b): q = a/b r = a - q*b return r # Now use it a = remainder(42,5) # a = 2 * 回传一个以上的值 def divide(a,b): q = a/b r = a - q*b return q,r x,y = divide(42,5) # x = 8, y = 2 类别 (Classes) * class叙述 class Account: def __init__(self, initial): self.balance = initial def deposit(self, amt): self.balance = self.balance + amt def withdraw(self,amt): self.balance = self.balance - amt def getbalance(self): return self.balance * 使用定义好的class a = Account(1000.00) a.deposit(550.23) a.deposit(100) a.withdraw(50) print a.getbalance() 例外处理 (Exceptions) * try叙述 try: f = open("foo") except IOError: print "Couldn’t open ’foo’. Sorry." * raise叙述 def factorial(n): if n < 0: raise ValueError,"Expected non-negative number" if (n <= 1): return 1 else: return n*factorial(n-1) * 没有处理的例外 >>> factorial(-1) Traceback (innermost last): File "<stdin>", line 1, in ? File "<stdin>", line 3, in factorial ValueError: Expected non-negative number >>> 档案处理 * open()函式 f = open("foo","w") # Open a file for writing g = open("bar","r") # Open a file for reading * 档案的读取/写入 f.write("Hello World") data = g.read() # Read all data line = g.readline() # Read a single line lines = g.readlines() # Read data as a list of lines * 格式化的输入输出 o 使用%来格式化字串 for i in range(0,10): f.write("2 times %d = %d\n" % (i, 2*i)) 模组 (Modules) * 程式可分成好几个模组 # numbers.py def divide(a,b): q = a/b r = a - q*b return q,r def gcd(x,y): g = y while x > 0: g = x x = y % x y = g return g * import叙述 import numbers x,y = numbers.divide(42,5) n = numbers.gcd(7291823, 5683) Python的标准模组函式库 * Python本身就包含了大量的模组提供使用 o String processing o Operating system interfaces o Networking o Threads o GUI o Database o Language services o Security. * 使用模组 import string ... a = string.split(x) 原文转载自 http://www.cdpa.nsysu.edu.tw/wp/wp-content/uploads/2006/07/Python.ppt -- ◢◢◢ ▃▃ ▃▂ ▃▂ ▂▂▂▃ ◤◤ ▎ │ φ批踢踢兔.itsming ▂▂▂ └ ▁▁▁▁▁▁▁▁▁ ▌▌▌ ▎ ▎ ┌ ── ▅▅ ▅▅▅▆ --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.239.158
1F:推 benck:好长= = 不过还是先推1个 11/03 21:51
2F:推 fishead1116:原po超认真 帮推一个 11/03 23:17
3F:推 Poplarysl: 帮推 照顾长帐号~~ 11/04 00:05
※ 编辑: ming1053 来自: 140.112.239.158 (11/04 00:47)
4F:→ chenaren:糟糕 不应该贪睡的 11/05 00:13
5F:推 dennis2030:请问 b = 'World' # Double quotes ' 要改成 " 吗? 11/06 14:29
6F:推 jimmyken793:印象中前後一样就可以 11/06 21:14
7F:推 dennis2030:所以那句的意思是 不管'或是"都是指定成字串? 11/07 00:27
8F:→ dennis2030:话说忘了感谢ming同学努力的帮忙蒐集资料!感谢! 11/07 00:28
9F:推 jimmyken793:对 但是引号要成对 也就是说'' 或 "" 11/07 00:55







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP