Python traps · Python
Python: -2 ** 2 = -4?!PRO
** binds tighter than the minus sign, so -2 ** 2 is -(2 ** 2). Brackets fix it: (-2) ** 2 = 4.
# Python: -2 ** 2 = -4 ?! # ** binds tighter than the minus sign, so this is -(2 ** 2). print(-2 ** 2) # -4 ?! print(-(2 ** 2)) # -4 (what Python actually did) # Fix: brackets around the negative number print((-2) ** 2) # 4 ✓ x = -2 print(x ** 2) # 4 ✓ a variable holds the whole -2Open code P11 in Pocket Compiler Download the Pocket Compiler app — free
No app yet? Install it, then open the app and type the code P11 in Code Library.