Python3 笔记 8 Python函数

8-1 认识函数

print()就是一个函数
a = 1.12564
print(round(a,2))

在命令行中查看内置函数的方法:
C:\Users\Tai Park\Documents\python>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

函数要求:
1、功能性
2、隐藏细节
3、避免编写重复的代码

8-2 函数的定义及运行特点

函数基本定义:
def funcname(parameter_list):
    pass

1、参数列表可以没有
2、可以return value

实现两个数字的相加的函数/打印输入的参数:
def add (x,y):
    result = x + y
    return result

def print_code(code):
    print(code)    #没有return所有最后打印出None

a = add(1,2)
b = print_code('python')
print(a,b)

#python
#3 None

设置系统递归的最大层数
import sys
sys.setrecursionlimit(1000)

8-3 如何让函数返回多个结果

函数遇到return后结束
对返回类型没有要求

返回多个结果自动以元组形式返回:
def damage(skill1,skill2):
damage1 = skill1 + 3
damage2 = skill2 * 3 + 10
return damage1,damage2

damages = damage(3,6)
print(type(damages))

#<class 'tuple'>

建议使用这种调用的方式,用有意义的名称解包:
def damage(skill1,skill2):
    damage1 = skill1 + 3
    damage2 = skill2 * 3 + 10
    return damage1,damage2

skill1_damage,skill2_damage = damage(3,6)    #序列解包
print(skill1_damage,skill2_damage)

8-4 序列解包与链式赋值 

序列解包:
a,b,c = 1,2,3    #既保持精简又保证可阅读性
d = 1,2,3    #可以一个变量赋多值,反向操作便为序列解包
print(type(d))
a,b,c = d    #解包,个数要相等

若a =1,b= 1,c =1
a = b = c = 1

8-5 必须参数与关键字参数

def add (x,y):    #x,y为形参
     result = x + y
     return result

a = add(1,2)    #1和2为实参

1、必须参数:参数是必须要传递,不传递就要报错
2、关键字参数:
c = add(y =  3, x = 2)     #可以明确指定

8-6 默认参数 

3、默认参数:
def add (x = 1,y = 2):       #默认参数,若没有设置默认参数就一定要传值进来
     result = x + y
     return result
非默认参数不能放在默认参数之后。
可以用关键参数标明,与默认参数结合,可以违背形参的顺序。
默认值参数和必须参数也不能混合调用

8-7 可变参数

4、可变参数:
def demo(*param):    #可变参数列表
    print(param)
    print(type(param))

demo(1,2,3,4,5,6)    #这里不用传元组了,不然会成为二维元组
#(1, 2, 3, 4, 5, 6)
#<class 'tuple'>

传入元组平铺:
def demo(*param):
    print(param)
    print(type(param))
a = (1,2,3,4,5,6)
demo(*a)    #传入*a
*a的作用是把元组中元素平铺出来

可以与必须参数结合,不过必须参数要放在前面
def demo(param1,param2 = 2,*param3):
    print(param1)
    print(param2)
    print(param3)

demo('a',1,2,3)
#a
#1    #没有办法跳过默认参数
#(2, 3)

可变参数直接涵盖了后面所有的值:
def demo(param1,*param3,param2 = 2):
    print(param1)
    print(param3)
    print(param2)

demo('a',1,2,3,'param')
#a    
#(1, 2, 3, 'param')    #可变参数直接涵盖了后面所有的值
#2        #默认值

用关键字参数可以赋值:
def demo(param1,*param3,param2 = 2):
    print(param1)
    print(param3)
    print(param2)

demo('a',1,2,3,param2 = 'param')    #用关键字参数可以赋值

8-8 关键字可变参数

求平方和:
def squsum(*param):
    sum = 0
    for i in param:    #可变参数绝大多数情况会使用for循环遍历可变参数列表里的每个参数
        sum += i * i
    print(sum)

squsum(1,2,3,4,5,6)

形参列表可以支持任意个数的关键字参数:
def city_temp(**param):    #两个*
print(param)
print(type(param))

city_temp(bj = '23c',xm = '24c',sh = '30c')
#'bj': '23c', 'xm': '24c', 'sh': '30c'}
#class 'dict'>    #输出字典类型

快速遍历字典:
def city_temp(**param):
    for key,value in param.items():    #快速遍历字典
        print(key,':',value)
a = {'bj':'32c','sh':'30c'}
city_temp(**a)    #双*传入字典

可变参数可以什么值都不传入,得到空的元组/字典

8-9 变量作用域 

c = 50

def add(x,y):
    c = x + y    #局部变量
    print(c)

add(1,2)
print(c)
#3
#50
函数中变量的作用域只在函数中,和函数外的c不是一个c
函数外部作用域覆盖整个文件:
c = 10    #全局变量
def demo():
    print(c)

demo()
#10
局部变量是相对的概念
在for循环外部是可以引用for循环内部的变量的,因为python没有块级作用域的概念
def demo():
    c = 50
    for i in range(0,9):
        a = 'a'
        c += 1
    print(c)
    print(a)    #可以引入for内的变量

demo()

函数中可以定义函数:
c = 1

def func1():
    c = 2
    def func2():
        c = 3
        print(c)
    func2()
func1()
#3

8-10 作用域链 

python的作用域有链式的特性,叫做作用域链,是最基本的变量引用的法则
c = 1

def func1():
     c = 2
     def func2():
         # c = 3
          print(c)
     func2()
func1()
#2

8-11 global关键字

全局变量不仅可以在模块内部使用,在整个应用程序中都能用。
把函数内部的局部变量转变为全局变量global:
def demo():
    global c
    c = 2
demo()    #先调用函数后就能够将c变为全局变量
print(c)



我的个人主页: 点击打开链接

猜你喜欢

转载自blog.csdn.net/qq_36329973/article/details/81021079