9 月 232020
 
#位置参数和关键词参数
#自定义函数,求梯形面积,上底,下底,高。
def trapezoid_area(base_up, base_down, height):
    return 1/2 * (base_up + base_down) * height

#位置参数
print('在函数调用中依次传入参数值的方式称为位置参数')
print('print(trapezoid_area(1,2,3))')
print(trapezoid_area(1,2,3))

#关键词参数
print('在函数调用中传输参数和赋予参数的值的方式称为关键词参数')
print('print(trapezoid_area(base_up=2, base_down=3, height=3))')
print(trapezoid_area(base_up=2, base_down=3, height=3))

#函数中引用参数时可以混合使用位置参数和关键词参数
#仅使用关键词参数时,参数顺序可以打乱
#混合使用参数时,位置参数必须在关键词参数之前

trapezoid_area(height=3, base_down=2, base_up=1) #正确
#trapezoid_area(height=3, base_down=2, 1) #错误
#SyntaxError: positional argument follows keyword argument
#trapezoid_area(base_up=1, base_down=2, 3) #错误
#SyntaxError: positional argument follows keyword argument
trapezoid_area(1, 2, height=3) #正确
trapezoid_area(1, height=3, base_down=2) #正确
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
在函数调用中依次传入参数值的方式称为位置参数
print(trapezoid_area(1,2,3))
4.5
在函数调用中传输参数和赋予参数的值的方式称为关键词参数
print(trapezoid_area(base_up=2, base_down=3, height=3))
7.5
PS C:\Users\harveymei>

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)