Python 什么是 // 运算符

Python 什么是 // 运算符

在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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程