zoukankan      html  css  js  c++  java
  • python学习-23 函数

    函数

    1.函数分为:数学定义的函数和编程语言中的函数

    例如: - 数学定义的函数:y=2*x+1

                - 编程语言的函数:

                   def test(x):

                              x += 1

                             return x     

                  def  :定义函数的关键字

                  test :函数名

                  ():定义参数

                 return : 返回函数

    def test(x):
        x += 1
        return x
    print(test(1))

    运行结果:

    2
    
    Process finished with exit code 0

    2.函数的过程

    没有返回值return的函数

    def test():
        x = 1
        print(x)
    print(test())

    运行结果:

    1
    None
    
    Process finished with exit code 0

    返回值数=0  返回none

    返回值数=1 返回object

    返回值数>1 返回tuple

    3.函数的参数

    -----

    def test(x,y):                     #   x,y 是形参: 不占内存空间
        a = x**y
        return a
    
    b=(123,321)                       # 123,321是实参
    print(b)

    ---- 对应

    def test(x,type='mysql'):
        print(x)
        print(type)
    test('hello')
    test('hey','sql')

    运行结果:

    hello
    mysql
    hey
    sql
    
    Process finished with exit code 0

    -- 参数组

    第一种:

    def test(x,*args):
        print(x)
        print(args)
        print(args[1])          # 通过索引获取test元素的字符
    test(1,2,4,3)

    运算结果:

    1
    (2, 4, 3)
    4
    
    Process finished with exit code 0

    第二种:

    def test(x,**kwargs):
        print(x)
        print(kwargs)
    
    test(1,a=1,b=2)

    运行结果:

    1
    {'a': 1, 'b': 2}
    
    Process finished with exit code 0

    第三种:

    def test(x,*args,**kwargs):
        print(x)
        print(args)
        print(kwargs)
    
    test(1,'abc',333,name='john',age=18)

    运行结果:

    1
    ('abc', 333)
    {'name': 'john', 'age': 18}
    
    Process finished with exit code 0
  • 相关阅读:
    HDU 3681 Prison Break(状态压缩dp + BFS)
    POJ 2711 Regular Words(DP + 高精度)
    ZOJ 2745 01-K Code(DP)(转)
    DAG模型——硬币问题
    HDU 1619 Unidirectional TSP(单向TSP + 路径打印)
    DAG模型——嵌套矩阵
    HDU 4294 A Famous Equation(DP)
    HDU 3920 Clear All of Them I(DP + 状态压缩 + 贪心)
    POJ 3254 Corn Fields(DP + 状态压缩)
    HDU 2089 不要62(数位DP)
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/11121388.html
Copyright © 2011-2022 走看看