zoukankan      html  css  js  c++  java
  • python学习——函数、内置函数

    函数返回值

    1、判断该函数是否有返回值:按住ctrl+函数名,如果是none则没有返回值

    函数中没有return就没有返回值,调用函数得到的结果就是None

    2、函数返回多个值,在return后加,用,隔开

    3、当函数执行到return后,会直接跳出函数返回结果(即:return后面的代码无意义)

    def func(a, b):
        c = a + b
        d = a - b
        return c, d
        # return之后不要写没有意义的代码,不会执行的
        # print("7890------")
        # print("-----7890")
    def fun(n):
        print(n**2)
    
    res=fun(8)
    print(res)
    
    结果:
    64
    None
    def fun(n):
        print(n**2)
        return n**2
    
    res=fun(8)
    print(res)
    
    结果:
    64
    64

    全局变量和局部变量

    全局变量:直接定义在python文件中的变量

    局部变量:定义在函数中的变量,只能在该函数内部调用

    在函数内部定义全局变量

    使用global进行声明

    def fun():
       global c
       c=100
    fun()

    函数的参数

    函数的参数:定义在函数后面的括号中
    定义的参数叫形参
    调用函数的时候传入的数据叫实参

    函数参数的分类:

    实参分类:
    1、位置传参:通过位置按顺序传递
    2、关键字传参:通过参数名指定参数进行传递

    形参的分类:
    1、必备参数/必需参数:定义几个就要传几个
    2、默认参数(缺省参数):可传可不传,不传的情况下使用默认值.
    3、不定长参数
    *args:接收0个或多个位置参数
    **kwargs:接收0个或多个关键字参数

    def func(*args, **kwargs):
        print(args)
        print(kwargs)
    
    func(11, 22, 33, aa=11, bb=22, c=33)
    
    结果:
    (11, 22, 33)
    {'aa': 11, 'bb': 22, 'c': 33}

    函数参数的拆包

    函数调用
    *:函数调用的时候可以用来对元组或列表拆包
    **:函数调用的时候可以用来对字典拆包

    def func(a, b, c):
        print(a)
        print(b)
        print(c)
    tu = (111, 222, 334)
    func(*tu)
    dic = {"a": 1, "b": 2, "c": 3}
    func(**dic)
    
    结果:
    111
    222
    334
    1
    2
    3

    内置函数

    1、获取最小值:min

    获取最大值:max
    求和:sum

    tu = (11, 222, 33, 44)
    li = [1, 2, 3, 4, 5]
    dic = {"a": 11, "b": 44, "c": 99}
    
    #min
    print(min(tu))
    print(min(li))
    print(min(dic.values()))
    
    #max
    print(max(tu))
    print(max((li)))
    print(max(dic.values()))
    
    
    #sum
    print(sum(tu))
    print(sum(li))
    print(sum(dic.values()))

    2、enumerate:获取数据的索引(位置)和值

    tu = (11, 222, 33, 44)
    li = [1, 2, 3, 4, 5]
    dic = {"a": 11, "b": 44, "c": 99}
    res = enumerate(tu)
    print(list(res))
    
    res2 = enumerate(li)
    print(list(res2))
    
    res3 = enumerate(dic)
    print(list(res3))
    
    结果:
    [(0, 11), (1, 222), (2, 33), (3, 44)]  (下标位置,值)
    [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
    [(0, 'a'), (1, 'b'), (2, 'c')]    字典获取出来的是key

    3、过滤器函数

    filter(参数1,参数2):
    参数1:函数(过滤的规则)
    参数2:需要过滤的数据
    filter会根据参数一函数的返回值是True还是False来决定改数据要不要过滤

    li = [11, 2, 22, 444, 56, 121, 324, 45, 11, 777, 3334, 664]
    
    # 需求:过滤出来大于50的数据?
    #for循环实现
    li2 = []
    for i in li:
        if i > 50:
            li2.append(i)
    
    print(li2)
    # 过滤器实现 def func(x): return x > 50 res = filter(func, li) print(res) print(list(res))

    结果:

    [444, 56, 121, 324, 777, 3334, 664]
    <filter object at 0x0000028C6ABD72B0>
    [444, 56, 121, 324, 777, 3334, 664]

    
    
    
    

    4、匿名函数

    语法:lambda 函数的参数:公式 (公式返回值为true、false)
    应用场景,一般用于非常简单的函数(函数内部只有一行代码),比如:结合过滤器使用(当成别的函数的参数)

    # 匿名函数 结合过滤器一起使用
    li = [11, 2, 22, 444, 56, 121, 324, 45, 11, 777, 3334, 664]
    res = filter(lambda x: x > 300, li)
    print(list(res))
    #把li2中li1有的元素过滤出来
    li1 = [1, 2, 3, 4]
    li2 = [1, 2, 32, 4, 5, 2, 5, 1, 3, 444, 2, 44, 2, 4, 5, 2, 4, 2]
    res = filter(lambda x: x in li1, li2)
    print(list(res))
    
    结果:[1, 2, 4, 2, 1, 3, 2, 2, 4, 2, 4, 2]
    # 过滤0-100能够被5正常的数据
    res = filter(lambda x: x % 5 == 0, range(101))
    print(list(res))
    
    结果:[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

    5、eval:能够识别字符串中的有效python表达式(等于去掉一层引号,非有效表达式则会报错)

    #  需求:s = "{"a":11,"c":111}",将s对应的数据,转换为字典类型
    
    s = '{"a":11,"c":111}'
    res = eval(s)
    print(res, type(res))
    
    结果:{'a': 11, 'c': 111} <class 'dict'>

    6、zip:聚合打包,zip只能强制转换一次

    # 将以下两个列表,组合为一个字典,title中的元素作为键,data中的元素作为值
    title = ["name", "age", "gender","info"]
    data = ["木森", 18, ""]
    #for循环实现 dic = {} for i in range(2): key = title[i] value = data[i] dic[key] = value print(dic)
    #zip实现 res = zip(title, data) res为zip类型对象,通过dic转为字典 print(dict(res)) 结果:{'name': '木森', 'age': 18, 'gender': ''}
    # 注意点 zip对象只能进行一次强制转换
    li1 = [11, 22, 33, 44]
    li2 = [1, 2, 3, 4, 5]
    li3 = [111, 222, 333, 44, 555]
    li4 = [11, 12, 13, 14, 15]
    
    res = zip(li1, li2)
    print(res)
    print(list(res))
    print(tuple(res))
    print(dict(res))
    
    结果:
    <zip object at 0x0000021E5914A608>
    [(11, 1), (22, 2), (33, 3), (44, 4)]
    ()
    {}
    li1 = [11, 22, 33, 44]
    li2 = [1, 2, 3, 4, 5]
    li3 = [111, 222, 333, 44, 555]
    li4 = [11, 12, 13, 14, 15]
    res = zip(li1,li2,li3,li4)
    
    list1 = list(res)
    print(tuple(list1))
    
    结果:((11, 1, 111, 11), (22, 2, 222, 12), (33, 3, 333, 13), (44, 4, 44, 14))
  • 相关阅读:
    dom4j 创建XML文件
    Convert.ToInt32()与int.Parse()的区别
    委托
    工厂模式
    策略模式
    大型网站架构演化
    字符串反转(面试)
    switch(面试)
    带宽计算
    新语法
  • 原文地址:https://www.cnblogs.com/erchun/p/12337567.html
Copyright © 2011-2022 走看看