[Python-ideas] Proposal: Using % sign for percentage

Steven D'Aprano steve at pearwood.info
Sat May 4 20:17:51 EDT 2019


On Sun, May 05, 2019 at 02:34:27AM +0800, Ronie Martinez wrote:

> It does not interfere with modulo operator as modulo follows a different
> format:
> ```
> a = x % y
> ```

Alas, that's not correct, because + and - are both unary operators as 
well as binary operators, so this:

    x % + y

is ambiguous:

    x modulo (+y)
    (x%) plus y


> This looks like a small feature but it will surely set Python a level
> higher in terms of readability.

It definitely does look like a very small feature, but can you 
demonstrate that it will be better for readability by showing some 
actual real code that would be improved by this? How often do you 
hard-code a rate into your program like this?

    rate = 12.75%


I don't see the readability improvement from converting the 
decimal to a percent in my head once, when I write the code:

     # think: I want 12.75% so divide by 100
     rate = 0.1275

versus having to convert the percent to a decimal in my head every 
single time I read the code:

     # read:
     rate = 12.75%
     # think: percent operator, not modulo, 
     # so divide by 100, the value must be 0.1275


Percent notation is already ambiguous in real life, which is why 
mathematicians don't use it. This is fine:

     discount = sell_price * 20%

but what do you expect these to do?

     price = cost + 20%

     marked_down_price = price - 15%

Aside from the ambiguity with modulo, and the dubious readability 
improvement, I think this will trip people up when they try to calculate 
with it. There are two obvious meanings for the above examples:

    # percent unary operator has low precedence
    price = (cost + 20)/100

    # percent unary operator has high precedence
    price = cost + (20/100)

and neither match what people mean when they say "the price is the cost 
plus 20 percent".



-- 
Steven


More information about the Python-ideas mailing list