Python3入門教程簡單但比較不錯_第1頁
Python3入門教程簡單但比較不錯_第2頁
Python3入門教程簡單但比較不錯_第3頁
Python3入門教程簡單但比較不錯_第4頁
Python3入門教程簡單但比較不錯_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Python已經(jīng)是3.1版本了,與時俱進(jìn)更新教程.2.5或2.6請看上一篇文章。本文適合有Java編程經(jīng)驗的程序員快速熟悉Python本文程序在windows xp+python3.1a1測試通過.本文提到的idle指python shell,即安裝python后你在菜單看到的IDLE(python gui)在idle里ctrl+n可以打開一個新窗口,輸入源碼后ctrl+s可以保存,f5運行程序.凡打開新窗口即指 ctrl+n的操作. TOC o 1-5 h z HYPERLINK l bookmark0 o Current Document 1你好1 HYPERLINK l bookmark

2、2 o Current Document 2字符串和數(shù)字 2 HYPERLINK l bookmark4 o Current Document 3列表3 HYPERLINK l bookmark6 o Current Document 4字典4 HYPERLINK l bookmark8 o Current Document 5字符串5 HYPERLINK l bookmark10 o Current Document 6條件和循環(huán)語句 6 HYPERLINK l bookmark12 o Current Document 7函數(shù)7 HYPERLINK l bookmark14 o Curre

3、nt Document 8異常處理8 HYPERLINK l bookmark16 o Current Document 9文件處理8 HYPERLINK l bookmark18 o Current Document 10類和繼承9 HYPERLINK l bookmark20 o Current Document 11包機制9 HYPERLINK l bookmark22 o Current Document 12內(nèi)建幫助手冊111你好復(fù)制代碼彳t碼如下:2字符串和數(shù)字但有趣的是,在javascript里我們會理想當(dāng)然的將字符串和數(shù)字連接,因為是動態(tài)語言嘛.但在Python里有點詭異,如下

4、:復(fù)制代碼彳t碼如下:3列表復(fù)制代碼彳t碼如下:! /usr/bin/python-*- coding: utf8 -*-列表類似Javascript的數(shù)組,方便易用定義元組word=a,b,c,d,e,f,g如何通過索引訪問元組里的元素a=word2print (a is: +a)b=word1:3print (b is:)print (b) # index 1 and 2 elements of word.c=word:2print (c is:)print (c) # index 0 and 1 elements of word.d=word0:print (d is:)print (d

5、) # All elements of word.元組可以合并e=word:2+word2:print (e is:)print (e) # All elements of word.f=word-1print (f is:)print (f) # The last elements of word.g=word-4:-2print (g is:)print (g) # index 3 and 4 elements of word.h=word-2:print (h is:)print (h) # The last two elements.i=word:-2print (i is:)prin

6、t (i) # Everything except the last two characters l=len(word)print (Length of word is: + str(l)print (Adds new element) word.append(h) print (word) #刪除元素del word0 print (word) del word1:3 print (word) III知識點:列表長度是動態(tài)的,可任意添加刪除元素.用索引可以很方便訪問元素,甚至返回一個子列表更多方法請參考 Python的文檔4字典復(fù)制代碼彳t碼如下:#! /usr/bin/pythonx=a

7、:aaa,b:bbb,c:12print (xa)print (xb)print (xc)for key in x:print (Key is %s and value is %s % (key,xkey)III知識點:*將他當(dāng)Java的Map來用即可.5字符串比起C/C+,Python 處理字符串的方式實在太讓人感動了.把字符串當(dāng)列表來用吧 復(fù)制代碼代碼如下:#! /usr/bin/python word=abcdefg a=word2print (a is:+a)b=word1:3print (b is:+b)index 1 and2 elements of word.c=word:2pr

8、int (c is:+c)index 0 and1 elements of word.d=word0:print (d is:+d)Allelementsof word.e=word:2+word2:print (e is:+e)Allelementsof word.f=word-1print (f is:+f)# Thelast elements of word.g=word-4:-2print (g is:+g)# index 3and 4 elements of word.h=word-2:print (h is:+h)# The lasttwo elements.i=word:-2pr

9、int (i is:+i) # Everythingexcept the last two characters! /usr/bin/python-*- coding: utf8 -*-s=input(輸入你的中文名,按回車?yán)^續(xù));print (你的名字是 :+s)l=len(s)print (你中文名字的長度是:+str(l)知識點:類似Java,在python3里所有字符串都是unicode,所以長度一致6條件和循環(huán)語句復(fù)制代碼彳t碼如下:! /usr/bin/python條件和循環(huán)語句x=int(input(Please enter an integer:) if x0: x=0prin

10、t (Negative changed to zero)elif x=0:print (Zero)else:print (More)Loops List a = cat, window, defenestrate for x in a: print (x, len(x)知識點:*條件和循環(huán)語句*如何得到控制臺輸入7函數(shù)復(fù)制代碼彳t碼如下:! /usr/bin/python-*- coding: utf8 -*- def sum(a,b): return a+bfunc = sumr = func(5,6) print (r)提供默認(rèn)值def add(a,b=2):return a+br=add

11、 print (r) r=add(1,5) print (r) 一個好用的函數(shù)! /usr/bin/python-*- coding: utf8 -*-The range() function a =range (1,10) for i in a: print (i)a = range(-2,-11,-3)# The 3rd parameter stands for stepfor i in a: print (i)知識點:Python不用。來控制程序結(jié)構(gòu),他強迫你用縮進(jìn)來寫程序,使代碼清晰定義函數(shù)方便簡單方便好用的range函數(shù)8異常處理復(fù)制代碼彳t碼如下:! /usr/bin/python

12、 s=input(Input your age:) if s =:raise Exception(Input must no be empty.) try: i=int(s)except Exception as err:print(err)finally: # Clean up actionprint(Goodbye!)9文件處理對比Java,python的文本處理再次讓人感動 復(fù)制代碼彳t碼如下:! /usr/bin/pythonspath=D:/download/baa.txtf=open(spath,w) # Opens file for writing.Creates this fi

13、le doesnt exist.f.write(First line 1.n)f.writelines(First line 2.)f.close()f=open(spath,r) # Opens file for reading for line in f:print(每一行的數(shù)據(jù)是:%s%line)f.close()知識點:open的參數(shù):r表示讀,w寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nèi)容 打開文件之后記得關(guān)閉10類和繼承復(fù)制代碼彳t碼如下:class Base:def _init_(self): self.data = def add(self, x): self.data.a

14、ppend(x) def addtwice(self, x): self.add(x) self.add(x) # Child extends Base class Child(Base): def plus(self,a,b): return a+b oChild =Child() oChild.add(str1) print (oChild.data) print (oChild.plus(2,3) 知識點:* self:類似Java的this參數(shù) III11包機制每一個.py文件稱為一個module,module之間可以互相導(dǎo)入.請參看以下例子 復(fù)制代碼代碼如下:# a.py def a

15、dd_func(a,b): return a+b# b.pyfrom a importadd_func # Also can beimport aprint (Importadd_func from module a)print (Result of 1 plus 2 is:)print (add func(1,2)# If using import a , then here should be a.add funcmodule可以定義在包里面.Python定義包的方式稍微有點古怪,假設(shè)我們有一個parent文件夾,該文件夾有一個 child子文件夾.child中有一個 module a.p

16、y .如何讓 Python知道這個 文件層次結(jié)構(gòu)?很簡單,每個目錄都放一個名為 _init_.py的文件.該文件內(nèi)容可以為空.這個 層次結(jié)構(gòu)如下所示:parent -_init_.py -child-_init_.py-a.py b.py 那么Python如何找到我們定義的module?在標(biāo)準(zhǔn)包sys中,path屬性記錄了 Python的包路徑.你可以將之打印出來:import sys print(sys.path) 通常我們可以將 module的包路徑放到環(huán)境變量PYTHONPATH中,該環(huán)境變量會自動添加到sys.path屬性.另一種方便的方法是編程中直接指定我們的module路徑到sys

17、.path中:復(fù)制代碼彳t碼如下:import sys import os sys.path.append(os.getcwd()+parentchild) print(sys.path)from a import add_funcprint (sys.path)print (Import add_func from module a)print (Result of 1 plus 2 is:)print (add_func(1,2)知識點:如何定義模塊和包如何將模塊路徑添加到系統(tǒng)路徑,以便python找到它們?nèi)绾蔚玫疆?dāng)前路徑12內(nèi)建幫助手冊對比C+,Java的突出進(jìn)步是內(nèi)建Javadoc機制

18、,程序員可以通過閱讀Javadoc 了解函數(shù)用法.Python也內(nèi)建了一些方便函數(shù)以便程序員參考.dir函數(shù):查看某個類/對象的方法.如果有某個方法想不起來,請敲dir.在idle里,試tdir(l ist)help函數(shù):詳細(xì)的類/對象介紹.在idle里,試thelp(list)#打開新窗口,輸入:#! /usr/bin/python# -* * * - coding: utf8 -*- s1=input(Input your name:)print(你好,s % s1) 知識點:* input(某字符串。函數(shù):顯示某字符串,并等待用戶輸入* print()函數(shù):如何打印.*如何應(yīng)用中文*如何用多行注

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論