Python中的除法运算符用法指南

2021年4月3日18:06:39 发表评论 829 次浏览

考虑以下Python语句。

# A Python program to demonstrate the use of 
# "/" for integers
print ( 5 / 2 )
print ( - 5 / 2 )

输出如下:

2
-3

第一个输出很好, 但是如果我们即将进入Java / C ++世界, 第二个输出可能会感到惊讶。在Python中, " /"运算符用作整数和浮点参数的下位除法。但是, 如果参数之一是浮点数, 则运算符/将返回一个浮点值(类似于C ++)

# A Python program to demonstrate use of 
# "/" for floating point numbers
print ( 5.0 / 2 )
print ( - 5.0 / 2 )

输出如下:

2.5
-2.5

实际楼层划分运算符为" //"。它返回整数和浮点参数的下限值。

# A Python program to demonstrate use of 
# "//" for both integers and floating points
print ( 5 / / 2 )
print ( - 5 / / 2 )
print ( 5.0 / / 2 )
print ( - 5.0 / / 2 )

输出如下:

2
-3
2.0
-3.0

看到这个例如。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: