What's the difference between “2*2” and “2**2” in Python?

前端 未结 10 1026
小蘑菇
小蘑菇 2020-12-30 18:36

What is the difference between the following codes?

code1:

var=2**2*3

code2:

var2         


        
相关标签:
10条回答
  • 2020-12-30 19:07

    2**2 = 2 power-of 2

    2*2 = 2 times 2

    0 讨论(0)
  • 2020-12-30 19:07

    A double asterisk means to the power of. A single asterisk means multiplied by. 22 is the same as 2x2 which is why both answers came out as 4.

    0 讨论(0)
  • 2020-12-30 19:17
      2**2 means 2 squared (2^2)
      2*2 mean 2 times 2 (2x2)
    

    In this case they happen to have the same value, but...

      3**3*4 != 3*3*4
    
    0 讨论(0)
  • 2020-12-30 19:17

    Power has more precedence than multiply, so:

    2**2*3 = (2^2)*3
    2*2*3 = 2*2*3
    
    0 讨论(0)
提交回复
热议问题