Syntax problem - cannot solve it by myself

D

Deadly Dirk

I am a total beginner with Python. I am reading a book ("The Quick Python
Book", 2nd edition, by Vernon Ceder) which tells me that print function
takes end="" argument not to print newline character. I tried and here is
what happens:
File "<stdin>", line 1
print(x,end="")
^
SyntaxError: invalid syntax
What does the error message mean? I am using Python 2.6.5 on Ubuntu 9.10.
 
P

Peter Otten

Deadly said:
I am a total beginner with Python. I am reading a book ("The Quick Python
Book", 2nd edition, by Vernon Ceder) which tells me that print function
takes end="" argument not to print newline character. I tried and here is
what happens:

File "<stdin>", line 1
print(x,end="")
^
SyntaxError: invalid syntax

What does the error message mean? I am using Python 2.6.5 on Ubuntu 9.10.

There are small differences between Python 2.x and 3.x.

print(x, end="")

is Python 3 as is the whole book you are reading. I would guess that is
prominently mentioned at the very beginning?

For the examples to work you have to install Python 3.1 with

$ sudo apt-get install python3

and invoke the interpreter with

$ python3

Good luck with your efforts!

Peter
 
F

fred lore

I am a total beginner with Python. I am reading a book ("The Quick Python
Book", 2nd edition, by Vernon Ceder) which tells me that print function
takes end="" argument not to print newline character. I tried and here is
what happens:


  File "<stdin>", line 1
    print(x,end="")
               ^
SyntaxError: invalid syntax

What does the error message mean? I am using Python 2.6.5 on Ubuntu 9.10.


in python 2.6.5, print is a keyword and this syntax is not allowed
print becomes a real function with few keywords arguments like "end"
only since python 3.0

Daniel
 
R

Robert Kern

I am a total beginner with Python. I am reading a book ("The Quick Python
Book", 2nd edition, by Vernon Ceder) which tells me that print function
takes end="" argument not to print newline character. I tried and here is
what happens:

File "<stdin>", line 1
print(x,end="")
^
SyntaxError: invalid syntax

What does the error message mean? I am using Python 2.6.5 on Ubuntu 9.10.

The Quick Python Book is based on Python 3, which is a major upgrade from Python
2.6 and changed some parts of Python's syntax. In particular, it changed the
print statement to a print() function.

You can install Python 3 on Ubuntu using the python3 package.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
D

Deadly Dirk

I am a total beginner with Python. I am reading a book ("The Quick
Python Book", 2nd edition, by Vernon Ceder) which tells me that print
function takes end="" argument not to print newline character. I tried
and here is what happens:

File "<stdin>", line 1
print(x,end="")
^
SyntaxError: invalid syntax
What does the error message mean? I am using Python 2.6.5 on Ubuntu
9.10.


I figured it out. What I need is the following:

"from __future__ import print_function" at the top of my script.
 
L

Lie Ryan

I am a total beginner with Python. I am reading a book ("The Quick Python
Book", 2nd edition, by Vernon Ceder) which tells me that print function
takes end="" argument not to print newline character. I tried and here is
what happens:

File "<stdin>", line 1
print(x,end="")
^
SyntaxError: invalid syntax

What does the error message mean? I am using Python 2.6.5 on Ubuntu 9.10.

That print function is Python 3.x syntax. In Python 2.x, print is a
statement, not a function and uses completely different syntax.

You can, though, add this switch at the top of your python code:

from __future__ import print_statement

to use the new print syntax in python 2.x

Or, you could download and install Python 3.x.

Or, you could find a book that discusses Python 2.x.
 
E

Ethan Furman

Deadly said:
I am a total beginner with Python. I am reading a book ("The Quick Python
Book", 2nd edition, by Vernon Ceder) which tells me that print function
takes end="" argument not to print newline character. I tried and here is
what happens:

File "<stdin>", line 1
print(x,end="")
^
SyntaxError: invalid syntax

What does the error message mean? I am using Python 2.6.5 on Ubuntu 9.10.

It means the book is written for Python 3.x.

You'll need a

from __future__ import print_function

before you can do the same in 2.6. Not sure, but I seem to recall some
very slight differences between the __future__ version of print and the
actual version in 3.x (although, with my memory, it could easily be one
of the other __future__ items).

~Ethan~
 
T

Thomas Jollans

I figured it out. What I need is the following:

"from __future__ import print_function" at the top of my script.
Yes, that will work, but you should really install Python 3.1 (it's in
ubuntu, as others have said!) because you will almost certainly hit into
other snags. Not as obvious as this one, but they are there. You can
work around all of them, of course, in one way or another...
 
D

Deadly Dirk

Yes, that will work, but you should really install Python 3.1 (it's in
ubuntu, as others have said!) because you will almost certainly hit into
other snags. Not as obvious as this one, but they are there. You can
work around all of them, of course, in one way or another...

The book covers Python3 but my understanding was that it should also
cover Python 2.5 and 2.6.
 
A

alex23

Deadly Dirk said:
The book covers Python3 but my understanding was that it should also
cover Python 2.5 and 2.6.

The "SECOND EDITION Covers Python 3" banner across the top of the
cover would seem to indicate otherwise. The first line of the About
section confirms it:

"This book is intended for people who already have experience in one
or more programming languages and want to learn the basics of Python 3
as quickly and directly as possible."

Unless you have a clear need for 3rd party libraries that currently
don't have 3.x versions, starting with Python 3 isn't a bad idea.
 
D

Deadly Dirk

Unless you have a clear need for 3rd party libraries that currently
don't have 3.x versions, starting with Python 3 isn't a bad idea.

From what I see, most of the people are still using Python 2.x. My reason
for learning Python is the fact that my CTO decided that the new company
standard for scripting languages will be Python. I've been using Perl for
15 years and it was completely adequate but, apparently, Perl is no
longer in. I am afraid that Python3 is like Perl 6, the one with Parrot:
everybody is reading articles about it but nobody is using it.
 
M

MRAB

Deadly said:
for learning Python is the fact that my CTO decided that the new company
standard for scripting languages will be Python. I've been using Perl for
15 years and it was completely adequate but, apparently, Perl is no
longer in. I am afraid that Python3 is like Perl 6, the one with Parrot:
everybody is reading articles about it but nobody is using it.
I won't say nobody. I'm using it, for example. :)

The differences between Python 2 and Python 3 are nothing like those
between Perl 5 and Perl 6; it's more of a tidy-up than a redesign.
 
A

alex23

Deadly Dirk said:
From what I see, most of the people are still using Python 2.x. My reason
for learning Python is the fact that my CTO decided that the new company
standard for scripting languages will be Python. I've been using Perl for
15 years and it was completely adequate but, apparently, Perl is no
longer in. I am afraid that Python3 is like Perl 6, the one with Parrot:
everybody is reading articles about it but nobody is using it.

If you feel that way, then the Quick Python Book 2E is not the book
from which you want to learn Python.

I'm not sure, though, why you feel capable of judging Python 3's
success when you were unable to recognise the book's scope from the
cover, the back text and the introduction. But please, feel free to
use your psychic abilities to tell us what we are and are not using.
 
S

Steven D'Aprano

From what I see, most of the people are still using Python 2.x.


Yes, that is correct, most people are still on 2.x. However, many people
are dipping their toe into 3.x by using both, or even exclusively on 3.
"Most" is not "all".

Remember than many deployed systems only have Python 2.6, 2.5 or even 2.4
as standard, and until the vendors start shipping 3.x as standard, many
people will be stuck using 2.x even if they want to upgrade.

My reason for learning Python is the fact that my CTO decided that the
new company standard for scripting languages will be Python.

And what version of Python will you be using?

I've been using
Perl for 15 years and it was completely adequate but, apparently, Perl
is no longer in.

Yes, being pushed out of a 15 year comfort zone is painful. Good luck!

I am afraid that Python3 is like Perl 6, the one with
Parrot: everybody is reading articles about it but nobody is using it.

Python 3 is actually shipping. While it is a backwards-incompatible
change from Python 2, it is an incremental change and not a complete re-
write. Large amounts of Python 2.x code will Just Work in Python 3, and
even larger amounts can be automatically converted using the 2to3 tool.
Very little needs to be re-written by hand. Most of the changes in Python
3 are additions, not subtractions. As the What's New says:

"you’ll find that Python really hasn’t changed all that much – by and
large, we’re mostly fixing well-known annoyances and warts, and removing
a lot of old cruft."

http://docs.python.org/release/3.0.1/whatsnew/3.0.html

People tend to fixate on things from 2.x that changes, but 3.x also
introduces many new features, like annotations, keyword-only arguments,
nonlocal, dict and set comprehensions, and ordered dicts.


The two biggest roadblocks for Python3.x use are:

* distributions are conservative and are still shipping older versions of
Python;

* while some web frameworks do support Python 3.x, some important 3rd
party libraries still don't (e.g. PIL, numpy).

If you're not using those libraries, or stuck on an old conservative
server, there's absolutely no reason not to start using Python 3.1.
 
J

Jorgen Grahn

But see below.
From what I see, most of the people are still using Python 2.x. My reason
for learning Python is the fact that my CTO decided that the new company
standard for scripting languages will be Python.

Not a bad choice.
I've been using Perl for
15 years and it was completely adequate but, apparently, Perl is no
longer in.

I hope your CTO still lets you use Perl for the things Perl does
better (like quickly and elegantly parse huge text files, and various
one-liners). For many other tasks, I think you will quickly find
Python superior.
I am afraid that Python3 is like Perl 6, the one with Parrot:
everybody is reading articles about it but nobody is using it.

It seemed like that for a year or two (when people regularly called it
"Python 3000"). Now it's in use -- although perhaps not so much as you
would think when you read comp.lang.python.

I am still perfectly happy with Python 2.4 and 2.5. These are the
versions which are installed by default in modern, recent Linux
distributions. I bet it will be years before Python 3 replaces them.

/Jorgen
 
A

Alister

From what I see, most of the people are still using Python 2.x. My
reason for learning Python is the fact that my CTO decided that the new
company standard for scripting languages will be Python. I've been using
Perl for 15 years and it was completely adequate but, apparently, Perl
is no longer in. I am afraid that Python3 is like Perl 6, the one with
Parrot: everybody is reading articles about it but nobody is using it.

If the CTO is seting Python as company standard he should also be
specifying which version!

even without the BIG changes between 2.XX & 3.XX not specifying a version
could lead to trouble.




--
Sometimes when I get up in the morning, I feel very peculiar. I feel
like I've just got to bite a cat! I feel like if I don't bite a cat
before sundown, I'll go crazy! But then I just take a deep breath and
forget about it. That's what is known as real maturity.
-- Snoopy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top