定义函数
def mail():n = 123n += 1print(n)mail()
f = mail
f()
輸出:
輸出:
[11, 22, 33, 44]
動態(tài)參數(shù):
一個(gè)*號 轉(zhuǎn)換成元組
def show(*arg):#轉(zhuǎn)換成元組print(arg,type(arg))show(11,22,33,44,55)
輸出:
(11, 22, 33, 44, 55) <class 'tuple'>
def show(**arg):print(arg,type(arg))show(n1 = 78,n2 = 99,n3 = "candy")
輸出:
#寫參數(shù)的時(shí)候一個(gè)*放在前面、兩個(gè)*放在后面 def show(*args,**kwargs):print(args,type(args))print(kwargs,type(kwargs)) #傳參數(shù)的時(shí)候也得按順序 show(11,22,33,44,n1=88,xiaolong = "candy")
輸出:
def show(*args,**kwargs):print(args,type(args))print(kwargs,type(kwargs))#show(11,22,33,44,n1=88,xiaolong='candy') l = [11,22,33,44] d = {'n1':88,'xiaolong':'candy'} #show(l,d) show(*l,**d)
輸出:
例:
s1 ="{0} is {1}" #result = s1.format("xiaolong","boy") l = ["xiaolong","boy"] result = s1.format(*l) print(result)
輸出:
s1 = "{name} is {acter}" d ={'name':'xiaolong','acter':'boy'} #result = s1.format(name = "xiaolong",acter = "boy") result = s1.format(**d) print(result)
輸出:
xiaolong is boy
def func(a):b = a + 1return b result = func(99) print(result)#lambda表達(dá)式,簡單函數(shù)的表示方式 func = lambda a: a+1 #lambda a: a+1 是一個(gè)函數(shù) #lambda 關(guān)鍵字 #a 形參 #: 函數(shù):函數(shù)體 #a+1 執(zhí)行,執(zhí)行的結(jié)果自動加一個(gè)return值#>>>創(chuàng)建一個(gè)形式參數(shù) #>>>創(chuàng)建了函數(shù)內(nèi)容, a+1 并把,結(jié)果return ret = func(99) print(ret)
輸出:
- 函數(shù)式:將某功能代碼封裝到函數(shù)中,日后便無需重復(fù)編寫,僅調(diào)用函數(shù)即可
- 面向?qū)ο?#xff1a;對函數(shù)進(jìn)行分類和封裝,讓開發(fā)“更快更好更強(qiáng)...”
函數(shù)的定義主要有如下要點(diǎn):
- def:表示函數(shù)的關(guān)鍵字
- 函數(shù)名:函數(shù)的名稱,日后根據(jù)函數(shù)名調(diào)用函數(shù)
- 函數(shù)體:函數(shù)中進(jìn)行一系列的邏輯計(jì)算,如:發(fā)送郵件、計(jì)算出 [11,22,38,888,2]中的最大數(shù)等...
- 參數(shù):為函數(shù)體提供數(shù)據(jù)
- 返回值:當(dāng)函數(shù)執(zhí)行完畢后,可以給調(diào)用者返回?cái)?shù)據(jù)。
1、返回值
函數(shù)是一個(gè)功能塊,該功能到底執(zhí)行成功與否,需要通過返回值來告知調(diào)用者。
import smtplib from email.mime.text import MIMEText from email.utils import formataddr def mail():ret = Truetry:msg = MIMEText('郵件內(nèi)容', 'plain', 'utf-8')msg['From'] = formataddr(["武沛齊", 'wptawy@126.com'])msg['To'] = formataddr(["走人", '424662508@qq.com'])msg['Subject'] = "主題"server = smtplib.SMTP("smtp.126.com", 25)server.login("wptawy@126.com", "郵箱密碼")server.sendmail('wptawy@126.com', ['424662508@qq.com', ], msg.as_string())server.quit()except Exception:ret = Falsereturn ret#返回值 ret = mail() if ret:print("發(fā)送成功") else:print("發(fā)送失敗")一個(gè)函數(shù)里面沒有出現(xiàn)return 、默認(rèn)返回的是none?
遇到return 函數(shù)內(nèi)容就終結(jié)了
def show():print('a')return [11,22]print('b')ret = show() print(ret)輸出:
a
[11, 22]
?參數(shù):
import smtplib from email.mime.text import MIMEText from email.utils import formataddrdef mail(user):#形參ret = Truetry:msg = MIMEText('郵件內(nèi)容', 'plain', 'utf-8')msg['From'] = formataddr(["武沛齊", 'wptawy@126.com'])msg['To'] = formataddr(["走人", '424662508@qq.com'])msg['Subject'] = "主題"server = smtplib.SMTP("smtp.126.com", 25)server.login("wptawy@126.com", "郵箱密碼")server.sendmail('wptawy@126.com', [user, ], msg.as_string())server.quit()except Exception:ret = Falsereturn ret#返回值 ret = mail('123456@qq.com')#實(shí)參 ret = mail('1234567@qq.com')#實(shí)參 ret = mail('12345678@qq.com')#實(shí)參 ret = mail('123456789@qq.com')#實(shí)參 if ret:print("發(fā)送成功") else:print("發(fā)送失敗")?
#無參數(shù) # show(): -- > show()#一個(gè)參數(shù) """ def show(arg):print(arg) show("xiao") """#兩個(gè)參數(shù) """ def show(arg,arga):print(arg,arga) show("xiao","long") """""" #默認(rèn)參數(shù),必須放在最后 def show(a1,a2=999):print(a1,a2)#show(111) show(111,888) """#指定參數(shù) def show(a1,a2):print(a1,a2)show(a2 =123, a1=999)?
def show(arg):print(arg)n =[11,22,33,44] show(n)輸出:
[11, 22, 33, 44]
動態(tài)參數(shù):
一個(gè)*號 轉(zhuǎn)換成元組
def show(*arg):#轉(zhuǎn)換成元組print(arg,type(arg))show(11,22,33,44,55)
輸出:
(11, 22, 33, 44, 55) <class 'tuple'>
?
兩個(gè)*轉(zhuǎn)換成字典:def show(**arg):print(arg,type(arg))show(n1 = 78,n2 = 99,n3 = "candy")
輸出:
{'n1': 78, 'n2': 99, 'n3': 'candy'} <class 'dict'>
?
強(qiáng)強(qiáng)聯(lián)合:#寫參數(shù)的時(shí)候一個(gè)*放在前面、兩個(gè)*放在后面 def show(*args,**kwargs):print(args,type(args))print(kwargs,type(kwargs)) #傳參數(shù)的時(shí)候也得按順序 show(11,22,33,44,n1=88,xiaolong = "candy")
輸出:
(11, 22, 33, 44) <class 'tuple'>
{'n1': 88, 'xiaolong': 'candy'} <class 'dict'>
?
注意:def show(*args,**kwargs):print(args,type(args))print(kwargs,type(kwargs))#show(11,22,33,44,n1=88,xiaolong='candy') l = [11,22,33,44] d = {'n1':88,'xiaolong':'candy'} #show(l,d) show(*l,**d)
輸出:
(11, 22, 33, 44) <class 'tuple'>
{'n1': 88, 'xiaolong': 'candy'} <class 'dict'>
?
字符串的格式化:例:
s1 ="{0} is {1}" #result = s1.format("xiaolong","boy") l = ["xiaolong","boy"] result = s1.format(*l) print(result)
輸出:
xiaolong is boy
?
字符串的格式化: 例:s1 = "{name} is {acter}" d ={'name':'xiaolong','acter':'boy'} #result = s1.format(name = "xiaolong",acter = "boy") result = s1.format(**d) print(result)
輸出:
xiaolong is boy
?
例:def func(a):b = a + 1return b result = func(99) print(result)#lambda表達(dá)式,簡單函數(shù)的表示方式 func = lambda a: a+1 #lambda a: a+1 是一個(gè)函數(shù) #lambda 關(guān)鍵字 #a 形參 #: 函數(shù):函數(shù)體 #a+1 執(zhí)行,執(zhí)行的結(jié)果自動加一個(gè)return值#>>>創(chuàng)建一個(gè)形式參數(shù) #>>>創(chuàng)建了函數(shù)內(nèi)容, a+1 并把,結(jié)果return ret = func(99) print(ret)
輸出:
100
100
?
#普通條件語句 if 1 == 1:name = "wupeiqi" else:name = "xiaolong" #三元運(yùn)算 name = "wupeiqi" if 1==1 else "xiaolong"?
轉(zhuǎn)載于:https://www.cnblogs.com/mrzuo/p/7118367.html
總結(jié)
- 上一篇: 【jQuery】复选框的全选、反选,推断
- 下一篇: matlab中fprintf函数的具体使