代码点读机 / Python / def

def Python · 关键字

定义一个函数,就像写一张“菜谱”并给它起个名字,比如“番茄炒蛋”。 以后你说“做番茄炒蛋”,别人就知道按这张菜谱的步骤来做菜。

怎么用

def 函数名(参数):

小例子

def 打招呼(名字):
    print('你好,' + 名字)

打招呼('小明')

运行结果

你好,小明

提醒:函数体必须缩进,通常 4 个空格。

官方文档:https://docs.python.org/zh-cn/3/tutorial/controlflow.html#tut-functions

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条