戻り値
def myfunc():
print("Hello")
return "Good bye"
x = myfunc()
#Hello #myfunc内の表示
print(x)
#Good bye #returnされた値(strオブジェクト値)
関数をリストに入れて呼び出す
import math
# mathモジュール内の関数のリストを作る
func_list = [math.log,math.sqrt, math.exp, math.cos]
# log(10),√10, exp(10), cos(10)を計算
for f in func_list:
print(f(10.0))
print('- '*20)
#番号をinput1個だけ動かす
a=int(input('funcNo.?'))#inputをint()で数値にする
print(func_list[a])
print(func_list[a](10))#リストから番号の関数を呼び出す()を付けると関数になる、10はとりあえずの引数
# ↓ ()を付けずに、関数の実体を書いても何もされないしエラーにもならない
math.log
math.sqrt
math.exp
math.cos
実行結果 2.302585092994046 3.1622776601683795 22026.465794806718 -0.8390715290764524 - - - - - - - - - - - - - - - - - - - - funcNo.?0 <built-in function log> 2.302585092994046 funcNo.?1 <built-in function sqrt> 3.1622776601683795 funcNo.?2 <built-in function exp> 22026.465794806718 funcNo.?3 <built-in function cos> -0.8390715290764524