Python程式设计入门

上传人:re****.1 文档编号:568639764 上传时间:2024-07-25 格式:PPT 页数:21 大小:339.47KB
返回 下载 相关 举报
Python程式设计入门_第1页
第1页 / 共21页
Python程式设计入门_第2页
第2页 / 共21页
Python程式设计入门_第3页
第3页 / 共21页
Python程式设计入门_第4页
第4页 / 共21页
Python程式设计入门_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《Python程式设计入门》由会员分享,可在线阅读,更多相关《Python程式设计入门(21页珍藏版)》请在金锄头文库上搜索。

1、Python程式設計入門程式設計入門簡介簡介lScript Program LanguagelObject-Oriented Program LanguagelGeneral-Purpose Program LanguagelEasy to learnl誰在使用Python呢?l大神Googlel美國太空總署(NASA)llHow to Become a Hacker 一文中推薦使用使用使用Pythonl有兩種主要使用python的方法l使用互動式命令列le.q. 直接鍵入python就會進入python的互動式命令列l將程式寫成檔案,再由python執行l直在將程式碼寫在檔案內,然後再執行p

2、ython去讀取該檔案Ex: python hello.pyl或是在檔案的第一個行寫著 #!/usr/bin/env python,然後在第二行之後輸入程式碼,如此可以直接執行該檔案Ex: ./hello.pyl作業平台lLinux、FreeBSD lWindows您的第一個您的第一個python程式程式 Hello Worldl使用互動式命令列 print “Hello World”Hello Worldl放在檔案裡#!/usr/bin/env pythonprint “Hello World”l記得將檔案改成可執行 chmod a+x 基本概念基本概念l語法特色l以冒號(:)做為敘述的開始

3、l不必使用分號(;)做為結尾l井字號(#)做為註解符號,同行井字號後的任何字將被忽略l使用tab鍵做為縮排區塊的依據l不必指定變數型態 (runtime時才會進行binding)變數變數(Variables)和和表示式表示式(Expressions)l表示式3 + 53 + (5 * 4)3 * 2Hello + Worldl變數指定a = 4 3b = a * 4.5c = (a+b)/2.5a = “Hello World”l型別是動態的,會根據指定時的物件來決定型別l變數單純只是物件的名稱,並不會和記憶體綁在一起。e.q.和記憶體綁在一起的是物件,而不是物件名稱。條件式敘述條件式敘述(C

4、onditional Statements) Part Ilif-elseif a b:z = belse:z = alpass 敘述 不做任何事時使用if a = a and b = c:print b is between a and cif not (b c):print b is still between a and c基本型態基本型態 (Numbers and String)lNumbers (數)a = 3# Integer (整數)b = 4.5# Float point (浮點數)c = 51728888333L# Long Integer (精準度無限)d = 4 + 3j

5、# Complex number (複數)lStrings (字串)a = 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”基本型態基本型態 串列串列(Lists)l任意物件的串列a = 2, 3, 4 # A list of integerb = 2, 7, 3.5, “Hello” #

6、 A mixed listc = # An empty listd = 2, a, b # A list containing a liste = a + b # Join two listsl串列的操作x = a1 # Get 2nd element (0 is first)y = b1:3 # Return a sub-listz = d102 # Nested listsb0 = 42 # Change an element基本型態基本型態 固定有序列固定有序列(Tuples)lTuplesf = (2,3,4,5) # A tuple of integersg = (,) # An e

7、mpty tupleh = (2, 3,4, (10,11,12) # A tuple containing mixed objectslTuples的操作x = f1 # Element access. x = 3y = f1:3 # Slices. y = (3,4)z = h11 # Nesting. z = 4l特色l與list類似,最大的不同tuple是一種唯讀且不可變更的資料結構l不可取代tuple中的任意一個元素,因為它是唯讀不可變更的基本型態基本型態 字典字典 (Dictionaries)lDictionaries (關聯陣列)a = # An empty dictionary

8、b = x: 3, y: 4 c = uid: 105, login: beazley, name : David Beazley lDictionaries的存取u = cuid # Get an elementcshell = /bin/sh # Set an elementif c.has_key(directory): # Check for presence of an memberd = cdirectoryelse:d = Noned = c.get(directory,None) # Same thing, more compact迴圈迴圈 (Loops)lwhile敘述whi

9、le a b:# Do somethinga = a + 1lfor敘述 (走訪序列的元素)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 i函式函式 (Functions)ldef敘述# Return the remainder of a/bdef remainder(a,b):q = a/br = a - q*breturn r# Now use

10、ita = remainder(42,5) # a = 2l回傳一個以上的值def divide(a,b):q = a/br = a - q*breturn q,rx,y = divide(42,5) # x = 8, y = 2類別類別 (Classes)lclass敘述class Account:def _init_(self, initial):self.balance = initialdef deposit(self, amt):self.balance = self.balance + amtdef withdraw(self,amt):self.balance = self.ba

11、lance - amtdef getbalance(self):return self.balancel使用定義好的classa = Account(1000.00)a.deposit(550.23)a.deposit(100)a.withdraw(50)print a.getbalance()例外處理例外處理 (Exceptions)ltry敘述try:f = open(foo)except IOError:print Couldnt open foo. Sorry.lraise敘述def factorial(n):if n 0:raise ValueError,Expected non-n

12、egative numberif (n factorial(-1)Traceback (innermost last):File , line 1, in ?File , line 3, in factorialValueError: Expected non-negative number檔案處理檔案處理lopen()函式f = open(foo,w) # Open a file for writingg = open(bar,r) # Open a file for readingl檔案的讀取/寫入f.write(Hello World)data = g.read() # Read all

13、 dataline = g.readline() # Read a single linelines = g.readlines() # Read data as a list of linesl格式化的輸入輸出l使用%來格式化字串for i in range(0,10):f.write(2 times %d = %dn % (i, 2*i)模組模組 (Modules)l程式可分成好幾個模組# numbers.pydef divide(a,b):q = a/br = a - q*breturn q,rdef gcd(x,y):g = ywhile x 0:g = xx = y % xy = g

14、return glimport敘述import numbersx,y = numbers.divide(42,5)n = numbers.gcd(7291823, 5683)Python的標準模組函式庫的標準模組函式庫lPython本身就包含了大量的模組提供使用lString processinglOperating system interfaceslNetworkinglThreadslGUIlDatabaselLanguage serviceslSecurity.l使用模組import string.a = string.split(x)參考參考l官方網頁(英)lPython教學文件(中)lhttp:/隨堂測驗隨堂測驗l請寫一支程式輸出下列結果*

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号