学习Python的笔记21--函数的参数传递

接上一节的习题2,给出代码的另一种写法:

def triangle(a,b):
    return 'The right triangle third side\'s length is {}'.format(pow((pow(a,2) + pow(b,2)),0.5))

print(triangle(3,4))

运行结果为:

The right triangle third side's length is 5.0

Process finished with exit code 0

与第一种代码的执行结果相同。
函数的参数传递包括两种:位置参数传递和关键词参数传递。

def trapezoid_area(base_up = 1, base_down = 2, height = 3):
    return 1/2 * (base_up + base_down) * height
print(trapezoid_area())
print(trapezoid_area(4,5,6))
print(trapezoid_area(height = 3,base_down = 2,base_up = 1))

运行结果是:

4.5
27.0
4.5

猜你喜欢

转载自blog.csdn.net/woshilingdaoren/article/details/80398525