zoukankan      html  css  js  c++  java
  • python-函数2(调用)

    python-函数2(调用)

    1、实参和形参调用

    2、默认调用

    3、参数驵调用

     

     1、实参和形参调用

    def test5(x,y):                             #形参
        print(x)
        print(y)
    
                                                 y=1
                                                 x=2
    
    test5(2,1) #实参与形参顺序依依对应,这种传参方式是位置参数。                            
    test5(x=3,y=5)#这种方式是关键字传参,没有顺序。
    
    test(3,y=2) 既有关键字又有位置参数,会按照位置参数的规则来进行。
    text5(y=y,x=x)关键字与实参顺序无关
    test(3,z=2,y=6)
    @@@@@关键参数不能写在位置参数前@@@@@。。。

    2、默认调用

    def test5(x,y=5):
        print(x)
        print(y)
    test5(2)
    ################################
    
    def test5(x,y=5):
        print(x)
        print(y)
    test5(2,y=3)
    
    默认参数特点:调用函数的时候,默认参数非必须传递。
    用途:1、默认安装值 (def test6(x,soft1=True,soft2=True:))
          2、默认数据库连接。(def test8(host,part="3306"):)

    3、参数组调用

    ________________________
    
    def test5(x,y=5):
        print(x)
        print(y)
    test5(2)
    ################################
    
    def test5(x,y=5):
        print(x)
        print(y)
    test5(2,y=3)
    
    默认参数特点:调用函数的时候,默认参数非必须传递。
    用途:1、默认安装值 (def test6(x,soft1=True,soft2=True:))
          2、默认数据库连接。(def test8(host,part="3306"):)
    
    
    
    
    参数组
    ----------------------------------------
    例1:以元组方式定义

    def test(*args): print(args) test(1,2,3,4,5,6) def test1(x,*args): print(args) test(*[1,2,3,4,2]) 例2以字典方式定义 def test2(**zdargs): print(zdargs)

    # print(zdargs["name"])
    # print(zdargs['age'])
    # print(zdargs['sex'])

    
    
    test2(name='kezi',age=18,sex="F")    #把N个关键字参数,转换成字典的方式
    test2(**{name="kezi1",age=28,sex="M"}) 
  • 相关阅读:
    Algorithm Gossip (48) 上三角、下三角、对称矩阵
    .Algorithm Gossip (47) 多维矩阵转一维矩阵
    Algorithm Gossip (46) 稀疏矩阵存储
    Algorithm Gossip (45) 费氏搜寻法
    Algorithm Gossip (44) 插补搜寻法
    Algorithm Gossip (43) 二分搜寻法
    Algorithm Gossip (42) 循序搜寻法(使用卫兵)
    Algorithm Gossip (41) 基数排序法
    Algorithm Gossip (40) 合并排序法
    AlgorithmGossip (39) 快速排序法 ( 三 )
  • 原文地址:https://www.cnblogs.com/kezi/p/11966581.html
Copyright © 2011-2022 走看看