python系列

python第一章(上)python第一章(下)python第二章python第三章(上)python第三章(下)python第四章(上)python第四章(下)


函数

  • python系列
  • 前言
  • 函数
  • 概念
  • 使用
  • 定义
  • 调用函数
  • 函数的参数
  • 默认值参数
  • 名称传递参数
  • 可变参数
  • 形参和实参
  • 返回多个值
  • lambda函数
  • sorted函数
  • 变量作用域
  • 局部变量
  • 全局变量
  • 同名
  • global声明
  • 递归函数
  • 综合应用



前言

  1. 函数的基本概念
  2. 函数的使用
  3. 综合应用

函数

  • 函数用来将复杂的问题分解为若干子问题,并逐个解决。
  • 一旦我们为一个特定任务编写了函数,就可以在任意时刻调用这个函数来完成这个特定的任务。
  • 使用函数有很多优点:
  • 减少程序中的代码重复量。
  • 把大而复杂的问题分解成小问题。
  • 有助于提升代码的整洁度,使代码更易于理解。
  • 使一段代码可以重复使用多次。

概念

  • round函数,功能是对一个浮点数进行四舍五入操作。
  • round定义:round( x [, n] ),我们可知:
    函数名:round。
    形式参数(形参):两个,x和n,其中n可以省略。
    函数返回值:浮点数x的四舍五入后的值。

使用

定义

def 函数名([参数列表]):
		函数体
  • 圆括号内是形参列表,如果有多个参数则使用逗号分隔开,即使该函数不需要接收任何参数,也必须保留一对空的圆括号。
  • 圆括号后的“:”必不可少。
  • 函数体相对于def关键字必须保持缩进。
  • 函数体中可以使用return语句返回值。return语句可以多条,在这种情况下,一旦第一条return得到执行,函数立即终止。return可以出现在函数体的任何位置。
#函数名:f
# 形参:a,b
# 返回值:a和b的和
def f(a,b):
    return a+b
# 函数名:m
# 形参:s
# 返回值:s的逆序串
def m(s):
    return s[::-1]
# 函数名:c
# 形参:lst
# 返回值:lst中所有元素的和
def c(list):
    return sum(list)
# 函数名:p
# 形参:无
# 返回值:无
def p():
    print('**************')
# 函数名:hi
# 形参:name:
# 返回值:无
def hi(name)
    print('hello'+name)

调用函数

函数名 ([实参列表])
  • 实参是在程序运行时,实际传递给函数的数据。实参列表必须与函数定义时的形参列表一一对应。参数的按位置传递,即按参数顺序,第一个实参的值传递给第一个形参,第二个实参的值传递给第二个形参,以此类推。
  • 函数如果有返回值,则可以在表达式中继续使用。如果函数没有返回值,则可以单独作为表达式语句使用。
#函数名:f
# 形参:a,b
# 返回值:a和b的和
def f(a,b):
    return a+b
# 函数名:m
# 形参:s
# 返回值:s的逆序串
def m(s):
    return s[::-1]
# 函数名:c
# 形参:lst
# 返回值:lst中所有元素的和
def c(list):
    return sum(list)
# 函数名:p
# 形参:无
# 返回值:无
def p():
    print('**************')
# 函数名:hi
# 形参:name
# 返回值:无
def hi(name):
    print('hello'+name)

print(f(12,32))#调用函数f,12传递给形参a,23传递给形参b
print(m('abcd'))#有返回值,返回的值在表达上继续使用,用print打印出来
print(c([1,2,4,5]))
p()#无返回值,可以作为单语句使用
print(p())#无返回值,不能用print返回结果,所以打印了None
hi('小琦')
print(hi('小琦'))
  • 没有返回值,会打印None
  • 运行截图

函数的参数

默认值参数

  • 在声明函数时,如果希望函数的一些参数是可选的,可以在声明函数时为这些参数指定默认值。调用该函数时,如果没有传入对应的实参值,则函数使用声明时指定的默认参数值。
def fun(word,times=1):
    print((word+"")*times)
fun('hello',3)
fun('trger')
  • 默认值参数必须写在形参列表的右边
  • 运行截图

名称传递参数

  • 函数调用时,实参默认按照位置顺序传递参数,按照位置传递的参数称为位置参数。函数调用时,也可以通过名称(关键字)指定传入的参数,按照名称指定传入的参数称为名称参数,也称为关键字参数。
  • 使用关键字参数具有三个优点:参数按名称意义明确;传递的参数与顺序无关;如果有多个可选参数,则可以选择指定某个参数值。
def mescore(mid_score,end_score,rate=0.4):
    score=mid_score*rate+end_score*(1-rate)
    return score
print(mescore(88,90))
print(mescore(mid_score=88,end_score=90,rate=0.5))
print(mescore(rate=0.5,end_score=90,mid_score=88))
  • 使用名称传递参数,就可以不按照参数的位置一一对应
  • 运行截图

可变参数

  • 在定义函数的时候,通过带星号的参数,例如*param1,允许向函数传递可变数量的参数。调用函数时,从该参数之后所有的参数都被收集为一个元组
def fun(*c):
    for i in c:
        print("{:^4}".format(i),end="")
    return len(c)
count=fun("李白","杜甫")
print("共{}人".format(count))
count=fun("李白","杜甫","李四","张三")
print("共{}人".format(count))
  • 运行截图
  • 在定义函数的时候,也可以通过带两个星号的参数,例如**param2,允许向函数传递可变数量的参数。调用函数时,从该参数之后所有的参数都被收集为一个字典。
def fun(**c):
    total=0
    print(c)
    for i in c:
        total+=c[i]
    return total
print(fun(g1=5,g2=85,g3=56,g4=20))
  • 运行截图

形参和实参

def fun(money,rate):
    money=money*(1+rate)
    return money
amount=100
rate=0.05
fun(amount,rate)
print(amount)
  • 运行截图

python round python中round函数的作用_pycharm

  • 这里发现amount的值没有改变,形参money只是接收到了实参amount的值,并不是访问实参,在函数修改了形参的值并不影响实参的值,
  • 相当于形参在函数里另外创建了一个变量money,并接收到了实参amount的值赋给了money,而实参amount是在函数外的一个变量,这两个的地址不是同一个,所以money怎么改变都是改变自己的值,并不影响amount的值,
def fun(money,rate):
    for i in range(len(money)):
        money[i]=money[i]*(1+rate)
amount=[1200,1400,1600,1700]
rate=0.05
fun(amount,rate)
print(amount)
  • 运行截图
  • 这里发现amount的值改变了,这是因为当实参是列表类型时,形参将会是该列表的引用,而不是创建新列表,所以在函数中可以直接修改列表中元素的值。相当于形参和实参指的是同一个列表
  • 因此,我们也称列表list为可变对象,而整数int,浮点float,字符串str和布尔类型bool为不可变对象。我们学过的字典也是可变对象,可以直接在函数中修改字典元素的值。

返回多个值

  • 在函数体中使用return语句,可以从函数跳出并返回一个值。如果需要返回多个值,则可以返回一个元组
def fun(a,b):
    return (a,b,a//b,a%b)
n1,n2,m,d=fun(6,4)
print("两个整数时{}和{}".format(n1,n2))
print("它们的商是",m)
print("余数是",d)
  • 运行截图

lambda函数

  • lambda函数是一种简便的,在同一行定义函数的方法。lambda实际上是生成一个函数对象,即匿名函数,它广泛用于需要函数对象作为参数或函数比较简单并且只使用一次的场合。
  • lambda函数的定义格式:
lambda 参数1,参数2,…… :<函数语句>
f=lambda x,y:x*y#返回参数x和y的乘积
print(f(3,5))
n=lambda a,b,c:(a+b+c)/3#返回参数a, b, c的平均值
print(n(3,5,8))
  • 运行截图
  • python round python中round函数的作用_全局变量_02

  • 理用lambda函数输出列表中所有负数
f = lambda x: x<0
list = [3, 5, -7, 4, -1, 0, -9]
for i in filter(f, list):
    print(i)
  • 运截图
  • python round python中round函数的作用_python round_03

  • 这里的filter()函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。filter()接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判断,然后返回True或False,最后将返回True的元素放到新列表中。
  • 简化如下
list = [3, 5, -7, 4, -1, 0, -9]
for i in filter(lambda x: x<0, list):
    print(i)

sorted函数

  • sorted函数中有一个默认值参数key,key可以在排序时指定用迭代对象元素的某个属性或函数作为排序关键字,再来看两个例子。
  1. 有列表 “list = [-2,7,-3,2,9,-1,0,4]“,需要按照列表中每个元素的平方值排序,可以写语句:
list = [-2,7,-3,2,9,-1,0,4]
print(sorted(list, key = lambda x:x*x))
  • 运行截图
  1. 有列表 “list = [“their”, “are”, “this”, “they”, “is”] “,需要按照列表中每个元素的长度值排序,可以写语句:
list = ["their", "are", "this", "they", "is"]
print(sorted(list, key = lambda x:len(x)))
  • 运行截图
  1. 利用lambda函数对字典元素按值或按键排序。
dict_data = {"化1704":33, "化1702":28, "化1701":34, "化1703":30}
print(sorted(dict_data))
print(sorted(dict_data.items()))
print(sorted(dict_data.items(),key = lambda x:x[1]))
print(sorted(dict_data.items(),key = lambda x:x[1]%10))
  • 运行截图

变量作用域

  • 一个程序的所有变量并不是在任何位置都可以访问的。访问权限决定于这个变量是在哪里赋值的。
  • 每个变量都有自己的作用域(命名空间),变量的作用域决定了在某些代码段内使用该变量是合法的,在某些代码段内使用该变量是非法的。
  • 两种最基本的变量作用域是**“全局变量”“局部变量”**。

局部变量

def f():
    x=10
    return x*x
print(f())
  • 运行截图
  • 变量x是局部变量,它的作用域在函数f()的内部。
def f():
    x=10
    return x*x
print(x)
  • 运行截图
  • 在函数f()外部访问x变量的时候,超出x的作用域范围,因此代码报错。

全局变量

def f():
    x=10#局部变量
    return x*x
x=1000#全局变量
print(x)
  • 运行截图
  • 第一个定义的x作用域只在函数f()的内部,第二个定义的x作用域在函数f()的外部,它是全局变量,全局可见,所以print语句只能访问到等于1000的那个x,代码的输出结果应该是1000。
def f():
    return x*x
x=10
print(f())
print(x)
  • 代码中只有一个x变量,它在函数之外赋值,因此是全局变量,全局可见。在函数f()内当然也可以访问这个全局的x,所以代码运行后会得到结果:
    100
    10
  • 运行截图
  • python round python中round函数的作用_python round_04

同名

def f():
    x=5
    print("f内部:x=",x)
    return x*x
x=10
print("f()=",f())
print("f外部:x=",x)
  • 运行截图
  • 这里的“x=5”和“x=10”变量名称虽然一样,但作用域不同。前者是局部变量,后者是全局变量。
  • 当执行第一个print语句的时候,既能访问局部的x,也能访问全局的x,在这种情况下,采用局部优先的原则:在局部变量(包括形参)和全局变量同名的时候,局部变量屏蔽全局变量。

global声明

def f():
    global x
    x=5#访问的是全局变量的x,因此把全局变量的x改为5
    print("f内部:x=", x)
    return x * x
x=10#全局变量x
print("f()=",f())
print("f外部:x=",x)
  • 运行截图
  • 在代码中,由于有“global x”这句话,第三行访问的x为全局变量x,第三行将全局变量x重新赋值为5,运行代码,结果为:

递归函数

  • Python中,一个函数既可以调用另一个函数,也可以调用它自己。如果一个函数调用了它们自己,就称为递归。
def fact(n):
    if n == 1: return 1
    else: return n * fact(n-1)#调用自己

for i in range(1, 9+1):        # 输出1—9的阶乘
    print("{}! =".format(i), fact(i))
  • 运行截图
  • python round python中round函数的作用_全局变量_05

  • 每个递归函数必须包括两个主要部分。
  • 终止条件。表示递归的结束条件,用于返回函数值,不再递归调用。在fact()函数中,递归的结束条件为“n == 1”。
  • 递归步骤。递归步骤把第n步的函数与第n-1步的函数关联,对于fact()函数,其递归步骤为“n * fact(n-1)”。即把求n的阶乘转化为求n-1的阶乘。
    `

综合应用

  1. 求斐波拉且数列
def fibo(n):
    if n == 1 or n == 2: return 1
    else: return fibo(n-1) + fibo(n-2)

for i in range(1, 20+1):
    print("{:>8}".format(fibo(i)), end="  " if i%5!=0 else "\n")
  • 运行截图
  1. 编写函数,接收任意多的实数,返回一个元组,其中第一个元素为所有参数的平均值,其他元素为所有参数中大于平均值的实数。
def fun(*para):
    avg=sum(para)/len(para)
    g=[i for i in para if i>avg]
    return (avg,g)
m,l=fun(6.7,2.4,-0.1,2.15,-5.8)
print("平均值:",m)
print("大于均值的数:",l)
  • 运行截图
  1. 编写函数,提取短语的首字母缩略词。缩略词是一个单词,是从短语中每一个单词取首字母组合而成的,且要求大写。例如:“very important person”的缩略词是“VIP”。
def f(s):
    lst=s.split()
    return [x[0].upper() for x in lst]
s=input("输入短语:")
print("".join(f(s)))
  • 运行截图
  1. 小明做打字测试,请编写程序计算小明输入字符串的准确率。
    (假设原始字符串为origin,小明输入的字符串为userInput,如果两个字符串的长度不一致,提示小明重新输入,如果长度一致,进行字符匹配。准确率为正确的字符个数除以字符总个数 )
def rate(origin,userInput):
    right=0
    for origin_char,user_char in zip(origin,userInput):
        if origin_char==user_char:
            right+=1
    return right/len(origin)
origin='Your smile will make my whole world bright.'
print(origin)
userInput=input("输入:")
if len(origin)!=len(userInput):
    print("字符长度不一致,请重新输入")
else:
    print("准确率为:{:.2%}".format(rate(origin,userInput)))
  • 第3行中zip函数的作用是将对应元素打包成一个个元组,然后返回由这些元组组成的对象。例如:

s1 = ‘ab’
s2 = ‘xy’
list(zip(s1,s2))# 将zip对象转换成列表 [(‘a’, ‘x’), (‘b’, ‘y’)]

  • 运行截图
  1. 输入一段英文文本,统计出现频率最高的前10个单词(去除of,a,the等无意义词语)。
    (待处理的英文文本如下:“I have a dream today!I have a dream that one day every valley shall be exalted, and every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight; “and the glory of the Lord shall be revealed and all flesh shall see it together.” This is our hope, and this is the faith that I go back to the South with.With this faith, we will be able to hew out of the mountain of despair a stone of hope. With this faith, we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. With this faith, we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom
    together, knowing that we will be free one day.”)
  • 观察这段文本,字母有大写有小写,还有一些标点符号,因此在统计单词出现频率之前我们需要先解决文本中大小写和标点符号的问题。
def getText(text):
    text=text.lower()
    for ch in "\,.:?;'":
        text=text.replace(ch," ")
    return text

def wordFreq(text,topn):
    words=text.split()
    counts={}
    for word in words:
        counts[word]=counts.get(word,0)+1
    excludes={'the','and','to','of','a','be'}
    for word in excludes:
     del(counts[word])
    items=list(counts.items())
    items.sort(key=lambda x:x[1],reverse=True)
    return items[:topn]
test='''I have a dream today!I have a dream that one day every valley shall be exalted, and every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight; "and the glory of the Lord shall be revealed and all flesh shall see it together." This is our hope, and this is the faith that I go back to the South with.With this faith, we will be able to hew out of the mountain of despair a stone of hope. With this faith, we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. With this faith, we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom
together, knowing that we will be free one day.'''
test=getText(test)
for word,freq in wordFreq(test,20):
    print("{:<10}{:>}".format(word,freq))
print("统计结束")
  • 运行截图