Python 2 vs Python 3 in physics

  • Python
  • Thread starter DataGG
  • Start date
  • #1
DataGG
Gold Member
156
22

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
cgk
Science Advisor
521
42
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)
 
  • #3
11,882
5,533
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:
  • #4
536
35
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.
 
  • #5
DataGG
Gold Member
156
22
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:
  • #6
11,882
5,533
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:
  • #7
990
57
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/
 

Related Threads on Python 2 vs Python 3 in physics

Replies
18
Views
4K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
13
Views
37K
  • Last Post
Replies
5
Views
1K
  • Last Post
Replies
2
Views
4K
Replies
10
Views
927
Replies
7
Views
568
Replies
4
Views
1K
Top