Star fruit Python little bit record 1-usage of Python function parameter with =

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

 

The function testFunc (x, y, k = 1.0) where k = 1.0 means that if the parameter k is not passed in, the default value is 1.0, but once the function is called with the parameter k passed, the value passed is taken.

Example Python code:

def func(x,y,k=1.0):
    print("x=", x)
    print("y=", y)
    print("k=", k)

if __name__ == '__main__':
	print("第一次调用func函数没有传参数k,因此k取默认值:")
	func(3, 4)
	print("第二次调用func函数传了参数k,因此k取传入的值:")
	func(6.5, 7.5, 8.5)

运行结果:
第一次调用func函数没有传参数k,因此k取默认值:
x= 3
y= 4
k= 1.0
第二次调用func函数传了参数k,因此k取传入的值:
x= 6.5
y= 7.5
k= 8.5

 

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104464420