表示一個功能,函數(shù)定義著是提供功能的人,函數(shù)調(diào)用者是使用功能的人。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:申請域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、岳普湖網(wǎng)站維護(hù)、網(wǎng)站推廣。
print:打印功能,將括號中的內(nèi)容,顯示到終端。
將括號中的內(nèi)容顯示在控制臺.
input:輸入功能,從終端中獲取輸入的信息,存到程序變量當(dāng)中
作用:將用戶輸入的內(nèi)容賦值給變量
第一個字符必須是字母表中字母或下劃線 _ 。
標(biāo)識符的其他的部分由字母、數(shù)字和下劃線組成。
標(biāo)識符對大小寫敏感。
python最具特色的就是使用縮進(jìn)來表示代碼塊,不需要使用大括號 {} 。
縮進(jìn)的空格數(shù)是可變的,但是同一個代碼塊的語句必須包含相同的縮進(jìn)空格數(shù)。實(shí)例如下:
通過關(guān)鍵字def來創(chuàng)建函數(shù),def的作用是實(shí)現(xiàn)python中函數(shù)的創(chuàng)建
函數(shù)定義過程:
函數(shù)名+()小括號執(zhí)行函數(shù)
函數(shù)體內(nèi)對全局變量只能讀取,不能修改
局部變量,無法在函數(shù)體外使用
python 使用 lambda 來創(chuàng)建匿名函數(shù)。
所謂匿名,意即不再使用 def 語句這樣標(biāo)準(zhǔn)的形式定義一個函數(shù)。
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ù)或模塊用途的詳細(xì)說明
help('math')查看math模塊的用處
a=[1,2,3,4]
help(a)查看列表list幫助信息
4.dir()獲得當(dāng)前模塊的屬性列表
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ù)下標(biāo),一般用在 for 循環(huán)當(dāng)中。
a=["tom","marry","leblan"]
list(enumerate(a))
[(0, 'tom'), (1, 'marry'), (2, 'leblan')]
9. oct() 函數(shù)將一個整數(shù)轉(zhuǎn)換成8進(jìn)制字符串
oct(15)
'0o17'
oct(10)
'0o12'
10. bin() 返回一個整數(shù) int 或者長整數(shù) long int 的二進(jìn)制表示
bin(10)
'0b1010'
bin(15)
'0b1111'
11.eval() 函數(shù)用來執(zhí)行一個字符串表達(dá)式,并返回表達(dá)式的值
eval('2+2')
4
12.int() 函數(shù)用于將一個字符串會數(shù)字轉(zhuǎn)換為整型
int(3)
3
int(3.6)
3
int(3.9)
3
int(4.0)
4
13.open() 函數(shù)用于打開一個文件,創(chuàng)建一個file對象,相關(guān)的方法才可以調(diào)用它進(jìn)行讀寫
f=open('test.txt')
14.str() 函數(shù)將對象轉(zhuǎn)化為適于人閱讀的形式
str(3)
'3'
15. bool() 函數(shù)用于將給定參數(shù)轉(zhuǎn)換為布爾類型,如果沒有參數(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() 方法對系列進(jìn)行求和計算
sum([1,2,3],5)
11
sum([1,2,3])
6
18. super() 函數(shù)用于調(diào)用下一個父類(超類)并返回該父類實(shí)例的方法。super 是用來解決多重繼承問題的,直接用類名調(diào)用父類方法
class ? User(object):
? def__init__(self):
class Persons(User):
? ? ? ? super(Persons,self).__init__()
19. float() 函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點(diǎn)數(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ù)將列表轉(zhuǎn)換為元組
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() 方法用于將元組轉(zhuǎn)換為列表
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)建一個復(fù)數(shù)或者轉(zhuǎn)化一個字符串或數(shù)為復(fù)數(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() 方法返回浮點(diǎn)數(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)建一個無序不重復(fù)元素集,可進(jìn)行關(guān)系測試,刪除重復(fù)數(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'}
字典是一種通過名字或者關(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的項;
(5)key in d:鍵值key是否在d中,是返回True,否則返回False。
(6)clear函數(shù):清除字典中的所有項
(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將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表
(12)keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器
(13)pop函數(shù):刪除字典中對應(yīng)的鍵
(14)popitem函數(shù):移出字典中的項
(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ù):清除字典中的所有項
# _*_ 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將所有的字典項以列表方式返回,列表中項來自(鍵,值),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ù):移出字典中的項
# _*_ 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]
1. print()函數(shù):打印字符串
2. raw_input()函數(shù):從用戶鍵盤捕獲字符
3. len()函數(shù):計算字符長度
4. format(12.3654,'6.2f'/'0.3%')函數(shù):實(shí)現(xiàn)格式化輸出
5. type()函數(shù):查詢對象的類型
6. int()函數(shù)、float()函數(shù)、str()函數(shù)等:類型的轉(zhuǎn)化函數(shù)
7. id()函數(shù):獲取對象的內(nèi)存地址
8. help()函數(shù):Python的幫助函數(shù)
9. s.islower()函數(shù):判斷字符小寫
10. s.sppace()函數(shù):判斷是否為空格
11. str.replace()函數(shù):替換字符
12. import()函數(shù):引進(jìn)庫
13. math.sin()函數(shù):sin()函數(shù)
14. math.pow()函數(shù):計算次方函數(shù)
15. 3**4: 3的4次方
16. pow(3,4)函數(shù):3的4次方
17. os.getcwd()函數(shù):獲取當(dāng)前工作目錄
18. listdir()函數(shù):顯示當(dāng)前目錄下的文件
19. socket.gethostbyname()函數(shù):獲得某主機(jī)的IP地址
20. urllib.urlopen(url).read():打開網(wǎng)絡(luò)內(nèi)容并存儲
21. open().write()函數(shù):寫入文件
22. webbrowser.open_new_tab()函數(shù):新建標(biāo)簽并使用瀏覽器打開指定的網(wǎng)頁
23. def function_name(parameters):自定義函數(shù)
24. time.sleep()函數(shù):停止一段時間
25. random.randint()函數(shù):產(chǎn)生隨機(jī)數(shù)
新聞名稱:python函數(shù)解說,python代碼函數(shù)
網(wǎng)站鏈接:http://muchs.cn/article44/hcgeee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、服務(wù)器托管、定制開發(fā)、網(wǎng)頁設(shè)計公司、網(wǎng)站維護(hù)、網(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)