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
  • 相关阅读:
    Java HashMap 和 ConcurrentHashMap
    递归算法应用
    二叉树基础知识
    自动删除qq空间说说
    移动APP测试的22条军规--笔记
    SQL Server数据库状态监控
    SqlSugar-事务操作
    详解第一范式、第二范式、第三范式、BCNF范式
    SQL 日期
    2019年世界各国gdp排名对比
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/11121388.html
Copyright © 2011-2022 走看看