ITPub博客

首页 > 应用开发 > IT综合 > python学习第四天(函数)

python学习第四天(函数)

IT综合 作者:紫翼龙王夜 时间:2015-04-08 18:49:59 0 删除 编辑
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://www.itmin.cn/yunweikaifa/python_jiaoben/838.html
1 定义函数
    >>> def myprint():
    ...     print 'hello world'
    ...
    >>> myprint()
    hello world
     
    2 函数传递参数
    >>> def printMax(a,b):
    ...     if a>b:
    ...             print a
    ...     else:
    ...             print b
    ...
    >>> x=2
    >>> y=3
    >>> printMax(x,y)
    3
     
    3 默认参数值
    >>> def printMax(a,b=1):
    ...     if a>b:
    ...             print a
    ...     else:
    ...             print b
    ...
    >>> x=5
    >>> printMax(x,y)
    5
     
    4 局部变量
    >>> def myprint(x):
    ...     print 'x is:', x
    ...     x=2
    ...     print 'x is:', x
    ...
    >>> x=3
    >>> myprint(x)
    x is: 3
    x is: 2
    >>> print 'x is :',x
    x is : 3
     
    5 全局变量
    >>> def myfunc():
    ...     global x
    ...     print 'x is:',x
    ...     x=2
    ...     print 'x is:',x
    ...
    >>> x=3
    >>> myfunc()
    x is: 3
    x is: 2
    >>> print 'x is:',x
    x is: 2
     
    6 return函数返回值
    >>> def printMax(x,y):
    ...     if x>y:
    ...             return x
    ...     else:
    ...             return y
    ...
    >>> print printMax(1,2)
    2

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30129545/viewspace-1549387/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-02-05

  • 博文量
    87
  • 访问量
    414057