Putting ':' after print does not return an error in python 3?

Viewed 28

The following loop does not return a syntax error, instead it runs forever. Why?

x = 1
while x <= 10:
  print :x=x+1
1 Answers

If we properly format that code, we get:

x = 1
while x <= 10:
  print: x = x + 1

So this is syntactically correct Python, but x is used as a type annotation (like x: int = 1).

Type annotations don't do anything on runtime but can be used by tools like mypy to help find bugs in your code.

So this code really boils down to:

while True:
    print = 2
Related