Python学习—— 函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习—— 函数相关的知识,希望对你有一定的参考价值。

一、函数

  1. 定义声明

  def 函数名(参数):

    函数体

    return 返回值

  注意:函数必须在调用前完成定义声明

  2. 参数

    1)普通参数

def test(a):        #此处参数a为普通参数
    print(a)

    2)指定参数

def test(a):
    print(a)

test(a=5)        #此处a为指定参数

    3)默认参数

def test(a, b=3):           #a为普通参数,b为默认参数
    print(a+b)

test(5)                       # 输出为8
test(5, 5)                    # 输出为10

    4)动态参数

def test(*args):                   # args 为动态参数
    print(typeof(args))            # tuple
    print(args)                    # (11, 22, 33)

test(11, 22, 33)    
def test(**kwargs):        # kwargs 为动态参数
    print(type(kwargs))    # dict
    print(kwargs)          # {‘a‘:3, ‘b‘:4, ‘c‘:5}

test(a=3, b=4, c=5)

 

    

以上是关于Python学习—— 函数的主要内容,如果未能解决你的问题,请参考以下文章

python学习手册:第十六章——函数基础

Python学习之旅--函数

学习 Python 之 函数

学习 Python 之 函数

Python学习第10天_函数

[python] 之 函数简介