zoukankan      html  css  js  c++  java
  • 函数&&变量

    #*- encoding=utf-8 -*
    import sys
    print(sys.getdefaultencoding())

    def test(x,y,z):
    print(x)
    print(y)
    print(z)

    test(1,2,3)
    #x,y,z称为形式参数,“1,2,3”称为位置参数,赋值时形参不能写在位置参数前面
    test(1,z=7,y=3)

    ####默认值参数
    def test6(x,y=2):
    print(x,y)

    test6(1,2) #通过位置参数传递
    test6(y=3,x=6) #通过关键字参数传递


    ##传递不固定长度的参数
    def test2(*args):
    print(args)

    test2(1,2,3,4,5)


    #传递参数数组
    def test3(*args):
    print(args)

    test3(*[1,2,3,4,5])

    ##传递字典参数 key:value :**kwargs定义:把n个关键字参数转换为字典的方式
    def test4(**kwargs):
    print(kwargs)

    test4(name='abc',age=18,job='sail')



    ###位置参数+默认值参数+字典参数
    def test5(name,age=18,**kwargs):
    print(name)
    print(age)
    print(kwargs)

    test5('YF',20,job='sail',address='BJ')
    test5('YF',job='sail',address='BJ',age=28)


    局部变量与全局变量:
    name='Liu Hu Lan'   #全局变量
    school='HB'
    def change_name(name):
        global school
        school='kinghua' #如果局部变量想变成全局变量,需要在该局部域(函数体)内用global 声明
        print('before_name:',name,',School is :',school)
        name='Hua Mu Lan'
        print('after name:',name)
    
    change_name(name)
    print(name,school)
    
    '''
    以上返回值:
    before_name: Liu Hu Lan ,School is : kinghua
    after name: Hua Mu Lan
    Liu Hu Lan kinghua
    '''
    View Code


  • 相关阅读:
    字符串匹配算法之Rabin-Karp算法
    算法导论之最近顶点对
    php连mssql中文乱码问题
    Trie树
    PAT 1057. Stack (30)
    PAT 1033. To Fill or Not to Fill (25)
    PAT 1034. Head of a Gang (30)
    PAT 1075. PAT Judge (25)
    spring框架资料
    Spring Security资料
  • 原文地址:https://www.cnblogs.com/yiyuf/p/8630177.html
Copyright © 2011-2022 走看看