python查關(guān)鍵字函數(shù) python關(guān)鍵字查找

python的關(guān)鍵字有哪些,都是什么意思?

我這里匯總Python經(jīng)常用到的27個關(guān)鍵字,希望對正在學(xué)Python的你能夠起到幫助

十載的潛山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整潛山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“潛山網(wǎng)站設(shè)計”,“潛山網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

1 and:邏輯與

2 as:為導(dǎo)入的模塊取一個別名,在Python2.6中新增

3 assert:斷言,在Python1.5新增

4 break:用在循環(huán)語句,跳轉(zhuǎn)到語句塊的末尾

5 class:用來定義一個類

6 continue:和break香對應(yīng),跳到語句塊的開頭

7 def:用來定義一個函數(shù)或方法

8 del:刪除

9 elif:全稱是else if

10 exec:內(nèi)置函數(shù)。執(zhí)行以string類型存儲的Python代碼

11 finally:用在異常處理語句try-excep-finally中

12 for:著名的for循環(huán),可以用來遍歷一個列表

13 from:字面意思,表示從一個包導(dǎo)入某個模塊

14 global:在函數(shù)或其他局部作用域中使用全局變量

15 if:如果

16 import:導(dǎo)入

17 in:在,后面跟一個列表,字典或字符串

18 is:邏輯判斷

19 not:邏輯非

20 or:邏輯或

21 pass:占位符,用來告訴Python這里不用考慮

22 print:寫得最多的關(guān)鍵字,后來在Python3.0中變成了內(nèi)置函數(shù)

23 raise:用來引發(fā)一個異常

24 return:函數(shù)返回

25 try:異常處理機(jī)制

26 while:while循環(huán)

27 with:在Python2.6中新增,使用with候不管with中的代碼出現(xiàn)什么錯誤,都會進(jìn)行對當(dāng)前對象進(jìn)行清理工作,注意該句話后面有一個冒號表示with語句。

以上就是我匯總的部分關(guān)鍵字,希望對你有所幫助

python中定義函數(shù)的關(guān)鍵字

函數(shù)是組織好的,可重復(fù)使用的,用來實(shí)現(xiàn)單一,或相關(guān)聯(lián)功能的代碼段。

函數(shù)能提高應(yīng)用的模塊性,和代碼的重復(fù)利用率。你已經(jīng)知道Python提供了許多內(nèi)建函數(shù),比如print()。但你也可以自己創(chuàng)建函數(shù),這被叫做用戶自定義函數(shù)。

定義一個函數(shù)

可以定義一個由自己想要功能的函數(shù),以下是簡單的規(guī)則:

1.函數(shù)代碼塊以 def 關(guān)鍵詞開頭,后接函數(shù)標(biāo)識符名稱和圓括號()。

2.任何傳入?yún)?shù)和自變量必須放在圓括號中間。圓括號之間可以用于定義參數(shù)。

3.函數(shù)的第一行語句可以選擇性地使用文檔字符串—用于存放函數(shù)說明。

4.函數(shù)內(nèi)容以冒號起始,并且縮進(jìn)。

5.return [表達(dá)式] 結(jié)束函數(shù),選擇性地返回一個值給調(diào)用方。不帶表達(dá)式的return相當(dāng)于返回 None。

語法

Python 定義函數(shù)使用 def 關(guān)鍵字,一般格式如下:

1

2

def 函數(shù)名(參數(shù)列表):

函數(shù)體

默認(rèn)情況下,參數(shù)值和參數(shù)名稱是按函數(shù)聲明中定義的順序匹配起來的。

示例

讓我們使用函數(shù)來輸出"Hello World!":

1

2

def hello() :

print("Hello World!")

輸出結(jié)果

hello()

Hello World!

如何查找Python中的關(guān)鍵字

一 查看所有的關(guān)鍵字:help("keywords")

Here?is?a?list?of?the?Python?keywords.??Enter?any?keyword?to?get?more?help.

and?????????????????elif????????????????import??????????????return

as??????????????????else????????????????in??????????????????try

assert??????????????except??????????????is??????????????????while

break???????????????finally?????????????lambda??????????????with

class???????????????for?????????????????not?????????????????yield

continue????????????from????????????????or

def?????????????????global??????????????pass

del?????????????????if??????????????????raise

二 其他

查看python所有的modules:help("modules")

單看python所有的modules中包含指定字符串的modules: help("modules yourstr")

查看python中常見的topics: help("topics")

查看python標(biāo)準(zhǔn)庫中的module:import os.path + help("os.path")

查看python內(nèi)置的類型:help("list")

查看python類型的成員方法:help("str.find")

查看python內(nèi)置函數(shù):help("open")

python字典操作函數(shù)

字典是一種通過名字或者關(guān)鍵字引用的得數(shù)據(jù)結(jié)構(gòu),其鍵可以是數(shù)字、字符串、元組,這種結(jié)構(gòu)類型也稱之為映射。字典類型是Python中唯一內(nèi)建的映射類型,基本的操作包括如下:

(1)len():返回字典中鍵—值對的數(shù)量;

(2)d[k]:返回關(guān)鍵字對于的值;

(3)d[k]=v:將值關(guān)聯(lián)到鍵值k上;

(4)del d[k]:刪除鍵值為k的項(xiàng);

(5)key in d:鍵值key是否在d中,是返回True,否則返回False。

(6)clear函數(shù):清除字典中的所有項(xiàng)

(7)copy函數(shù):返回一個具有相同鍵值的新字典;deepcopy()函數(shù)使用深復(fù)制,復(fù)制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題

(8)fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認(rèn)對應(yīng)的值為None

(9)get函數(shù):訪問字典成員

(10)has_key函數(shù):檢查字典中是否含有給出的鍵

(11)items和iteritems函數(shù):items將所有的字典項(xiàng)以列表方式返回,列表中項(xiàng)來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

(12)keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

(13)pop函數(shù):刪除字典中對應(yīng)的鍵

(14)popitem函數(shù):移出字典中的項(xiàng)

(15)setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值

(16)update函數(shù):用一個字典更新另外一個字典

(17)?values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復(fù)的元素

一、字典的創(chuàng)建

1.1 直接創(chuàng)建字典

d={'one':1,'two':2,'three':3}

printd

printd['two']

printd['three']

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1.2 通過dict創(chuàng)建字典

# _*_ coding:utf-8 _*_

items=[('one',1),('two',2),('three',3),('four',4)]

printu'items中的內(nèi)容:'

printitems

printu'利用dict創(chuàng)建字典,輸出字典內(nèi)容:'

d=dict(items)

printd

printu'查詢字典中的內(nèi)容:'

printd['one']

printd['three']

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

items中的內(nèi)容:

[('one',1), ('two',2), ('three',3), ('four',4)]

利用dict創(chuàng)建字典,輸出字典內(nèi)容:

{'four':4,'three':3,'two':2,'one':1}

查詢字典中的內(nèi)容:

或者通過關(guān)鍵字創(chuàng)建字典

# _*_ coding:utf-8 _*_

d=dict(one=1,two=2,three=3)

printu'輸出字典內(nèi)容:'

printd

printu'查詢字典中的內(nèi)容:'

printd['one']

printd['three']

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出字典內(nèi)容:

{'three':3,'two':2,'one':1}

查詢字典中的內(nèi)容:

二、字典的格式化字符串

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

print"three is %(three)s."%d

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

threeis3.

三、字典方法

3.1?clear函數(shù):清除字典中的所有項(xiàng)

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

d.clear()

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

{}

請看下面兩個例子

3.1.1

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{'two':2,'one':1}

3.1.2

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d.clear()

printd

printdd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{}

3.1.2與3.1.1唯一不同的是在對字典d的清空處理上,3.1.1將d關(guān)聯(lián)到一個新的空字典上,這種方式對字典dd是沒有影響的,所以在字典d被置空后,字典dd里面的值仍舊沒有變化。但是在3.1.2中clear方法清空字典d中的內(nèi)容,clear是一個原地操作的方法,使得d中的內(nèi)容全部被置空,這樣dd所指向的空間也被置空。

3.2?copy函數(shù):返回一個具有相同鍵值的新字典

# _*_ coding:utf-8 _*_

x={'one':1,'two':2,'three':3,'test':['a','b','c']}

printu'初始X字典:'

printx

printu'X復(fù)制到Y(jié):'

y=x.copy()

printu'Y字典:'

printy

y['three']=33

printu'修改Y中的值,觀察輸出:'

printy

printx

printu'刪除Y中的值,觀察輸出'

y['test'].remove('c')

printy

printx

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

初始X字典:

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

X復(fù)制到Y(jié):

Y字典:

{'test': ['a','b','c'],'one':1,'three':3,'two':2}

修改Y中的值,觀察輸出:

{'test': ['a','b','c'],'one':1,'three':33,'two':2}

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

刪除Y中的值,觀察輸出

{'test': ['a','b'],'one':1,'three':33,'two':2}

{'test': ['a','b'],'three':3,'two':2,'one':1}

注:在復(fù)制的副本中對值進(jìn)行替換后,對原來的字典不產(chǎn)生影響,但是如果修改了副本,原始的字典也會被修改。deepcopy函數(shù)使用深復(fù)制,復(fù)制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題。

# _*_ coding:utf-8 _*_

fromcopyimportdeepcopy

x={}

x['test']=['a','b','c','d']

y=x.copy()

z=deepcopy(x)

printu'輸出:'

printy

printz

printu'修改后輸出:'

x['test'].append('e')

printy

printz

運(yùn)算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出:

{'test': ['a','b','c','d']}

{'test': ['a','b','c','d']}

修改后輸出:

{'test': ['a','b','c','d','e']}

{'test': ['a','b','c','d']}

3.3?fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認(rèn)對應(yīng)的值為None

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'])

printd

運(yùn)算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':None,'two':None,'one':None}

或者指定默認(rèn)的對應(yīng)值

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'],'unknow')

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':'unknow','two':'unknow','one':'unknow'}

3.4?get函數(shù):訪問字典成員

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.get('one')

printd.get('four')

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1

None

注:get函數(shù)可以訪問字典中不存在的鍵,當(dāng)該鍵不存在是返回None

3.5?has_key函數(shù):檢查字典中是否含有給出的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.has_key('one')

printd.has_key('four')

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

True

False

3.6?items和iteritems函數(shù):items將所有的字典項(xiàng)以列表方式返回,列表中項(xiàng)來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

list=d.items()

forkey,valueinlist:

printkey,':',value

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

three :3

two :2

one :1

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

it=d.iteritems()

fork,vinit:

print"d[%s]="%k,v

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

d[three]=3

d[two]=2

d[one]=1

3.7?keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printu'keys方法:'

list=d.keys()

printlist

printu'\niterkeys方法:'

it=d.iterkeys()

forxinit:

printx

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

keys方法:

['three','two','one']

iterkeys方法:

three

two

one

3.8?pop函數(shù):刪除字典中對應(yīng)的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.pop('one')

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'three':3,'two':2}

3.9?popitem函數(shù):移出字典中的項(xiàng)

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.popitem()

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'two':2,'one':1}

3.10?setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.setdefault('one',1)

printd.setdefault('four',4)

printd

運(yùn)算結(jié)果:

{'three':3,'two':2,'one':1}

{'four':4,'three':3,'two':2,'one':1}

3.11?update函數(shù):用一個字典更新另外一個字典

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3

}

printd

x={'one':1}

d.update(x)

printd

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':123}

{'three':3,'two':2,'one':1}

3.12?values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復(fù)的元素

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3,

'test':2

}

printd.values()

運(yùn)算結(jié)果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

[2,3,2,123]

python關(guān)鍵字的查詢方法

Python find() 方法檢測字符串中是否包含子字符串 str ,如果指定 beg(開始) 和 end(結(jié)束) 范圍,則檢查是否包含在指定范圍內(nèi),如果包含子字符串返回開始的索引值,否則返回-1。

str.find(str, beg=0, end=len(string))

str -- 指定檢索的字符串

beg -- 開始索引,默認(rèn)為0。

end -- 結(jié)束索引,默認(rèn)為字符串的長度。

初學(xué)者建議用上面的,進(jìn)階可以用正則表達(dá)式

文章名稱:python查關(guān)鍵字函數(shù) python關(guān)鍵字查找
鏈接URL:http://muchs.cn/article36/hgsgsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計、自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計公司、搜索引擎優(yōu)化、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)