0

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.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172

3 Answers3

3

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.

1

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.

Mandraenke
  • 3,086
  • 1
  • 13
  • 26
  • Well, *python specifically* works this way. Another programming language could support implicit multiplication. I mean, the *computer* doesn't understand any of that at all, it understands machine code – juanpa.arrivillaga Oct 20 '21 at 19:35
1

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)