Python Python 2 vs Python 3 in physics

  • Thread starter Thread starter DataGG
  • Start date Start date
  • Tags Tags
    Physics Python
AI Thread Summary
The discussion centers on the choice between learning Python 2 or Python 3 for computational physics. It emphasizes that while Python 2 is still prevalent in many projects, Python 3 is considered the future due to its ongoing support and improvements. Beginners are encouraged to start with Python 3, as most libraries, including numpy, have been ported to it, making it suitable for new code development. The conversation highlights that Python 3 introduces significant changes, particularly in string handling and syntax, which can affect how code is written. However, it acknowledges that foundational concepts in Python 2 can still be relevant for beginners. Resources like Jython and scientific computing stacks such as WinPython and Anaconda are suggested for those interested in integrating Java libraries or simplifying dependency management. Overall, the consensus leans towards starting with Python 3, especially for new projects in computational physics.
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
Views
3K
Replies
8
Views
2K
Replies
42
Views
5K
Replies
5
Views
2K
Replies
17
Views
3K
Replies
2
Views
2K
Back
Top