Python实战入门到精通第一讲——函数

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

扫描二维码关注公众号,回复: 8983104 查看本文章

10

1

2

3

4

发布了332 篇原创文章 · 获赞 170 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qq_32146369/article/details/104158358