Python零基礎入門講義:函式的用法
目錄:
1.定義、使用函式
1.函式定義:def
2.函式呼叫:例:myprint()
學習Python中有不明白推薦加入交流群
號:864573496
群裡有志同道合的小夥伴,互幫互助,
群裡有不錯的視訊學習教程和PDF!
3.函式可以當作一個值賦值給一個變數 例:a=myprint() a()
4.寫return return必須是最後一句,不寫return返回null
2.函式的引數
1.不帶引數的函式
2.帶固定引數的函式
3.帶可變引數的函式
4.帶關鍵字引數的函式
5.引數順序:固定、可變、關鍵字引數 #可變引數最多隻能有一個
3.變數的作用範圍
1.全域性變數:除在函式內宣告的變數都是全域性變數
2.區域性變數:當一個變數同時存在於函式內和函式外,函式內優先使用區域性變數
3.函式再呼叫之前,函式內部的變數必須再呼叫之前存在
4.global 改的是全域性變數
5.nonlocal用在巢狀函式,內層改外層
4.可變數與不變數
1.函式內部不能修改不變數,在內部只是把值傳進去,外部仍然是原來的值
2.可變數:列表、字典、集合
3.不變數:數字、字串、元組
5.匿名函式與高階函式
1.匿名函式:很短,只用一次
2.高階函式:
過濾:filter
遍歷:map
遞迴:reduce
排序:sorted
2.函式的引數
print("函式的定義:")
print("不帶引數的定義:")
def myfunc():
print("哈哈")
return1myfunc()
a=myprint()#先將myprint函式執行一遍,將返回值賦給 aprint("a的值:",a)print("a的型別",type(a))
b=myprint#b 即myprint函式b()#呼叫函式print("b的值:",b)print("b的型別",type(b))print("帶固定引數的定義:")def myfunc2(a1):
c=sum(a1)
print(c)
myfunc2([1,2])print("帶可變引數的定義:")defmyfunc3(*args):
c=sum(args)
print(c)
myfunc3()
myfunc3(1)
myfunc3(1,2,3)print("固定引數與可變引數:")defmyfunc4(a,*args):
print(a,args)
myfunc4("a","b")
myfunc4("a","b","c")print("固定引數、可變引數、關鍵字引數:")defmyfunc5(a,*args,k=2):
print(a,args,k)
myfunc5(1,2)
myfunc5(1,2,3,k=3)
3.變數的作用範圍
#全域性變數print("全域性變數:")
a="sss"foriin a:
m=iprint(a)print(m)print("區域性變數:")def mycount():
a=1 b=2print("區域性a",a)
returna+b
res=mycount()print(res)print("全域性a",a)print("函式內修改全域性變數:")
d=9a=3def myfunc(a1,a2):
globala#函式內部修改全域性變數a=4print("內部a",a)
returna1+a2print(myfunc(a,d))print("全域性a",a)print("巢狀函式變數:")def f1():
a=3def f2():
nonlocal a #只在巢狀函式中使用,內層修改外層print(a)
a=5 f2()
print(a)
f1()
4.可變數與不變數
a1="abc"b1=[1,2,3]print("不可變數字串與可變變數列表的用法")def f1(a,b):
a+="d"print(a)
b.append(4)
print(b)
f1(a1,b1)print("字串是不可變數",a1,"列表是可變數",b1)#字元是不可變數,在函式外值還是原來的值,列表是可變數,在函式內修改,函式外也跟著修改print("不可變數數字的用法:")
c1=66def f2(c):
c+=6print(c)
f2(c1)print("數字是不可變數",c1)print("可變變數集合的用法:")
d1={1,4}def f3(d):
d.add(7)
print(d)
f3(d1)print("可變變數集合",d1)print("可變數字典的用法:")
e1={1:"x",2:"y"}def f4(e):
e[1]="m"print(e)
f4(e1)print("可變數字典",e1)
5.匿名函式與高階函式
filter: filter(function,sequence),function可以是匿名函式或者自定義函式,它會對後面的sequence序列的每個元素判定是否符合函式條件,返回TRUE或者FALSE,從而只留下TRUE的元素;sequence可以是列表、元組或者字串
map:它接收一個函式 f 和一個 list,並通過把函式 f 依次作用在 list 的每個元素上,得到一個新的 list 並返回
reduce:reduce(function,iterable),其中function必須傳入兩個引數,iterable可以是列表或者元組
sorted:函式對所有可迭代的物件進行排序操作。
python 匿名函式與高階函式#匿名函式lambdaprint("匿名函式:")
f=lambdai:i*2print(f(3))
f2=lambdam,n:m*nprint(f2(2,4))print("高階函式:")#篩選filterprint("篩選filter:")
result=[("G211","5",""),
("D311","7",""),
("T411","","8"),
("K511","","6")
]print(list(filter(lambdai:i[0][0]in"GD",result)))print(list(filter(lambdai:i[0][0]notin"GD",result)))#遍歷mapprint("遍歷map:")
list(map(print,filter(lambdai:i[0][0]in"GD",result)))
list(map(print,filter(lambdai:i[0][0]notin"GD",result)))
list(map(lambdax: x ** 2, [1, 2, 3, 4, 5]))print(list(map(abs,[-1,2,-3,4])))#遞迴reduceprint("遞迴reduce:")fromfunctoolsimport reducedef add(x,y):
returnx+yprint(reduce(add,[1,2,3,4,5]))print(reduce(lambdai,j:i+j,[1,2,3,4,5]))#排序sortedprint("排序sorted:")print(sorted([1,5,7,3,-2]))print(sorted([1,5,7,3,-2],reverse=True))#reverse=True從大到小排序,預設從小到大l=[('a', 1), ('b', 2), ('d', 6), ('e', 4), ('c', 3)]print(sorted(l,key=lambda i:i[0]))print(sorted(l,key=lambdai:i[0],reverse=True))