// 運算子的作用是什麼?


在 Python 中,// 是雙斜槓運算子,即地板除。// 運算子用於執行除法,並將結果向下舍入到最接近的整數。// 運算子的使用非常簡單。我們還將比較單斜槓除法的結果。讓我們首先看看語法。

//(雙斜槓)運算子的語法

a 和 b 分別是第 1 個和第 2 個數字

a // b

//(雙斜槓)運算子的示例

讓我們現在看一個示例,在 Python 中實現雙斜槓運算子 -

a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

輸出

The 1st Number =  37
The end Number =  11
Result of floor division =  3

使用負數實現 //(雙斜槓)運算子

我們將嘗試使用負數作為輸入來使用雙斜槓運算子。讓我們看看這個例子 -

# A negative number with a positive number a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

輸出

The 1st Number =  -37
The end Number =  11
Result of floor division =  -4

如您在上述輸出中看到的,使用負數並沒有影響舍入。結果向下舍入。

讓我們檢查一下單斜槓的結果並進行比較。

/ 運算子的示例

/ 運算子是 Python 中的除法運算子。讓我們看一個例子 -

a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using the / operator res = a / b print("Result of division = ", res)

輸出

The 1st Number =  37
The end Number =  11
Result of division =  3.3636363636363638

使用負數實現 / 運算子

在這個例子中,我們將學習斜槓運算子如何與負數一起工作。

a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using the / operator res = a / b print("Result of division = ", res)

輸出

The 1st Number =  -37
The end Number =  11
Result of division =  -3.3636363636363638

更新於: 2022年9月15日

483 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告