Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use exclam (!) syntax for factorial in python

Can a working factorial function/operator be defined with a syntax like in mathematics? i.e. using the ! symbol.

I can't think of any use cases of the existing symbol where things could be ambiguous

  • ipython !shell_escape always has the bang at the start of a line
  • something like 0!=1 would be True as usual because the other interpretation of factorial(0) = 1 would be SyntaxError: can't assign to literal or similar error)
like image 695
wim Avatar asked Sep 01 '26 23:09

wim


1 Answers

If you mean, "can I define the ! operator in my Python program?," the answer is no, Python doesn't have a bang operator that can be defined.

If you mean, "could Python be extended to include a ! operator?," the answer is yes, though probably not as a postfix operator like x!. I very much doubt that the Python community would agree it was a good idea and move ahead with it though.

like image 152
Ned Batchelder Avatar answered Sep 03 '26 15:09

Ned Batchelder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!