定义函数

def mail():
    n = 123
    n += 1
    print(n)

mail()
f = mail
f()
  • 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可
  • 面向对象:对函数进行分类和封装,让开发“更快更好更强...”

函数的定义主要有如下要点:

  • def:表示函数的关键字
  • 函数名:函数的名称,日后根据函数名调用函数
  • 函数体:函数中进行一系列的逻辑计算,如:发送邮件、计算出 [11,22,38,888,2]中的最大数等...
  • 参数:为函数体提供数据
  • 返回值:当函数执行完毕后,可以给调用者返回数据。

1、返回值

函数是一个功能块,该功能到底执行成功与否,需要通过返回值来告知调用者。

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def mail():
    ret = True
    try:
        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()
    except Exception:
        ret = False
    return ret#返回值

ret = mail()
if ret:
    print("发送成功")
else:
    print("发送失败")

一个函数里面没有出现return 、默认返回的是none 

遇到return 函数内容就终结了

def show():
    print('a')
    return [11,22]
    print('b')

ret = show()
print(ret)
输出:

a
[11, 22]

 参数:

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

def mail(user):#形参
    ret = True
    try:
        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', [user, ], msg.as_string())
        server.quit()
    except Exception:
        ret = False
    return ret#返回值

ret = mail('123456@qq.com')#实参
ret = mail('1234567@qq.com')#实参
ret = mail('12345678@qq.com')#实参
ret = mail('123456789@qq.com')#实参
if ret:
    print("发送成功")
else:
    print("发送失败")

 

#无参数
#   show():    -- > show()

#一个参数
"""
def show(arg):
    print(arg)
show("xiao")
"""

#两个参数
"""
def show(arg,arga):
    print(arg,arga)
show("xiao","long")
"""

"""
#默认参数,必须放在最后
def show(a1,a2=999):
    print(a1,a2)

#show(111)
show(111,888)
"""

#指定参数
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]

动态参数:

一个*号 转换成元组

def
show(*arg):#转换成元组 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")

输出:

{'n1': 78, 'n2': 99, 'n3': 'candy'} <class 'dict'>

 

强强联合:

#写参数的时候一个*放在前面、两个*放在后面
def show(*args,**kwargs): print(args,type(args)) print(kwargs,type(kwargs))
#传参数的时候也得按顺序
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 + 1 return b result = func(99) print(result) #lambda表达式,简单函数的表示方式 func = lambda a: a+1 #lambda a: a+1 是一个函数 #lambda 关键字 #a 形参 #: 函数:函数体 #a+1 执行,执行的结果自动加一个return值 #>>>创建一个形式参数 #>>>创建了函数内容, a+1 并把,结果return ret = func(99) print(ret)

输出:

100
100

 

#普通条件语句
if 1 == 1:
    name = "wupeiqi"
else:
    name = "xiaolong"
#三元运算
name = "wupeiqi" if 1==1 else "xiaolong"

 

posted @ 2017-07-04 20:22  MrZuo  阅读(229)  评论(0编辑  收藏  举报