zoukankan      html  css  js  c++  java
  • python_函数

    1. 函数的格式定义

      def 函数名(参数列表):

        语句块(向右缩进4个或者8个空格,只要后续所有的缩进一样即可)

        return (无返回值时,返回None)

    2. 对参数设置默认值

       def 函数名(param1 = value1 , param2 = value2, ...):

        语句块

        return (无返回值时,返回None)

    3. 提供可变参数

      def 函数名(param1 ,..., *paramN):

        语句块

        return (无返回值时,返回None)

       备注:

        a. *代表的是收集其余的位置参数

        b. 一个*代表将参数收集为元组,二个*代表将参数收集为字典

    4. 局部变量和全局变量

      一般在函数中的变量属于局部变量,在局部变量中可以使用global声明全局变量

    5. 示例:

    #coding:utf-8
    
    #函数的定义
    def hello(name):
        return "hello " + name + "!"
        
    print hello("Lucy")
    
    
    #给函数的参数设置默认值
    def hello_1(greeting = "hello", name = "world"):
        print "%s, %s!" % (greeting, name)
        
    hello_1()
    hello_1("Greetings")
    hello_1("Greetings", "universe")
    hello_1(name = "Gumby")
    
    
    #提供可变参数
    def print_params_1(x, y, z = 3, *pospar, **keypar):
        print x, y, z
        print pospar
        print keypar
    print_params_1(1, 2, 3, 5, 6, 7, foo = 1, bar = 2)
    
    
    #局部变量和全局变量
    x = 20
    def print_var_1(y):
        print y
        global x
        print x
        x = x + 1
    print_var_1(5)
    print x

      运行结果:

        

  • 相关阅读:
    jq validate的用法
    position:fixed定位
    postgresql-日志表
    postgresql-查看各个数据库大小
    postgresql-清空shared_buffers
    postgresql-int,bigint,numeric效率测试
    postgresql-查看表大小
    mongodb postgresql mysql jsonb对比
    postgresql和redis
    postgresql-死锁
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/5190639.html
Copyright © 2011-2022 走看看