python不是完全支持函數(shù)式編程,他支持部分函數(shù)式編程,函數(shù)式編程就是一種抽象程度很高的編程范式,純粹的函數(shù)式編程語言編寫的函數(shù)沒有變量,因此,任意一個函數(shù),只要輸入是確定的,輸出就是確定的,這種純函數(shù)我們稱之為沒有副作用。而允許使用變量的程序設計語言,由于函數(shù)內(nèi)部的變量狀態(tài)不確定,同樣的輸入,可能得到不同的輸出,因此,這種函數(shù)是有副作用的。
成都創(chuàng)新互聯(lián)主要從事網(wǎng)站設計、成都網(wǎng)站制作、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務嘉陵,十載網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
Python對函數(shù)式編程提供部分支持。由于Python允許使用變量,因此,Python不是純函數(shù)式編程語言。
abs() divmod() input() open() staticmethod()
all() enumerate() int() ord() str()
any() eval() isinstance() pow() sum()
basestring() execfile() issubclass() print() super()
bin() file() iter() property() tuple()
bool() filter() len() range() type()
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reverse() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set()
delattr() help() next() setattr()
dict() hex() object() slice()
dir() id() oct() sorted()
Lambda函數(shù)、Map函數(shù)、Filter函數(shù)、Zip函數(shù)、Reduce函數(shù)。
Lambda函數(shù)是Python中功能最強大的函數(shù)之一,它有時也被稱之為匿名函數(shù).。Map是程序員用來簡化程序的Python內(nèi)置函數(shù),此函數(shù)可以在不使用任何循環(huán)的情況下對所有指定的元素進行迭代。Filter是Python中的另一個內(nèi)置函數(shù),當需要區(qū)分其他類型的數(shù)據(jù)時,這個函數(shù)非常有用。Filter函數(shù)經(jīng)常用于根據(jù)特定過濾條件來提取數(shù)據(jù)。
Python(英國發(fā)音:/paθn/美國發(fā)音:/paθn/)是一種廣泛使用的解釋型、高級和通用的編程語言。Python支持多種編程范型,包括函數(shù)式、指令式、結構化、面向?qū)ο蠛头瓷涫骄幊?。它擁有動態(tài)類型系統(tǒng)和垃圾回收功能,能夠自動管理內(nèi)存使用,并且其本身擁有一個巨大而廣泛的標準庫。
字典是一種通過名字或者關鍵字引用的得數(shù)據(jù)結構,其鍵可以是數(shù)字、字符串、元組,這種結構類型也稱之為映射。字典類型是Python中唯一內(nèi)建的映射類型,基本的操作包括如下:
(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中的內(nèi)容:'
printitems
printu'利用dict創(chuàng)建字典,輸出字典內(nèi)容:'
d=dict(items)
printd
printu'查詢字典中的內(nèi)容:'
printd['one']
printd['three']
運算結果:
=======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)容:
或者通過關鍵字創(chuàng)建字典
# _*_ coding:utf-8 _*_
d=dict(one=1,two=2,three=3)
printu'輸出字典內(nèi)容:'
printd
printu'查詢字典中的內(nèi)容:'
printd['one']
printd['three']
運算結果:
=======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
運算結果:
=======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中的內(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復制到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}
注:在復制的副本中對值進行替換后,對原來的字典不產(chǎn)生影響,但是如果修改了副本,原始的字典也會被修改。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的常用內(nèi)置函數(shù)
1.abs() 函數(shù)返回數(shù)字的絕對值
abs(-40)=40
2. dict() 函數(shù)用于創(chuàng)建一個字典
dict()
{} ? ? ?#創(chuàng)建一個空字典類似于u={},字典的存取方式一般為key-value
例如u = {"username":"tom", ?"age":18}
3. help() 函數(shù)用于查看函數(shù)或模塊用途的詳細說明
help('math')查看math模塊的用處
a=[1,2,3,4]
help(a)查看列表list幫助信息
4.dir()獲得當前模塊的屬性列表
dir(help)
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
5.min() 方法返回給定參數(shù)的最小值 /參數(shù)可以為序列
a=? min(10,20,30,40)
a
10
6. next() 返回迭代器的下一個項目
it = iter([1, 2, 3, 4, 5])
next(it)
1
next(it)
2
7. id() 函數(shù)用于獲取對象的內(nèi)存地址
a=12
id(a)
1550569552
8.enumerate() 函數(shù)用于將一個可遍歷的數(shù)據(jù)對象(如列表、元組或字符串)組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標,一般用在 for 循環(huán)當中。
a=["tom","marry","leblan"]
list(enumerate(a))
[(0, 'tom'), (1, 'marry'), (2, 'leblan')]
9. oct() 函數(shù)將一個整數(shù)轉換成8進制字符串
oct(15)
'0o17'
oct(10)
'0o12'
10. bin() 返回一個整數(shù) int 或者長整數(shù) long int 的二進制表示
bin(10)
'0b1010'
bin(15)
'0b1111'
11.eval() 函數(shù)用來執(zhí)行一個字符串表達式,并返回表達式的值
eval('2+2')
4
12.int() 函數(shù)用于將一個字符串會數(shù)字轉換為整型
int(3)
3
int(3.6)
3
int(3.9)
3
int(4.0)
4
13.open() 函數(shù)用于打開一個文件,創(chuàng)建一個file對象,相關的方法才可以調(diào)用它進行讀寫
f=open('test.txt')
14.str() 函數(shù)將對象轉化為適于人閱讀的形式
str(3)
'3'
15. bool() 函數(shù)用于將給定參數(shù)轉換為布爾類型,如果沒有參數(shù),返回 False
bool()
False
bool(1)
True
bool(10)
True
bool(10.0)
True
16.isinstance() 函數(shù)來判斷一個對象是否是一個已知的類型
a=5
isinstance(a,int)
True
isinstance(a,str)
False
17. sum() 方法對系列進行求和計算
sum([1,2,3],5)
11
sum([1,2,3])
6
18. super() 函數(shù)用于調(diào)用下一個父類(超類)并返回該父類實例的方法。super 是用來解決多重繼承問題的,直接用類名調(diào)用父類方法
class ? User(object):
? def__init__(self):
class Persons(User):
? ? ? ? super(Persons,self).__init__()
19. float() 函數(shù)用于將整數(shù)和字符串轉換成浮點數(shù)
float(1)
1.0
float(10)
10.0
20. iter() 函數(shù)用來生成迭代器
a=[1,2,3,4,5,6]
iter(a)
for i in iter(a):
... ? ? ? ? print(i)
...
1
2
3
4
5
6
21.tuple 函數(shù)將列表轉換為元組
a=[1,2,3,4,5,6]
tuple(a)
(1, 2, 3, 4, 5, 6)
22.len() 方法返回對象(字符、列表、元組等)長度或項目個數(shù)
s = "playbasketball"
len(s)
14
a=[1,2,3,4,5,6]
len(a)
6
23. property() 函數(shù)的作用是在新式類中返回屬性值
class User(object):
?def __init__(self,name):
? ? ? ? ? self.name = name
def get_name(self):
? ? ? ? ? return self.get_name
@property
?def name(self):
? ? ? ? ?return self_name
24.type() 函數(shù)返回對象的類型
25.list() 方法用于將元組轉換為列表
b=(1,2,3,4,5,6)
list(b)
[1, 2, 3, 4, 5, 6]
26.range() 函數(shù)可創(chuàng)建一個整數(shù)列表,一般用在 for 循環(huán)中
range(10)
range(0, 10)
range(10,20)
range(10, 20)
27. getattr() 函數(shù)用于返回一個對象屬性值
class w(object):
... ? ? ? ? ? ? s=5
...
a = w()
getattr(a,'s')
5
28. complex() 函數(shù)用于創(chuàng)建一個復數(shù)或者轉化一個字符串或數(shù)為復數(shù)。如果第一個參數(shù)為字符串,則不需要指定第二個參數(shù)
complex(1,2)
(1+2j)
complex(1)
(1+0j)
complex("1")
(1+0j)
29.max() 方法返回給定參數(shù)的最大值,參數(shù)可以為序列
b=(1,2,3,4,5,6)
max(b)
6
30. round() 方法返回浮點數(shù)x的四舍五入值
round(10.56)
11
round(10.45)
10
round(10.45,1)
10.4
round(10.56,1)
10.6
round(10.565,2)
10.56
31. delattr 函數(shù)用于刪除屬性
class Num(object):
...? ? a=1
...? ? b=2
...? ? c=3.
.. print1 = Num()
print('a=',print1.a)
a= 1
print('b=',print1.b)
b= 2
print('c=',print1.c)
c= 3
delattr(Num,'b')
print('b=',print1.b)
Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'
32. hash() 用于獲取取一個對象(字符串或者數(shù)值等)的哈希值
hash(2)
2
hash("tom")
-1675102375494872622
33. set() 函數(shù)創(chuàng)建一個無序不重復元素集,可進行關系測試,刪除重復數(shù)據(jù),還可以計算交集、差集、并集等。
a= set("tom")
b = set("marrt")
a,b
({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})
ab#交集
{'t', 'm'}
a|b#并集
{'t', 'm', 'r', 'o', 'a'}
a-b#差集
{'o'}
當前題目:python支持函數(shù),python支持函數(shù)的遞歸調(diào)用
文章URL:http://muchs.cn/article30/pheepo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、微信公眾號、搜索引擎優(yōu)化、商城網(wǎng)站、網(wǎng)站導航、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)