[Tutor] a newbie question [identifiers can't contain colons]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Thu, 23 Aug 2001 20:56:36 -0700 (PDT)
On Thu, 23 Aug 2001, Daniel Coughlin wrote:
> try this:
>
> >>> execfile("c:\\download\\practice python\\p17.py")
>
> note the quoatation marks and two backslashes. backslash is a special
> character; so, you need to use two to tell python that they are there.
Also, it should be ok to use forward slashes to separate your directories:
execfile("c:/download/practice python/p17.py")
We need the quotes around "c:/download/practice python/p17.py" because we
need to tell Python to take that the whole thing as a piece of quoted
string, and not really look into it for some hidden meaning. Otherwise,
Python will go into contortions trying to figure out what we mean.
Let's take a closer look:
###
>>> c:\\download\\practice python\\p17.py
File "<stdin>", line 1
c:\\download\\practice python\\p17.py
^
SyntaxError: invalid syntax
###
When we type an unquoted name into Python, Python assumes that we're
trying to evoke the name of a variable or function, or do some other funky
thing. In this case, Python will think it's seeing the beginning of a
name. That's why it's underlining the colon: names can't contain colons.
###
>>> colon = "colon" # "colon" can be a name
>>> : = "colon" # but not ":".
File "<stdin>", line 1
: = "colon"
^
SyntaxError: invalid syntax
##
Later, you'll find out that ':' has a special meaning in Python: we use it
when we begin to "indent" inner blocks in our code. Here's an example:
###
if question == "What is the meaning of life?":
print "42"
###
Hope this explains why Python gave that particular SyntaxError. Good
luck!