Home  >  Article  >  Backend Development  >  What does python’s division operator mean?

What does python’s division operator mean?

藏色散人
藏色散人Original
2019-06-27 10:04:097355browse

What does python’s division operator mean?

In Python, there are two division operators: / and //.

X / Y

For Python2.X, if both operands are integers, the result will be rounded down (this sum The difference in C is that C is rounded to 0), that is to say, if the result is originally -2.5, then round down to -3, if the result is originally 2.5, then round down to 2; if both If the operand has a floating point number, then it is a floating point number division, and the result is a floating point number division; for Python3. The most important thing is that for the % remainder operation, in Python, unlike C syntax, the remainder operator supports floating point operations, and if the operand has a floating point number, the returned result type is also a floating point type.

X // Y

//The effect of the operation is the same for Python2.X and Python3.X, regardless of the operands Whether they are all integers or floating point numbers, // will be rounded down. Of course, for calculations with floating point numbers, the result will still be returned in the form of floating point numbers, such as -5.0 // 2, the result is -3.0:

5 / -2
#2.X 商:-3 余数:-1
#3.X 商:-2.5 余数:-1
#C 商:-2 余数:1

5.0 / -2
#2.X 商:-2.5 余数:-1.0
#3.X 商:-2.5 余数:-1.0
#C 商:-2.5 余数:C中的%取余运算符不支持浮点数类型

You can know from the above example that in Python, the sign of the remainder is the same as the divisor (and in C, the sign of the remainder is the same as the dividend). With this, you can calculate the quotient Size and symbols

Related recommendations: "

Python Tutorial

"

The above is the detailed content of What does python’s division operator mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn