python可使用的函數(shù)的簡單介紹

python有多少內置函數(shù)

Python內置函數(shù)有很多,為大家推薦5個神仙級的內置函數(shù):

龍安網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)。

(1)Lambda函數(shù)

用于創(chuàng)建匿名函數(shù),即沒有名稱的函數(shù)。它只是一個表達式,函數(shù)體比def簡單很多。當我們需要創(chuàng)建一個函數(shù)來執(zhí)行單個操作并且可以在一行中編寫時,就可以用到匿名函數(shù)了。

Lamdba的主體是一個表達式,而不是一個代碼塊。僅僅能在lambda表達式中封裝有限的邏輯進去。

利用Lamdba函數(shù),往往可以將代碼簡化許多。

(2)Map函數(shù)

會將一個函數(shù)映射到一個輸入列表的所有元素上,比如我們先創(chuàng)建了一個函數(shù)來返回一個大寫的輸入單詞,然后將此函數(shù)應有到列表colors中的所有元素。

我們還可以使用匿名函數(shù)lamdba來配合map函數(shù),這樣可以更加精簡。

(3)Reduce函數(shù)

當需要對一個列表進行一些計算并返回結果時,reduce()是個非常有用的函數(shù)。舉個例子,當需要計算一個整數(shù)列表所有元素的乘積時,即可使用reduce函數(shù)實現(xiàn)。

它與函數(shù)的最大的區(qū)別就是,reduce()里的映射函數(shù)(function)接收兩個參數(shù),而map接收一個參數(shù)。

(4)enumerate函數(shù)

用于將一個可遍歷的數(shù)據(jù)對象(如列表、元組或字符串)組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標,一般用在for循環(huán)當中。

它的兩個參數(shù),一個是序列、迭代器或其他支持迭代對象;另一個是下標起始位置,默認情況從0開始,也可以自定義計數(shù)器的起始編號。

(5)Zip函數(shù)

用于將可迭代的對象作為參數(shù),將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的列表

當我們使用zip()函數(shù)時,如果各個迭代器的元素個數(shù)不一致,則返回列表長度與最短的對象相同。

Python常用函數(shù)三有哪些?這7個函數(shù)使用頻率最高,總算搞明白了

1.1 例如:print(hex(2))案例

1.2 輸出函數(shù):print(hex(2))

1.3 輸出結果:0x2

1.4 解析說明:返回16進制的數(shù)。

2.1 例如:print(chr(10))案例

2.2 輸出函數(shù):print(chr(10))

2.3 輸出結果:0o12

2.4 解析說明:返回當前整數(shù)對應的ASCll碼

3.1 例如:print(ord("b"))案例

3.2 輸出函數(shù):print(ord("b"))

3.3 輸出結果:98

3.4 解析說明:返回當前ASCll碼的10進制數(shù)

4.1 例如:print(chr(97))

4.2 輸出函數(shù):print(chr(97))

4.3 輸出結果:b

4.4 解析說明:返回當前ASCll碼的10進制數(shù)。

案例一:給你一個字符串,s = 'hello kitty'

1.1 輸出函數(shù):print(s.capitalize())

1.2 輸出結果:0x2

1.3 解析說明:返回16進制的數(shù)。

2.1輸出函數(shù):print(s.replace('kitty','kuang'))

2.2 輸出結果:hello kuang

2.3 解析說明:替換功能,將kitty換成kuang。

2.4 輸出函數(shù):print(s.replace('4','KK'))

2.5 輸出結果:12KK12KK

2.6 解析說明:所有的4都替換成KK

2.7 輸出函數(shù):print(s.replace('4','KK'))

2.8 輸出結果:12KK12KK124

2.9 解析說明:將前兩個的4替換成go

案例一:給你一個字符串,ip = '192.168.1.1'

3.1 輸出函數(shù):print(ip.split(','))

3.2 輸出結果:['192.168.1.1']

3.3 解析說明:將字符串分割成列表

案例一:給你一個字符串,ip = '192.168.1.1'

3.3 輸出函數(shù):print(ip.split(',',2))

3.4 輸出結果:['192.168.1.1']

3.5 解析說明:從第二個開始分割成列表

python里面有哪些自帶函數(shù)?

python系統(tǒng)提供了下面常用的函數(shù):

1. 數(shù)學庫模塊(math)提供了很多數(shù)學運算函數(shù);

2.復數(shù)模塊(cmath)提供了用于復數(shù)運算的函數(shù);

3.隨機數(shù)模塊(random)提供了用來生成隨機數(shù)的函數(shù);

4.時間(time)和日歷(calendar)模塊提供了能處理日期和時間的函數(shù)。

注意:在調用系統(tǒng)函數(shù)之前,先要使用import 語句導入 相應的模塊

該語句將模塊中定義的函數(shù)代碼復制到自己的程 序中,然后就可以訪問模塊中的任何函數(shù),其方 法是在函數(shù)名前面加上“模塊名.”。

希望能幫到你。

python字典操作函數(shù)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

printd

printd['two']

printd['three']

運算結果:

=======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中的內容:'

printitems

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

d=dict(items)

printd

printu'查詢字典中的內容:'

printd['one']

printd['three']

運算結果:

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

items中的內容:

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

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

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

查詢字典中的內容:

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

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

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

printu'輸出字典內容:'

printd

printu'查詢字典中的內容:'

printd['one']

printd['three']

運算結果:

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

輸出字典內容:

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

查詢字典中的內容:

二、字典的格式化字符串

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

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

printd

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

運算結果:

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

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

threeis3.

三、字典方法

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

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

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

printd

d.clear()

printd

運算結果:

=======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

運算結果:

=======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

運算結果:

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

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

{}

{}

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

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

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

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

printu'初始X字典:'

printx

printu'X復制到Y:'

y=x.copy()

printu'Y字典:'

printy

y['three']=33

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

printy

printx

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

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

printy

printx

運算結果:

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

初始X字典:

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

X復制到Y:

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}

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

# _*_ 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

運算輸出:

=======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ù):使用給定的鍵建立新的字典,鍵默認對應的值為None

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

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

printd

運算輸出:

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

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

或者指定默認的對應值

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

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

printd

運算結果:

=======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')

運算結果:

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

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

1

None

注:get函數(shù)可以訪問字典中不存在的鍵,當該鍵不存在是返回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')

運算結果:

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

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

True

False

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

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

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

printd

list=d.items()

forkey,valueinlist:

printkey,':',value

運算結果:

=======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

運算結果:

=======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

運算結果:

=======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ù):刪除字典中對應的鍵

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

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

printd

d.pop('one')

printd

運算結果:

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

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

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

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

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

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

printd

d.popitem()

printd

運算結果:

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

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

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

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

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

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

printd

printd.setdefault('one',1)

printd.setdefault('four',4)

printd

運算結果:

{'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

運算結果:

=======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返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復的元素

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

d={

'one':123,

'two':2,

'three':3,

'test':2

}

printd.values()

運算結果:

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

[2,3,2,123]

分享文章:python可使用的函數(shù)的簡單介紹
URL分享:http://muchs.cn/article40/doedoho.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、虛擬主機、用戶體驗、建站公司、品牌網(wǎng)站設計、網(wǎng)站改版

廣告

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

h5響應式網(wǎng)站建設