Besides the syntactic differences, I do not understand what the difference between import turtle
and from turtle import *
is, as both methods appear to produce the same result:
Method 1
import turtle
t = turtle.Pen()
# rest of program...
Method 2
# Method 2:
from turtle import *
t = Turtle()
# rest of program...
One Internet tutorial I followed used Method 1 and another used Method 2. What is the difference between the two and when should I use each one?
Method 1
You are simply importing the package
turtle
and as you may already know, you are unable to use a variable that has not been declared. Thus, you must reference every item with the package name as a prefix liketurtle.Pen
orturtle.Turtle
.Method 2
You are not importing the package
turtle
and thus you can not use it at all. In fact, you are actually importing every member from the namespace and thus you can just use the names of the items as is as seen inPen
orTurtle
which are defined to be inclusive of the namespace.