Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need to learn Python (v2.7) for my job, and looking for the best intro book for professional programmers.

I found (via amazon) that "The Quick Python Book" is the best, but it's for Python 3.1

I know there's a lot of similarities in 2.7 and 3.1, and somewhere read that you can mostly use 3.1 syntax in 2.7 as a good "future practice".

Can someone with experience please verify that a book for learning Python3 would still be directly applicable for 2.7?

Thank you very much.

edit: "The Quick Python Book" is for 3.1

share|improve this question
    
note: this is for books on learning the base language structure and syntax. I can understand that books emphasizing various frameworks wouldn't be so applicable to 2.7 and 3.0 –  JasonS Nov 19 '12 at 8:50
    
A Python 3 book will lead you to type things that don't work in Python 2.7. Silly, that. –  Don Gateley Nov 24 '12 at 4:28
add comment

3 Answers

up vote 6 down vote accepted

I'd stick with a Python 2 book instead.

Yes, a lot of changes applicable to Python 3 have been back-ported, and with each new 3.x release the transition from 2.x to 3.x is made easier. But a Python 3.x book will not tell you how to adapt your code to work in Python 2.7. Your Python 3.x code will have surprising effects when run by a Python 2.x interpreter.

Take the print() function, for example. In Python 2, print is a statement, so you normally would write print somevalue and that'd print the contents of the variable somevalue to stdout. In Python 3.x, you'd write print(somevalue) instead, as print() is a function there.

However, writing print(somevalue) also works in Python 2.x, but Python interprets that as the expression (somevalue) to be printed to stdout, instead of the expression somevalue being passed as an argument to the print() function. And that is subtly different.

The distinction becomes more apparent when more than one value is involved; print(onevalue, anothervalue) means, in Python 2.x, to print the tuple (onevalue, anothervalue), while in Python 3.x this means something different, namely to pass two arguments to the print() function, and each one has to be printed with the default separator (a space), which is a very different operation:

# Python 2.7
>>> print(1, 2)
(1, 2)

# Python 3.3
>>> print(1, 2)
1 2

So, although the code looks the same, the effect is quite different.

If you want to write code that works consistently across both major python versions, you'd actually need to do the following:

# Python 2.7
>>> from __future__ import print_function
>>> print(1, 2)
1 2

The __future__ import statement there was added to Python 2.6 and newer to facilitate the transition, to make it easier to write code that'll work the same in both Python 2.x and 3.x. In any module that imports print_function, the print statement is replaced by a print() function. Now, print(onevalue, twovalue) behaves the same way it would in Python 3.x.

The print vs. print() change is but one of many changes between the major versions though. Others require a deeper understanding of the language still, and place much heavier demands on someone just starting out with Python.

To give you an idea, here is a super-compacted and incomplete overview: I/O has been overhauled, the default string types have been switched, dict methods return views instead of lists, other built-in methods return iterators instead of lists, ordering comparisons have been simplified, the behaviour of the division operator has changed, octal literals have been tightened, you can specify that keyword arguments to a function can only be called using keyword arguments (and not as positionals), a new nonlocal statement to handle nested scoping beyond global vs. local, the import system has been overhauled and made more introspectable and hookable, many standard library modules have been moved around and reworked, etc., etc., etc.

If your job requires you to learn Python 2.7, then focus on that. The transition to 3.x will come later, with more experience of what Python is about.

share|improve this answer
    
Regarding the change in the syntax and semantics of print: what was the motivation for the change? –  Giorgio Nov 19 '12 at 11:45
4  
@Giorgio: See PEP 3105: it should always have been a function, to make it a statement was a mistake. More detailed motivations are laid out in the PEP. –  Martijn Pieters Nov 19 '12 at 11:51
    
Thanks for the link. –  Giorgio Nov 19 '12 at 12:09
add comment

Have a look at Appendix A. Porting Code to Python 3 with 2to3 from Dive Into Python 3 by Mark Pilgrim. It a kind of summarizes the syntactic differences in a brief form.

share|improve this answer
add comment

Well, I personally find all the little differences between 2.7 quite annoying (from print statement to exception handling). As such, I would say, if you work with 2.7 and have two equally good books available, take the book for 2.7 ...otherwise, if the 3.0 book is really that much better, you may perhaps take it.

It also perhaps depends if your target is to make a lot of small utility scripts or more a full scale application. If it's about making scripts, the 2.7 "recipe" book would be more comfortable in the long run I guess. Otherwise, a 3.0 would be fine as well since the bigger concepts transfer well.

share|improve this answer
    
thanks for the reply, i know that in python 2.7, some api's work in both the 2.x and 3.x way, though i don't know if print is one of them. –  JasonS Nov 19 '12 at 9:51
    
fyi, i just tried, and both of the following work in python 2.7 print("Hello, World") print "Hello, World" –  JasonS Nov 19 '12 at 10:54
1  
@jaysun: in this case, it is the same, but as Martijn says, for print(1,2) it would be different –  arnaud Nov 19 '12 at 12:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.