nomurabbitのブログ

nomurabbitのブログはITを中心にした技術ブログです。

nomurabbitのブログ

【Python】標準ライブラリで算術演算 Part1【AWS】

この記事はPython 3.9AWS Lambdaで算術計算する方法を解説したものです。


こんにちは!らびです。今回はPython 3.9AWS Lambdaで計算を試していきます。

標準ライブラリでどこまでできるか楽しみですね。


Python Lambdaのコード

早速ですがPythonのコードです。今回は三角関数を計算していきます。


1/4π(45°)のsin、cos、tan値

import json
import math

def lambda_handler(event, context):
  answer_sin = math.sin(math.pi / 4)
  answer_cos = math.cos(math.pi / 4)
  answer_tan = math.tan(math.pi / 4)
  return {
    "statusCode": 200,
    "body": json.dumps({
        "message": "sin:" + str(answer_sin ) + " cos:" + str(answer_cos ) + " tan:" + str(answer_tan ),
    }),
  }


実行結果はこんな感じだジョ。


f:id:nomurabbit:20220123175820p:plain


piが有限として計算されているので、tanの結果が0.999…になっていますね。

まとめ

以上がPython 3.9AWS Lambdaによる三角関数の計算でした。

標準ライブラリだけでもいろいろな計算が試せそうですね!

次回もぜひご覧ください。では!