1.参数
函数的核心是参数传值,其次是返回值,熟练这两这个技术即可灵活使用函数。
1>普通参数
def show(name):
print(name)
show('tom')
2>默认参数
def show(name,age=18)
print("%s,%s"%(name,age))
show('tom',19)
show('tom')
3>动态参数-序列
def show(*args):
print(args)
show(11,22,33)
li=[11,22,33,44]
show(*li)
4>动态参数-字典
def show(**kwargs):
print(args)
show(name='tom',age=18)
d={name='tom',age=18,sex=male}
show(**d)
5>动态参数-序列字典
def show(*args,**kwargs):
print(args)
print(kwargs)
6>example
#example *
s1 ="{0} is {1}"
l=['alex','teacher']
#result=s1.format('alex','teacher')
result=s1.format(*l)
print(result)
#example **
s1 = "{name} is {acter}"
d = {'name':'alex','acter':'teacher'}
#result= s1.format(name='alex',acter='teacher')
result=s1.format(**d)
print(result)
7>扩展:邮件发送实列
import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddr msg = MIMEText('邮件内容', '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()
|
8>lambda(函数的简单表达方式)
def func(a):
a +=1
return a
result=func(4)
print(result)
f= lambda a: a+1
ret = f(99)
print(ret)