鍑芥暟鐨勭畝鍗曞簲鐢�

1.鍑芥暟鐨勭畝鍗曞簲鐢�

鍏叡锛�

聽 聽 str, int,float,bool,bytes,list,set,tuple, dict,max,min,len,print,input

鐗瑰畾绫诲瀷鐨勫嚱鏁帮細

聽 Math :聽 pow(), sqrt().

str:聽 find(), split(), replace()..

random:聽 random()

os:聽 listdir(),remove()

os.path:聽 isfile() , isdir()

鍑芥暟鏈川灏辨槸灏嗛噸澶嶇殑浠g爜灏佽鍦ㄤ竴鍧楋紝褰撲娇鐢ㄦ椂鍙閲嶅鍦拌皟鐢ㄣ��

瀹氫箟鍑芥暟鐨勮娉曪細

聽 聽 def鍑芥暟鍚� (鍙傛暟鍒楄〃):

鍔熻兘璇彞

聽 聽 聽 聽 [return琛ㄨ揪寮廬聽 #濡傛灉鍑芥暟鏈夎繑鍥炲�硷紝鍒欎娇鐢� return

濡傦細

聽 聽 聽 def聽 sub(x, y):聽 聽 聽 #瀹氫箟鏃讹紝鍑芥暟鐨勫弬鏁板悕绉颁箣涓� 褰㈠弬

聽 聽 聽 聽 聽 聽 聽 return x + y聽 聽 #杩斿洖涓や釜鏁扮殑鍜�

聽 聽 聽 s = sub(10, 20)聽 # s : 30 ,璋冪敤鏃讹紝鍑芥暟鐨勫弬鏁扮О涔嬩负 瀹炲弬

娉ㄦ剰锛� 缂栧啓瑙勮寖锛屽嚱鏁扮殑鍓嶅悗搴旇绌轰袱琛�

鍑芥暟涓槸鍚﹀瓨鍦ㄥ弬鏁帮紝閮藉繀椤诲瓨鍦� 灏忔嫭鍙� ( )

鍑芥暟涓瓨鍦╳hile寰幆鏃讹紝濡傛灉鍦ㄥ惊鐜鍙ユ湁 return璇彞锛屽垯浼氫腑鏂惊鐜�

鍑芥暟鍚嶇殑鍛藉悕锛� 璺熷彉閲忓悕鐨勫懡鍚嶈鍒欐槸涓�鏍风殑

2.鍑芥暟鐨勫叧閿瓧鍙傛暟

璋冪敤鍑芥暟鏃讹紝鍙互浣跨敤鍑芥暟鐨勫舰鍙傚悕鏉ヤ紶鍊笺��

濡傛灉浣跨敤褰㈠弬鍚嶆寚瀹氬弬鏁板�硷紝鍒欐墍鏈夊弬鏁板繀椤荤粺涓�锛� 闄や簡甯﹂粯璁ゅ�肩殑鍙傛暟

3.鍑芥暟鍙傛暟鐨勯粯璁ゅ��

聽 聽 聽 鏄湪褰㈠弬鍚嶅悗 璁剧疆榛樿鍊硷細def xx(ns=10)

鏈夐粯璁ゅ�肩殑鍙傛暟锛屽湪璋冪敤鏃跺彲浠ヤ笉鐢ㄦ彁渚涘疄鍙�

4.涓嶅畾闀垮害鐨勫弬鏁�

聽 聽 聽 *args聽 :聽 tuple鍏冪粍绫诲瀷

聽 聽 聽 **kwargs:聽 dict瀛楀吀

聽 聽 聽 鍦ㄥ嚱鏁扮殑鍙傛暟鍒楄〃涓紝*args鍜�**kwargs涓�鑸鏀惧湪鏅�氱殑鍙傛暟鐨勫悗闈紝

聽 聽 聽 濡傛灉鍦ㄥ墠闈㈢殑璇濓紝鏅�氬弬鏁板繀椤讳娇鐢ㄥ叧閿弬鏁颁紶鍊�(鍙傛暟鍚� =鍙傛暟鍊�)銆�

5.鍖垮悕鍑芥暟

聽 鍑芥暟鍚嵚� = lambda鍙傛暟鍒楄〃:琛ㄨ揪寮�,鍑芥暟鐨勮繑鍥炲�煎嵆鏄〃杈惧紡鍊�

聽 鍑芥暟鍚�(瀹炲弬鍒楄〃)

6.鍋忓嚱鏁�

锛� functools.partial(鐜版湁鐨勫嚱鏁板悕锛� 鍏抽敭鍙傛暟璧嬪�艰〃杈惧紡)锛岃繑鍥炲嚱鏁板悕

琛ㄨ薄锛� 鍋忓嚱鏁板嵆鏄幇鏈夊嚱鏁扮殑鍒嗚韩锛屽尯鍒槸鍏抽敭鍙傛暟鍊间笉鍚屻��

7.浣滅敤鍩�

鍙拡瀵规ā鍧椼�佺被锛屽嚱鏁颁腑鐨勫彉閲忥紝杩欎簺鍙橀噺涓�鑸彧浣滅敤鍦ㄥ鍑芥暟鐨勫眬閮ㄣ��

濡傛灉鎯冲皢灞�鍙樻彁鍗囦负鍏ㄥ眬锛�

聽 聽 聽 聽 global灞�閮ㄥ彉閲忓悕

8. Python鐨勫紓甯稿鐞�

涔嬪墠鐨勪唬鐮佺粡甯镐細鍑虹幇鐨勫紓甯革細

聽 聽 聽 TypeError, KeyError锛孖ndexError銆� ValueError

Exception

9.鍑芥暟

1锛夊畾涔夊嚱鏁�

def聽 鍑芥暟鍚�(鍙傛暟鍒楄〃)锛�

璇彞

return 缁撴灉

濡傚嚱鏁版病鏈変竴涓猺eturn ,鍒欒繑鍥炴槸None

def add(x, y):

s = x + y

return s

聽 聽 聽 聽聽 璋冪敤鍑芥暟锛歛dd(10, 20)聽 杩斿洖30

聽 聽 聽 聽聽 鍏抽敭鍙傛暟浼犲�硷細

add(y=10, x=20)

聽 聽 聽 聽聽 鍙傛暟榛樿鍊�

def聽 multiple(x, base=4):

if x % base == 0:

return True

print(鈥榟ahahaha鈥�)

return False

聽 聽 聽 璋冪敤鏃讹細multiple(13)聽 杩斿洖False

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 multiple(15, 5) 杩斿洖True

def sum_(*args):

s = 0

for n in args:

s += n

return s

璋冪敤锛歴um_(10, 20, 30 , 40, 50)

sum_(1, 5, 8)

聽 聽聽 **kwargs :聽 涓�鑸儏鍐典笅锛屽仛鍑芥暟鐨勫彲閫夐」

def openTxt(path, **kwargs):

mode = kwargs.get(鈥榤ode鈥�)

if not mode:

聽 聽 聽 聽 聽 聽 聽聽 mode = 鈥榬t鈥櫬� 聽 # 榛樿鍊�

charset = kwargs.get(鈥榗harset鈥�)

if not charset:

charset= 鈥榰tf-8鈥�

with open(path, mode, encoding=charset) as f:

c =聽 f.read()

print(c)

璋冪敤锛歰penTxt(r鈥檆:\aa.txt鈥�)

openTxt(r鈥檆:\aa.txt鈥�, charset=鈥榞bk鈥�)

openTxt(r鈥檆:\aa.txt鈥�,charset=鈥榞bk鈥�,mode=鈥榳+鈥�)

寮傚父澶勭悊锛�

try:

聽 聽 聽 聽聽 鍙兘鍑虹幇鐨勫紓甯�

except 寮傚父绫籥s 绫诲璞�:

聽 聽 聽 聽聽 澶勭悊寮傚父鐨勮鍙�

finally:

聽 聽 聽 聽 聽 鏈�缁堝鐞嗙殑璇彞

鎵撳紑鏂囦欢鏃跺彲鑳戒細鍑虹幇寮傚父锛孖OError

try:

聽 聽 聽 聽聽 f = open(r鈥檆:\aa.txt鈥�)聽 # 濡傛灉aa.txt鏂囦欢涓嶅瓨鍦�

except:

聽 聽 聽 聽聽 print(鈥樻枃浠朵笉瀛樺湪鈥�)

finally:

聽 聽 聽 聽聽 print(鈥樻枃浠舵搷浣滅粨鏉熲��)

聽 聽 def聽 chgInt(x):聽 # 灏嗗瓧绗︾殑x鍙傛暟鍊艰浆鎴恑nt绫诲瀷

try:

return int(x)

except:

聽 聽 聽 聽 聽 聽 print(鈥榹0}聽 涓嶈兘杞垚int鈥�.format(x))

return 0

鈥樷��.replace()

s.find()

鈥榮aa鈥�.split()

max()

open()

璋冪敤锛歝hgInt(鈥榓鈥�) == None聽 # 鎵撳嵃鈥榓鈥� 涓嶈兘杞垚int

聽 聽 聽 聽聽 chgInt(鈥�10鈥�) == 10聽 # 杩斿洖10

Sort 鎺掑簭

你可能感兴趣的:(鍑芥暟鐨勭畝鍗曞簲鐢�)