zoukankan      html  css  js  c++  java
  • (07)-Python3之--函数

    1.定义

    函数:实现了某一特定功能。

         可以重复使用。

    例如:

    len()    功能:获取长度。
    input()   功能: 控制台输入
    print()   功能:输出

    语法:

    def 函数名称(参数1,参数2.。。。):
      实现函数的功能(代码段/块)
      [return [值]]

    2.函数的参数

    2.1位置参数(必传,定义的时候没有数值。)

    形参和实参的参数顺序是一一对应的

    形式参数,接收具体的数值。会变

    例如:

    复制代码
    def out_going(id,ticket):
        print("请出示身份证和飞机票!")
        print("身份证为:" + id,"机票为:" + ticket)
        if len(id) != 18:
            print("身份证不符合要求!!")
        if id is not None and ticket is not None:
            print("请上飞机!")
    
    # 调用函数
    out_going("363636200001023212","东方航空")
    
    结果:
    请出示身份证和飞机票!
    身份证为:363636200001023212 机票为:东方航空
    请上飞机!
    复制代码

    2.2默认参数(定义函数时,给形参一个默认的具体数值。)

    可传可不传

    默认参数要放在所有的必传参数之后

    例如:

    复制代码
    def out_going(id, ticket, enter="上海", h="5"):
        enters = ["上海", "北京", "深圳"]
        print("请出示身份证和飞机票!")
        print(" 身份证为:" + id + "
    ", "机票为:" + ticket + "
    ","到达城市为:" + enter + "
    ", "耗时:" + h + "
    ")
        if len(id) != 18:
            print("身份证不符合要求!!")
        elif enter not in enters:
            print("当前城市尚未开放机场!!")
        elif id is not None and ticket is not None:
            print("请上飞机!")
    
    # 调用函数
    out_going("363636200001023212", "东方航空")
    
    结果:
    请出示身份证和飞机票!
     身份证为:363636200001023212
     机票为:机票
     到达城市为:上海
     耗时:5
    
    请上飞机!
    复制代码

    2.3指定参数(调用的时候去指定  形参=数据)

    形参=值。可以不按位置的顺序来传参

    例如:

    复制代码
    def out_going(id, ticket, enter="上海", h="5"):
        enters = ["上海", "北京", "深圳"]
        print("请出示身份证和飞机票!")
        print(" 身份证为:" + id + "
    ", "机票为:" + ticket + "
    ","到达城市为:" + enter + "
    ", "耗时:" + h + "
    ")
        if len(id) != 18:
            print("身份证不符合要求!!")
        elif enter not in enters:
            print("当前城市尚未开放机场!!")
        elif id is not None and ticket is not None:
            print("请上飞机!")
    
    # 调用函数
    out_going("363636200001023212", "东方航空",h="10",enter="北京")
    
    结果:
    请出示身份证和飞机票!
     身份证为:363636200001023212
     机票为:东方航空
     到达城市为:北京
     耗时:10
    
    请上飞机!
    复制代码

    2.4可变参数(*args、**kwargs)

    参数个数不固定 。调用的时候来确定有几个参数。

    *args

    在函数内部,是以元组的形式来表示。

    放在位置参数,默认参数之后。

    例如:

    复制代码
    def my_args(*args):
        print(args)
    
    # 调用函数
    my_args(12,34,True,False,[1,2])
    my_args("hello",True)
    my_args()
    
    结果:
    (12, 34, True, False, [1, 2])
    ('hello', True)
    ()
    复制代码

    **kwargs

    在函数内部,是以字典的形式来表达。

    一个函数的定义当中,都有*args,**kwargs.先*args,再**kwargs。

    例如:

    复制代码
    def my_all_args(num,*args,**kwargs):
        print(num,args,kwargs)
        if "my_class" in kwargs:
            if kwargs["my_class"] == "python17":
                print("正确!")
                
    # 调用函数
    my_all_args(12,23,34,45,hello="world",name="python17")
    
    结果:
    12 (23, 34, 45) {'hello': 'world', 'name': 'python17'}
    复制代码

    3.函数返回(return)

    1、代表函数返回数据

    2、终止函数调用 

    3、return 后面可以不用跟任何的数据。实际上代表的是返回的None 。return None

    4、调用函数的时候,如果函数有返回值,要主动用变量接收函数的返回值

    5、return 可以返回任意类型的数据

    6、定义函数的时候,没有用到return.请问调用函数,有返回值?有,为None

  • 相关阅读:
    hdoj 2803 The MAX【简单规律题】
    hdoj 2579 Dating with girls(2)【三重数组标记去重】
    hdoj 1495 非常可乐【bfs隐式图】
    poj 1149 PIGS【最大流经典建图】
    poj 3281 Dining【拆点网络流】
    hdoj 3572 Task Schedule【建立超级源点超级汇点】
    hdoj 1532 Drainage Ditches【最大流模板题】
    poj 1459 Power Network【建立超级源点,超级汇点】
    hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
    hdoj 1012 u Calculate e
  • 原文地址:https://www.cnblogs.com/dengbingbing/p/12330579.html
Copyright © 2011-2022 走看看