1. 定义函数
def function():
print("Hello,World.")
return
a = function()
print(a)
输出:
Hello,World.
None
def function(string = "Hi"):
print("What you say is:", string)
return
function()
function(string = "Hello,World.")
输出:
What you say is: Hi What you say is: Hello,World.
2.函数的参数
def function1(string): #定义必备参数
print("What you say is:", string)
return
def function2(string = "Hi"): #定义默认参数
print("What you say is:", string)
return
def function3(string2 = "World",string1 = "Hello"): #定义关键字参数
print("What you say is:", string1, string2)
return
def function4(arg1, *arg2): #定义不定长参数
print(arg1)
for i in arg2:
print(i)
return
function1("Hello,World.")
function2()
function3()
function4(10, 1, 2, 3, 4)
输出:
What you say is: Hello,World.
What you say is: Hi
What you say is: Hello World
10
1
2
3
4