I add colon and semicolon after print but interpreter is not throwing error.
please run with python3.8.x(edit)
x=5
print:(x)
print;(x)
I add colon and semicolon after print but interpreter is not throwing error.
please run with python3.8.x(edit)
x=5
print:(x)
print;(x)
The interpreter thinks the colon is a type annotation. Which is why it raises SyntaxError
in earlier versions of Python, but is valid syntax in Python 3.6+.
In later versions of Python this is valid
a: int
As is this
import sys
def exclaim(string):
sys.stdout.write(f"{string}!")
print = exclaim
print("Hello")
I.e. You can annotate the type of a variable. And you can reassign print
.
So when you do print:(x)
the interpreter just thinks you're annotating print
to be of "type" 5
.
Semi-colons are valid Python, and are used to put two separate statements on the same line. They're just considered to by "unpythonic". You do see them used sometimes to do things like import pdb; pdb.set_trace()
for print;(x)
, the interpreter treats it like 2 different statements, print
and (x)
. The interpreter prints out "<built-in function print>" and "5". The print function is a built-in function. x is set to 5 so it prints out 5 too.
print:(x)
is a type annotation as said in @Batman's answer