ray88’s diary

お仕事で困ったとき用の自分用の覚書

Python 引数がなく戻り値がある関数

python 目次 - ray88’s diary
各種目次 - ray88’s diary

【書式】
def 関数名():
     #インデントの開始(半角空白4個下げ)
    ステートメント1
    ステートメント2
    ステートメント3
    return 戻り値
#インデントの終了(関数の終わり)

【例】

#現在の日時を日本語形式の文字列で取得する
def get_current_time():
    import datetime
    now = datetime.datetime.now()
    current_time = now.strftime("%Y年%m月%d日 %H:%M:%S")
    return current_time

#使用例
formatted_time= get_current_time()
print(formatted_time)

出力結果

print(get_current_time())