Python初学教程:函数定义,python初学教程函数,Python 方法定义如


Python 方法定义

如下代码演示如何在python中定义方法:

#!/usr/bin/python## python中定义方法使用def关键字,def之后空格然后是方法名,后面是()内定义方法的参数列表# 在python中参数不需要指定类型# 方法体内的第一个字符串是文档字符串,用来说明方法的意义def prhello():    "Print hello"    print "Hello, World!"# 调用方法prhello()#定义带参数的方法#def prlines(str, num):    "Print num lines consisting of str, repeating str once more on each line."    for n in range(0,num):        print str * (n + 1)prlines('z', 5)printprlines('fred ', 4)

评论关闭