-1

So I started learning python 3 and I wanted to run a very simple code on ubuntu:

print type("Hello World")
         ^
SyntaxError: invalid syntax

When I tried to compile that with command python3 hello.py in terminal it gave me the error above, but when used python hello.py (I think it means to use python 2 instead of 3) then it's all fine. Same when using python 3 and 2 shells in the terminal.

It seems like I'm missing something really stupid because I did some research and found no one with the same issue.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
Ilnicz
  • 15
  • 1
  • 2

3 Answers3

2

In Python3, print was changed from a statement to a function (with brackets):

i.e.

# In Python 2.x
print type("Hello World")
# In Python 3.x
print(type("Hello World"))
Coder
  • 1,175
  • 1
  • 12
  • 32
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
1

In Python 3.x print() is a function, while in 2.x it was a statement. The correct syntax in Python 3 would be:

print(type("Hello World"))
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

This is because from Python 3, print is a function, not a statement anymore. Therefore, Python 3 only accepts:

print(type("Hello World"))
fractals
  • 816
  • 8
  • 23