Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Python 2 vs Python 3 in physics

  1. Jul 10, 2014 #1


    User Avatar
    Gold Member


    This might sound like something that I should have posted to the "computing & technology" forums, but I disagree.

    I know that beginners should probably start with Python 3 instead of python 2, as "python 2 is legacy and python 3 is the future".

    I already bought a book on python 2, so I'm questioning if I should throw it away and buy a python 3 book instead. The question is:

    Should one learn python 2 or python 3 if one wants to apply that to computational physics?
  2. jcsd
  3. Jul 10, 2014 #2


    User Avatar
    Science Advisor

    At this moment the thought that "python 3 is the future" seems more of a wishful thinking of the authors than a fact to me. The vast majority of python projects---including new ones---are written in python 2.x, and I see little movement to change that. That is: Python 2 is fine---for physics and everything else. Also, python 3 is only slightly different from python 2, so there is no reason to not learn the latter first.

    But you should read a intro to python 3 first, so that you can avoid deprecated features in preference of improved replacements (e.g., avoid %-operator for string formatting in favor of str.format or avoid map/filter in favor of list comprehensions/generator expressions; the latter being the new variants which have various advantages)
  4. Jul 10, 2014 #3


    Staff: Mentor

    Use Python 2 vs Python 3 is dependent on what you're trying to do, what was written before and what extensions you're planning to use for the problem at hand.

    There's a discussion about the virtues of Python2 vs Python 3 in this article:


    If you're planning to write new code for computational work then you'll want to consider numpy which has been ported to Python3.

    Alternatively, you could consider Jython which is Python running on the Java VM. Its main draw is that you can access any Java library seamlessly while still writing in the Python paradigm. Java has a HUGE collection of libraries that cover everything you can imagine in computing except for maybe FTL processing.


    Jython is used in SCaVis, a scientific computing environment that may be of interest to you:


    The Processing IDE has a Jython mode where you can write scripts that use OpenGL graphics which is pretty cool:


    and some contrary discussion here with cool graphics:


    Lastly, don't worry so much about where your post goes, let the PF librarians and mentors decide that if they feel its better under Computing. I believe its a better choice with far more visibility.

    PF is a curated forum where things are better organized most other forums on the web and is well maintained by a lot of unpaid volunteers.
    Last edited: Jul 10, 2014
  5. Jul 10, 2014 #4
    You should use Python 3. I am using it for the just thing you said.

    Almost all libraries are already ported to Python 3, so there's really no problem with that.

    The only reason to use Python 2 is when you are going to continue some legacy project written in Python 2 and you don't want to port it.
  6. Jul 10, 2014 #5


    User Avatar
    Gold Member

    Damn! Why did I buy this stupid python 2 book!

    EDIT: Maybe what my book teaches is introductory enough that it doesn't make much of a difference if it's python 2 or python 3? The name of the book is: "Learn Python The Hard Way, 3rd edition"
    Last edited: Jul 10, 2014
  7. Jul 10, 2014 #6


    Staff: Mentor

    I think you'll be okay with the book. From the discussions I've seen Python 3 was such a big jump that some of its features were backported to Python 2.7 and 2.7. There are also flags for Python3 compatibility checks and some tools to convert Python2 to Python3.

    The major area of differences is in how some internal things work, how unicode strings are handled and how exception processing is handled.

    One simple difference is that in 2.7 you could write:

    Code (Text):

    print "Hello, world!"
    but in 3.x you'd have to write:

    Code (Text):

    print("Hello, world!")
    Here's a blog that discusses the differences:

    Last edited: Jul 10, 2014
  8. Jul 10, 2014 #7
    Enough of the Python scientific computing infrastructure is now working with Python 3 that I'd suggest any beginner start with Python 3, unless you know you need some package that hasn't made the transition yet.

    For windows and mac users I would suggest installing a scientific computing stack like winpython or anaconda, which gather all the dependencies for you.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook