简体   繁体   中英

a * a//b doesn't work properly in Python (// Operator)

The python / / operator is integer division.

But in case of a * a//b, It works like a * a/b..

a = 10
b = 3
a * a//b
>> 33

a//b * a
>> 30

temp = a//b
a * temp
>> 30

Why is this happening??

You probably meant to write a * (a//b) . Multiplication happens before division, since it's on the left and has same priority.

The order of operations is the same in both cases, but there is an implicit integer truncation operation happening in different parts of the two statements.

In the first case, you truncate to an integer at the end, in the second case, you do that at the start. As a result, you get different answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM