Python 2 vs Python 3 in physics

  • Context: Python 
  • Thread starter Thread starter DataGG
  • Start date Start date
  • Tags Tags
    Physics Python
Click For Summary

Discussion Overview

The discussion centers on the choice between learning Python 2 or Python 3 for applications in computational physics. Participants explore the implications of each version, including library support and compatibility with existing projects.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • Some participants argue that Python 3 is the future and recommend beginners start with it, citing that most libraries are now ported to Python 3.
  • Others contend that Python 2 is still widely used in many projects and that learning it first may not hinder understanding of Python 3.
  • One participant mentions that the choice depends on specific needs, such as existing codebases and the libraries required for computational tasks.
  • Another participant highlights that Python 3 introduced significant changes, but some features have been backported to Python 2.7, which may ease the transition.
  • There is a suggestion to consider Jython for accessing Java libraries while using Python syntax, which could be beneficial for certain computational tasks.
  • A participant expresses frustration over purchasing a Python 2 book, questioning its relevance given the ongoing debate about version preference.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether to learn Python 2 or Python 3 first. Multiple competing views remain, with some advocating for Python 3 while others defend the continued use of Python 2.

Contextual Notes

There are unresolved questions regarding the specific libraries and tools that may influence the choice between Python versions, as well as the varying degrees of compatibility between the two versions.

DataGG
Gold Member
Messages
157
Reaction score
22
Hey,

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?
 
Technology news on Phys.org
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)
 
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:

https://wiki.python.org/moin/Python2orPython3

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.

http://en.wikipedia.org/wiki/Jython

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

http://en.wikipedia.org/wiki/SCaViS

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

www.processing.org

and some contrary discussion here with cool graphics:

http://secondcomingforprocessingpy.blogspot.com/

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:
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.
 
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:
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:
print "Hello, world!"

but in 3.x you'd have to write:

Code:
print("Hello, world!")

Here's a blog that discusses the differences:

http://nbviewer.ipython.org/github/...erences_between_python_2_and_3.ipynb?create=1
 
Last edited:
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.

http://winpython.sourceforge.net/
https://store.continuum.io/cshop/anaconda/
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
8K
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K