Home > other >  8/2(2 2) doesn't work in python but 8/2 (2 2) and 8/2*(2 2) do. Why?
8/2(2 2) doesn't work in python but 8/2 (2 2) and 8/2*(2 2) do. Why?

Time:10-21

Trying to understand operands in python.

8/2(2 2) gave the following error:

TypeError Traceback (most recent call last)
<ipython-input-12-8949a58e2cfa> in <module>
----> 1 8/2(2   2)

TypeError: 'int' object is not callable.

Trying to do this like this then using sum() then as python dictionary then in numpy.

CodePudding user response:

Python doesn't support implicit multiplication. When Python tries to run 2(2 2), it tries to call the numeric literal 2 as a function, passing 2 2 as an argument to it. You need to use * between things you want to multiply.

CodePudding user response:

There is no operator between the 2 and the ( - human math assumes a multiplication here but a computer does not.

The parser sees 2(...) - which is interpreted as a function with the name 2 and a parameter.

Since there is no default function with that name and there is no def 2(x) you get that error message.

Additionally 2 is not a vaild function name in python.

CodePudding user response:

Python doesn't work like normal maths. 2(2 2) will not be executed as 2×4. Instead, 2 will be treated as a function, which is not callable (your error message) . To do that, you've to put operator between 2 and (2 2). Try putting a * between 2 and (2 2). Your expression would be 8/2*(2 2)

  • Related