角度を度からラジアンに変換する

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

数学において、角度は度またはラジアンで測定することができます。ラジアンは、数学や物理学の多くの分野で使用される角度測定の標準単位です。このチャレンジでは、角度を度からラジアンに変換する関数を書くよう求められます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/comments("Comments") python/FunctionsGroup -.-> python/function_definition("Function Definition") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/ModulesandPackagesGroup -.-> python/using_packages("Using Packages") python/ModulesandPackagesGroup -.-> python/standard_libraries("Common Standard Libraries") python/PythonStandardLibraryGroup -.-> python/math_random("Math and Random") subgraph Lab Skills python/comments -.-> lab-13618{{"角度を度からラジアンに変換する"}} python/function_definition -.-> lab-13618{{"角度を度からラジアンに変換する"}} python/importing_modules -.-> lab-13618{{"角度を度からラジアンに変換する"}} python/using_packages -.-> lab-13618{{"角度を度からラジアンに変換する"}} python/standard_libraries -.-> lab-13618{{"角度を度からラジアンに変換する"}} python/math_random -.-> lab-13618{{"角度を度からラジアンに変換する"}} end

度からラジアンへ

引数として度で表された角度を受け取り、ラジアンで表された角度を返す関数 degrees_to_rads(deg) を作成します。関数は、次の式を使用して度をラジアンに変換する必要があります。

radians = (degrees * pi) / 180.0

ここで、pi は円の周囲の長さと直径の比を表す定数値であり(約3.14159)、degrees は度で表された角度です。

関数は、4 桁の小数で丸めたラジアンで表された角度を返す必要があります。

from math import pi

def degrees_to_rads(deg):
  return (deg * pi) / 180.0
degrees_to_rads(180) ## ~3.1416

まとめ

このチャレンジでは、式 radians = (degrees * pi) / 180.0 を使用して角度を度からラジアンに変換する方法を学びました。また、この式を実装し、4 桁の小数で丸めたラジアンで表された角度を返す関数を作成しました。