zoukankan      html  css  js  c++  java
  • lesson4: 函数简单应用

    函数语法:

    语法:

    def functionname(para):

         "函数_文档字符串"

          function_suite

          return[expression]

    sample1:print str by def

    def printstr(str):
        "print input str"
        print str
        return

    printstr("welcome to python!")

    实参:调用函数时的变量名称

    形参:定义函数时的变量名称

    缺省参数:调用函数时,缺省参数的值没有传入,则被认为是默认值

                sample2

                 def printstr(str="hell0,python"):

                      print str

                  printstr()

    多类型传参:sample3

    def f(x):

        print x

    f(10)

    f("hello")

    f([1,2,3])

    f((1,2,3))

    f({1,2,3})

    Sample 4:

    def f(x,y):

        print x,y

    T={5,"janice"}
    f(*T)
    为什么打印结果为 janice 5 呢?

    sample5:一一对应的字典传值

    def f(x,y):

        print x,y

    T={'x':'janice', 'y':'18'}
    f(**T)

     结果输出:janice 18

    sample 6:处理不定长参数

    def f(x,*args,**kw):
        print x
        print args,
        print kw

    f(1,2,3,y=20,z=30)

  • 相关阅读:
    Lua C Api
    Lua string.gsub (s, pattern, repl [, n])
    LearnOpenGL 你好,三角形[转]--附源码
    学习OpenGL简单易懂网站
    泰文排版规则
    Lua截取utf-8编码的中英文混合字符串
    字符编码
    使用Ant编译提示Class not found: javac1.8
    MySQL索引
    转 linux 权限
  • 原文地址:https://www.cnblogs.com/janicce-zhong/p/5110534.html
Copyright © 2011-2022 走看看