What does python's division operator mean?

藏色散人
Release: 2019-08-01 15:47:52
Original
7347 people have browsed it

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中的%取余运算符不支持浮点数类型
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!