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
  • 相关阅读:
    Windows7安装SQL Server 2008图解
    【Android病毒分析报告】
    linux source命令学习
    SQL学习笔记——SQL初入门,Ubuntu下MySQL的安装
    挂断电话的实现(即类似于电话号码黑名单)
    35+雪花Logos设计灵感
    jdk8预览
    PL/SQL --> 动态SQL调用包中函数或过程
    VMware 彻底删除虚拟机操作系统的方法
    Ubuntu 16.04 LTS: apt-get update 失败处理 Aborted (core dumped)
  • 原文地址:https://www.cnblogs.com/liujinjing521/p/11121388.html
Copyright © 2011-2022 走看看