python中的函数
1.函数的目的
1.避免代码冗余2.增强可读性
2.函数的定义与调用
#定义函数 使用关键字def
"""1.定义函数:
def 函数名():
函数体"""
defsayhello():print("hello")"""2.调用函数:
函数名()"""sayhello()
3.函数返回值
#函数的返回值
"""1.什么都不写的情况下没有返回值
2.return # 没有返回值 # 结束函数
3.return None
4.return 变量"""
defsay_hello1():print("hello")print(say_hello1())defsay_hello2():print("hello")return
print(say_hello2())defsay_hello3():print("hello")returnNoneprint(say_hello3())defsay_hello4():print("hello")return "hello"
print(say_hello4())
序列解压
#序列的解压
a, b, c, d = (1, 2, 3, 4)print(a, b, c, d) #1 2 3 4
a, _, _, d= (1, 2, 3, 4)print(a, d) #1 4
a,*_ = (1, 2, 3, 4)*_, d = (1, 2, 3, 4)print(a, d) #1 4#字典、列表、集合同样适用
a, b = {'name': 'eva', 'age': 18}print(a, b) #age name
多个返回值
defdemo():"""多个返回值情况"""
return 1, 2, 3, 'a'
"""多个返回值,用一个变量接收时,得到的是一个元祖"""a&