Python 函数

Python中用 def 关键字声明函数:

def sign(x):
    if x>0:
        return 'positive'
    elif x<0:
        return 'negative'
    else:
        return 'zero'
for x in [-1,0,8]:
    print(sign(x))
   # print: negative,zero,positive

定义一个有默认值参数的函数:

def hello(name, loud=False):
    if loud:
        print('HELLO, %s!' % name.upper())
    else:
        print('Hello, %s' % name)

hello('Bob') # Prints "Hello, Bob"
hello('Fred', loud=True)  # Prints "HELLO, FRED!"
原文地址:https://www.cnblogs.com/Jesse-Li/p/8782110.html