Python中编写函数时有时候会看到代码中有一个return()的语句

在Python中定义一个函数时可以在最后加上return返回值,方便查看函数是否运行完成返回函数的值
函数可以不用return,如果没有return返回值,函数返回的值为None
————————————————————————————————————————————
示例函数的code:

#函数cv_score(d)关于参数d=max_depth的函数创建如下
def cv_score(d):
clf=DecisionTreeClassifier(max_depth=d)
clf.fit(X_train, y_train)
tr_score=clf.score(X_train, y_train)
cv_score=clf.score(X_test,y_test)
return (tr_score, cv_score)
#所以其实cv_score有两列,第0列为tr_score,第1列为cv_score,且由于返回的是两个值,所以将以元组tuple的方式返回
————————————————————————————————————————————
函数可以返回数字,字符串,列表,元组,字典,集合;如果返回多个值,则返回的值将以元组返回(tuple相当于只读的list
Python--编写函数后为什么有个return?
————————————————————————————————————————————
return语句代表函数执行结束,函数不执行return语句后的操作

相关文章:

  • 2022-12-23
  • 2021-09-22
  • 2021-04-25
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-31
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
相关资源
相似解决方案