1、函数的定义
- 定义函数:如果不主动调用函数,函数不会执行
def say_hello():
print('hello1')
print('hello2')
print('hello3')
##函数的调用
say_hello()
运行结果:
hello1
hello2
hello3
- 函数的嵌套
def linux():
print('is linux')
def python():
print('is python')
python()
linux()
运行结果:
is linux
is python
- 形参:定义函数的时候的变量 叫作形参(形参可以任意起名)
- 实参:真实的数据,调用函数时传递的参数 实参
def welcome(a): ##形参
print('hello',a)
welcome('lily') #实参
welcome('coco')
运行结果:
hello lily
hello coco
2、函数的四大参数
参数:{形参,实参}
形参:{位置参数,默认参数,可变参数,关键字参数}
- 位置参数:形参和实参个数必须保持一致,默认按位置传参
def getInfo(name,age):
print(name,age)
getInfo('westos',11)
getInfo(11,'westos')
getInfo(age = 11,name='westos')
运行结果:
westos 11
11 westos
westos 11
- 默认参数:形参和实参可以不一致(实参的优先级大于形参)
def mypow(x,y=2):
print(x ** y)
mypow(4,3)
运行结果:
64
- 可变参数
def mysum(*args): #*args:可变参数;args:是元组数据类型
sum = 0
for item in a:
sum += item
print(sum)
mysum(1,2,3,4,5,6,7,8,9)
运行结果:
45
- 关键字参数
def getStuInfo(name,age,**kwargs): #**kwargs是一个字典,可以传递任意多的key-value
print(name,age)
print(kwargs)
getStuInfo('westos','19',hobbies=['coding','running'],gender='female'
运行结果:
westos 19
{'hobbies': ['coding', 'running'], 'gender': 'female'}
3、函数的返回值
返回值:函数运算的结果,还需要进一步操作时,给一个返回值;
return 用来返回函数执行结果,如果没有返回值,默认为None;
python 可以间接返回多个值(返回一个元组)
一旦遇到return,函数执行结束,后面的代码不会执行
- 例1:没有返回值
def mypow(x,y=2):
print(x ** y,x+y)
print('hello')
a=mypow(3)
print(a)
运行结果:
9 5
hello
None
- 例2:有返回值
def mypow(x,y=2):
print(x ** y,x+y)
return x ** y,x+ y
print('hello') #此处代码未读到
a=mypow(3)
print(a)
运行结果:
9 5
(9, 5)
4、函数的示例练习
- 例1
"""
# _*_coding:utf-8 _*_
Name:函数_03.py
Date:4/3/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
编写一个函数cacluate, 可以接收任意多个参数,返回的是一个元组.
元组的第一个值为所有参数的平均值, 第二个值是大于平均值的所有数.
"""
def cacluate(ave,num,*args):
ave = sum(args)/len(args)
num = []
for i in args:
if i > ave:
num.append(i)
return ave,num
a=cacluate(1,3,5,7,9,23,15)
print(a)
运行结果:
(11.8, [23, 15])
- 例2
"""
# _*_coding:utf-8 _*_
Name:函数_04.py
Date:4/3/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
编写一个函数, 接收字符串参数, 返回一个元组,'hello WORLD'
元组的第一个值为大写字母的个数, 第二个值为小写字母个数.
"""
def function01(a):
big_count = 0
low_count = 0
for i in a:
if i.isupper():
big_count += 1
elif i.islower():
low_count += 1
else:
continue
return big_count,low_count
a = function01(input('输入:'))
print(a)
运行结果:
输入:hello WORLD
(5, 5)
- 例3
"""
# _*_coding:utf-8 _*_
Name:函数_05.py
Date:4/3/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
编写函数, 接收一个列表(包含30个1~100之间的随机整形数)和一个整形数k, 返回一个新列表.
函数需求:
- 将列表下标k之前对应(不包含k)的元素逆序;
- 将下标k及之后的元素逆序;
[1,2,3,4,5] 2 [2,1,5,4,3]
"""
import random
li = []
for i in range(30):
li.append(random.randint(1,100))
print(li)
def fun_01(list,k):
if k<0 or k>len(list):
return '该k值无效'
return list[:k][::-1] + list[k:][::-1]
print(fun_01(li,10))
运行结果:
[40, 58, 93, 32, 36, 66, 25, 28, 46, 81, 84, 77, 64, 2, 20, 5, 16, 21, 43, 71, 84, 45, 23, 59, 71, 3, 15, 5, 60, 2]
[81, 46, 28, 25, 66, 36, 32, 93, 58, 40, 2, 60, 5, 15, 3, 71, 59, 23, 45, 84, 71, 43, 21, 16, 5, 20, 2, 64, 77, 84]
- 第二种方法
import random
def list_2():
list = []
for i in range(30):
a=random.randint(1,100)
list.append(a)
b=random.randint(1,30)
print(list,b)
return list[:b][::-1],list[b:][::-1]
v=list_2()
print(v)