0

In this piece of code the iterations should stop at one position earlier than they do.

k = 0
while 2^k < 5:
    k += 1
print(k)

I expect it to print '3' but it prints out '4'.

Mario
  • 561
  • 3
  • 18

2 Answers2

11

^ is the bitwise XOR operator. You probably meant ** for exponentiation.

0x5453
  • 12,753
  • 1
  • 32
  • 61
6

In Python the ^ operator is bitwise exclusive-or - not exponentiation. Use ** for exponentiation.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122