干貨:Python3-入門教程-簡(jiǎn)單但比較不錯(cuò)_第1頁
干貨:Python3-入門教程-簡(jiǎn)單但比較不錯(cuò)_第2頁
干貨:Python3-入門教程-簡(jiǎn)單但比較不錯(cuò)_第3頁
干貨:Python3-入門教程-簡(jiǎn)單但比較不錯(cuò)_第4頁
干貨:Python3-入門教程-簡(jiǎn)單但比較不錯(cuò)_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

Python已經(jīng)是3.1版本了,與時(shí)俱進(jìn)更新教程.2.5或2.6請(qǐng)看上一篇文章。本文適合有Java編程經(jīng)驗(yàn)的程序員快速熟悉Python

本文程序在windowsxp+python3.1a1測(cè)試通過.

本文提到的idle指pythonshell,即安裝python后你在菜單看到的IDLE(pythongui)

在idle里ctrl+n可以打開一個(gè)新窗口,輸入源碼后ctrl+s可以保存,f5運(yùn)行程序.

凡打開新窗口即指ctrl+n的操作.

1你好 12字符串和數(shù)字 23列表 34字典 45字符串 56條件和循環(huán)語句 67函數(shù) 78異常處理 89文件處理 810類和繼承 911包機(jī)制 912內(nèi)建幫助手冊(cè) 111你好

復(fù)制代碼代碼如下:

#打開新窗口,輸入:

#!/usr/bin/python

#-*-coding:utf8-*-

s1=input("Inputyourname:")

print("你好,%s"%s1)

'''

知識(shí)點(diǎn):

*input("某字符串")函數(shù):顯示"某字符串",并等待用戶輸入.

*print()函數(shù):如何打印.

*如何應(yīng)用中文

*如何用多行注釋

'''

2字符串和數(shù)字

但有趣的是,在javascript里我們會(huì)理想當(dāng)然的將字符串和數(shù)字連接,因?yàn)槭莿?dòng)態(tài)語言嘛.但在Python里有點(diǎn)詭異,如下:

復(fù)制代碼代碼如下:

#!/usr/bin/python

a=2

b="test"

c=a+b

運(yùn)行這行程序會(huì)出錯(cuò),提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換

復(fù)制代碼代碼如下:

#!/usr/bin/python

#運(yùn)行這行程序會(huì)出錯(cuò),提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換

a=2

b="test"

c=str(a)+b

d="1111"

e=a+int(d)

#Howtoprintmultiplyvalues

print("cis%s,eis%i"%(c,e))

'''

知識(shí)點(diǎn):

*用int和str函數(shù)將字符串和數(shù)字進(jìn)行轉(zhuǎn)換

*打印以#開頭,而不是習(xí)慣的//

*打印多個(gè)參數(shù)的方式

'''

3列表

復(fù)制代碼代碼如下:

#!/usr/bin/python

#-*-coding:utf8-*-

#列表類似Javascript的數(shù)組,方便易用

#定義元組

word=['a','b','c','d','e','f','g']

#如何通過索引訪問元組里的元素

a=word[2]

print("ais:"+a)

b=word[1:3]

print("bis:")

print(b)#index1and2elementsofword.

c=word[:2]

print("cis:")

print(c)#index0and1elementsofword.

d=word[0:]

print("dis:")

print(d)#Allelementsofword.

#元組可以合并

e=word[:2]+word[2:]

print("eis:")

print(e)#Allelementsofword.

f=word[-1]

print("fis:")

print(f)#Thelastelementsofword.

g=word[-4:-2]

print("gis:")

print(g)#index3and4elementsofword.

h=word[-2:]

print("his:")

print(h)#Thelasttwoelements.

i=word[:-2]

print("iis:")

print(i)#Everythingexceptthelasttwocharacters

l=len(word)

print("Lengthofwordis:"+str(l))

print("Addsnewelement")

word.append('h')

print(word)

#刪除元素

delword[0]

print(word)

delword[1:3]

print(word)

'''

知識(shí)點(diǎn):

*列表長(zhǎng)度是動(dòng)態(tài)的,可任意添加刪除元素.

*用索引可以很方便訪問元素,甚至返回一個(gè)子列表

*更多方法請(qǐng)參考Python的文檔

'''

4字典

復(fù)制代碼代碼如下:

#!/usr/bin/python

x={'a':'aaa','b':'bbb','c':12}

print(x['a'])

print(x['b'])

print(x['c'])

forkeyinx:

print("Keyis%sandvalueis%s"%(key,x[key]))

'''

知識(shí)點(diǎn):

*將他當(dāng)Java的Map來用即可.

'''

5字符串

比起C/C++,Python處理字符串的方式實(shí)在太讓人感動(dòng)了.把字符串當(dāng)列表來用吧.

復(fù)制代碼代碼如下:

#!/usr/bin/python

word="abcdefg"

a=word[2]

print("ais:"+a)

b=word[1:3]

print("bis:"+b)#index1and2elementsofword.

c=word[:2]

print("cis:"+c)#index0and1elementsofword.

d=word[0:]

print("dis:"+d)#Allelementsofword.

e=word[:2]+word[2:]

print("eis:"+e)#Allelementsofword.

f=word[-1]

print("fis:"+f)#Thelastelementsofword.

g=word[-4:-2]

print("gis:"+g)#index3and4elementsofword.

h=word[-2:]

print("his:"+h)#Thelasttwoelements.

i=word[:-2]

print("iis:"+i)#Everythingexceptthelasttwocharacters

l=len(word)

print("Lengthofwordis:"+str(l))

中文和英文的字符串長(zhǎng)度是否一樣?

復(fù)制代碼代碼如下:

#!/usr/bin/python

#-*-coding:utf8-*-

s=input("輸入你的中文名,按回車?yán)^續(xù)");

print("你的名字是:"+s)

l=len(s)

print("你中文名字的長(zhǎng)度是:"+str(l))

知識(shí)點(diǎn):

類似Java,在python3里所有字符串都是unicode,所以長(zhǎng)度一致.

6條件和循環(huán)語句

復(fù)制代碼代碼如下:

#!/usr/bin/python

#條件和循環(huán)語句

x=int(input("Pleaseenteraninteger:"))

ifx<0:

x=0

print("Negativechangedtozero")

elifx==0:

print("Zero")

else:

print("More")

#LoopsList

a=['cat','window','defenestrate']

forxina:

print(x,len(x))

#知識(shí)點(diǎn):

#*條件和循環(huán)語句

#*如何得到控制臺(tái)輸入

7函數(shù)

復(fù)制代碼代碼如下:

#!/usr/bin/python

#-*-coding:utf8-*-

defsum(a,b):

returna+b

func=sum

r=func(5,6)

print(r)

#提供默認(rèn)值

defadd(a,b=2):

returna+b

r=add(1)

print(r)

r=add(1,5)

print(r)

一個(gè)好用的函數(shù)

#!/usr/bin/python

#-*-coding:utf8-*-

#Therange()function

a=range(1,10)

foriina:

print(i)

a=range(-2,-11,-3)#The3rdparameterstandsforstep

foriina:

print(i)

知識(shí)點(diǎn):

Python不用{}來控制程序結(jié)構(gòu),他強(qiáng)迫你用縮進(jìn)來寫程序,使代碼清晰.

定義函數(shù)方便簡(jiǎn)單

方便好用的range函數(shù)

8異常處理

復(fù)制代碼代碼如下:

#!/usr/bin/python

s=input("Inputyourage:")

ifs=="":

raiseException("Inputmustnobeempty.")

try:

i=int(s)

exceptExceptionaserr:

print(err)

finally:#Cleanupaction

print("Goodbye!")

9文件處理

對(duì)比Java,python的文本處理再次讓人感動(dòng)

復(fù)制代碼代碼如下:

#!/usr/bin/python

spath="D:/download/baa.txt"

f=open(spath,"w")#Opensfileforwriting.Createsthisfiledoesn'texist.

f.write("Firstline1.\n")

f.writelines("Firstline2.")

f.close()

f=open(spath,"r")#Opensfileforreading

forlineinf:

print("每一行的數(shù)據(jù)是:%s"%line)

f.close()

知識(shí)點(diǎn):

open的參數(shù):r表示讀,w寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nèi)容.

打開文件之后記得關(guān)閉

10類和繼承

復(fù)制代碼代碼如下:

classBase:

def__init__(self):

self.data=[]

defadd(self,x):

self.data.append(x)

defaddtwice(self,x):

self.add(x)

self.add(x)

#ChildextendsBase

classChild(Base):

defplus(self,a,b):

returna+b

oChild=Child()

oChild.add("str1")

print(oChild.data)

print(oChild.plus(2,3))

'''

知識(shí)點(diǎn):

*self:類似Java的this參數(shù)

'''

11包機(jī)制

每一個(gè).py文件稱為一個(gè)module,module之間可以互相導(dǎo)入.請(qǐng)參看以下例子:

復(fù)制代碼代碼如下:

#a.py

defadd_func(a,b):

returna+b

#b.py

fromaimportadd_func#Alsocanbe:importa

print("Importadd_funcfrommodulea")

print("Resultof1plus2is:")

print(add_func(1,2))#Ifusing"importa",thenhereshouldbe"a.add_func"

module可以定義在包里面.Python定義包的方式稍微有點(diǎn)古怪,假設(shè)我們有一個(gè)parent文件夾,該文件夾有一個(gè)child子文件夾.child中有一個(gè)modulea.py.如何讓Python知道這個(gè)文件層次結(jié)構(gòu)?很簡(jiǎn)單,每個(gè)目錄都放一個(gè)名為_init_.py的文件.該文件內(nèi)容可以為空.這個(gè)層次結(jié)構(gòu)如下所示:

parent

--__init_.py

--child

--__init_.py

--a.py

b.p

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論