// 运算符的作用是什么?
在 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
广告