Python中的數值運算
本文說明了Python中的數值運算。
您可以學習到數值類型、內建函數和模組的概述,以及math模組中數學函數的概述。
YouTube Video
Python中的數值運算
Python具備非常強大的數值運算功能。以下是Python中基本數值運算的說明。
數值類型
Python 主要有以下幾種數值類型。
1# Integer (int)
2x = 10
3y = -5
4print("x =", x, "type:", type(x)) # <class 'int'>
5print("y =", y, "type:", type(y)) # <class 'int'>
6print("x + y =", x + y) # 5
7
8# Float
9a = 3.14
10b = -0.001
11print("\na =", a, "type:", type(a)) # <class 'float'>
12print("b =", b, "type:", type(b)) # <class 'float'>
13print("a * 2 =", a * 2) # 6.28
14
15# Complex
16c = 2 + 3j
17d = 1 - 1j
18print("\nc =", c, "type:", type(c)) # <class 'complex'>
19print("d =", d, "type:", type(d)) # <class 'complex'>
20print("c + d =", c + d) # (3+2j)
21print("c * d =", c * d) # (5+1j)- int(整數類型) 代表整數,例如
10或-5。 - float(浮點數類型) 代表帶有小數點的數字,例如
3.14或-0.001。 - complex(複數類型) 代表複數,例如
2 + 3j。這裡,j是虛數單位。
內建函數和模組
Python還提供了許多對數值運算有用的內建函數和模組。
abs(x)函式會返回絕對值。
1result = abs(-10) # result is 10
2print(result)本範例展示如何使用 abs() 函數來取得數值的絕對值。
round(x, n)函式會將數值四捨五入到小數點後 n 位。
1result = round(3.14159, 2) # result is 3.14
2print(result)本範例展示如何使用 round() 函數將數值四捨五入到指定的小數位數。
math模組 提供更進階的數學函式。
1import math
2
3# result is 4.0 (square root)
4result = math.sqrt(16)
5print(result)
6
7# result is 1.0 (sine function)
8result = math.sin(math.pi / 2)
9print(result)本範例展示如何使用 math 模組進行進階數學運算,如開平方根和三角函數計算。
decimal模組 支援高精度的小數計算。
1from decimal import Decimal
2
3# result is Decimal('0.3')
4result = Decimal('0.1') + Decimal('0.2')
5print(result)
6
7# The result is 0.30000000000000004 due to floating-point inaccuracies
8print(0.1 + 0.2)此程式碼為避免浮點數錯誤並使用 decimal 模組進行高精度小數計算的範例。
fractions模組 處理有理數(分數)。
1from fractions import Fraction
2result = Fraction(1, 3) + Fraction(1, 6) # result is Fraction(1, 2)
3print(result)憑藉這些功能,Python使執行各種數值計算變得輕而易舉。由於浮點數計算的精度限制,建議在需要更高精度的計算中使用decimal模組。
Python中的數學函數
在 Python 中,有一個方便的模組叫做 math,可以用來執行數學函數。此模組提供許多進行數學計算所需的函數。讓我們來看看 math 模組的一些代表性函數。
基礎數學函數
math.sqrt(x):返回x的平方根。x必須是非負數。math.pow(x, y):返回x的y次方。math.exp(x):返回x的指數值 ((e^x))。math.log(x[, base]):計算自然對數或指定base的對數值。如果省略base,將計算自然對數。
三角函數
math.sin(x):返回x的正弦值(以弧度計)。math.cos(x):返回x的餘弦值(以弧度計)。math.tan(x):返回x的正切值(以弧度計)。
反三角函數
math.asin(x):返回x的反正弦值。math.acos(x):返回x的反餘弦值。math.atan(x):返回x的反正切值。
雙曲函數
math.sinh(x):返回x的雙曲正弦值。math.cosh(x):返回x的雙曲餘弦值。math.tanh(x):返回x的雙曲正切值。
常數
math.pi:一個常數,表示圓周長與直徑的比值 ((\pi))。math.e:一個常數,表示自然對數的底數 ((e))。
計算範例
以下是一些例子。
1import math
2
3# Square root calculation
4print(math.sqrt(16)) # Output: 4.0
5
6# Power calculation
7print(math.pow(2, 3)) # Output: 8.0
8
9# Natural logarithm calculation
10print(math.log(math.e)) # Output: 1.0
11
12# Trigonometric functions
13degree = 45
14radian = math.radians(degree) # Convert to radians
15print(math.sin(radian)) # Output: 0.7071067811865475 (approximately 1/√2)
16
17# Constants
18print(math.pi) # Output: 3.141592653589793
19print(math.e) # Output: 2.718281828459045- 此程式碼使用
math模組來執行如平方根、冪運算、自然對數和三角函數等計算,並且顯示 π 和歐拉數等常數。
總結
在這個例子中,我們使用了 math 模組,但 Python 也提供了廣泛的支援,用於高級數學計算和隨機數生成。通過使用像 NumPy 或 SciPy 這樣的函式庫,您可以利用更強大且多樣化的數學功能。
您可以在我們的 YouTube 頻道上使用 Visual Studio Code 來跟隨上述文章一起學習。 請也查看我們的 YouTube 頻道。