0Pricing
Python For Kids · レッスン

Mathライブラリを使う

Pythonの数学関数について学びます。

「Mathライブラリを使う」はCoddyKit上の無料Python For Kidsレッスンです。 これはレッスン2/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPython For Kids学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Python For Kidsコースには全3レッスンが含まれています。

Mathライブラリの使用

Mathライブラリのレッスンへようこそ!ここでは、Pythonでmathライブラリを使う方法を学びます。このライブラリには、数学的な計算を行うための便利な関数が数多く用意されています。

Mathライブラリを使う — イラスト1

Mathライブラリとは

Pythonのmathライブラリには、次のような基本的および高度な数学演算のための関数が含まれています。

  • 平方根や累乗の計算
  • 三角関数の利用
  • 最大公約数(GCD)の計算
  • πやeなどの定数の利用

まず、mathライブラリをインポートしてみましょう。

Mathライブラリのインポート

mathライブラリを使用するには、プログラムにインポートする必要があります。

例:

# Imports the math library
import math

# Prints the value of pi
print("The value of pi is:", math.pi)
# Output: The value of pi is: 3.141592653589793

よく使う数学関数

mathライブラリには、よく使う計算のための関数が数多く用意されています。

  • math.sqrt(x): xの平方根を返します。
  • math.pow(x, y): xのy乗を返します。
  • math.ceil(x): xを切り上げて、最も近い整数を返します。
  • math.floor(x): xを切り捨てて、最も近い整数を返します。

例:

import math

# Demonstrates common math functions
print("Square root of 16:", math.sqrt(16))  # Output: 4.0
print("2 raised to the power 3:", math.pow(2, 3))  # Output: 8.0
print("Ceiling of 2.3:", math.ceil(2.3))  # Output: 3
print("Floor of 2.7:", math.floor(2.7))  # Output: 2

三角関数

mathライブラリには、三角関数の計算に使用する関数が含まれています。

  • math.sin(x): xの正弦を返します(単位はラジアンです)。
  • math.cos(x): xの余弦を返します(単位はラジアンです)。
  • math.tan(x): xの正接を返します(単位はラジアンです)。

例:

import math

# Demonstrates trigonometric functions
angle = math.radians(30)  # Converts degrees to radians
print("Sine of 30 degrees:", math.sin(angle))  # Output: 0.5
print("Cosine of 30 degrees:", math.cos(angle))  # Output: ~0.866
print("Tangent of 30 degrees:", math.tan(angle))  # Output: ~0.577

定数の使用

mathライブラリには、math.pi(π)やmath.e(ネイピア数)などの定数が用意されています。

例:

import math

# Demonstrates constants from the math library
print("The value of pi is:", math.pi)  # Output: 3.141592653589793
print("The value of e is:", math.e)  # Output: 2.718281828459045

高度な関数

mathライブラリには、高度な計算のための関数も用意されています。

  • math.factorial(x): xの階乗を返します。
  • math.gcd(x, y): xとyの最大公約数を返します。
  • math.log(x, base): 指定した底におけるxの対数を返します。

例:

import math

# Demonstrates advanced math functions
print("Factorial of 5:", math.factorial(5))  # Output: 120
print("GCD of 8 and 12:", math.gcd(8, 12))  # Output: 4
print("Logarithm of 1000 (base 10):", math.log(1000, 10))  # Output: 3.0

実践例: 円の計算

mathライブラリを使って、円周と円の面積を計算してみましょう。

例:

import math

# Prompts the user for the radius
radius = float(input("Enter the radius of the circle: "))

# Calculates circumference and area
circumference = 2 * math.pi * radius
area = math.pi * math.pow(radius, 2)

# Prints the results
print(f"Circumference: {circumference:.2f}")
print(f"Area: {area:.2f}")

import math

# Calculates and prints the result
result = math.sqrt(49) + math.factorial(3)
print("Result:", result)

よくできました!

mathライブラリを使って、基本的な数学計算から高度な計算まで行う方法を学びました。三角関数、階乗、πのような定数を扱う場合にも、mathライブラリは数学的な問題を解決するための強力なツールです。関数をいろいろ試して、プログラミングスキルを高めていきましょう!

Mathライブラリを使う — イラスト10

よくある質問

「Mathライブラリを使う」レッスンは無料ですか?

はい。「Mathライブラリを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Python For Kidsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Python For Kidsコースには全3レッスンが含まれています。

「Mathライブラリを使う」で何を学びますか?

Pythonの数学関数について学びます。 ブラウザで直接実行するハンズオンコードでPython For Kidsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Python For Kidsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのPython For Kidsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/3です。

「Mathライブラリを使う」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このPython For Kidsレッスンでコードを書いて実行できますか?

はい。すべてのPython For Kidsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Pythonライブラリ入門
  2. Mathライブラリを使う
  3. PyGame入門
← Python For Kidsに戻る