使用数学库
探索 Python 中的数学函数。
使用数学库 是 CoddyKit 上的免费 Python For Kids 课时。 这是第 2 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Python For Kids 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Python For Kids 课程共包含 3 节课。
使用数学库
欢迎学习数学库!今天,我们将探索如何在 Python 中使用 math 库。这个库提供了许多用于数学计算的实用函数。

什么是数学库?
Python 中的 math 库包含用于基本和高级数学运算的函数,例如:
- 计算平方根和幂
- 使用三角函数
- 求最大公约数(GCD)
- 使用
π和e等常量
让我们先导入 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 库都是解决数学问题的强大工具。请继续尝试使用其中的函数,提升您的编程技能!

常见问题解答
「使用数学库」课时是免费的吗?
是的 — 「使用数学库」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Python For Kids 课程的其余内容,请升级到 CoddyKit PRO。 Python For Kids 课程共包含 3 节课。
「使用数学库」这节课中我会学到什么?
探索 Python 中的数学函数。 你通过在浏览器中直接运行的动手代码来练习 Python For Kids,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Python For Kids 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Python For Kids 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 3 节。
「使用数学库」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Python For Kids 课中编写并运行代码吗?
能。每节 Python For Kids 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Python 库简介
- 使用数学库
- PyGame 简介