Python 2 vs Python 3 in physics

In summary, you should learn Python 3 if you are planning to write new code for computational work. If you are only using Python 2 then you might be okay without learning Python 3. However, if you are using Python 2 and you plan to continue using legacy code written in Python 2 then you should learn Python 3.
  • #1
DataGG
Gold Member
157
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
  • #2
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
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
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
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
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
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/
 

FAQ: Python 2 vs Python 3 in physics

1. What is the difference between Python 2 and Python 3 in physics?

Python 2 and Python 3 are two different versions of the popular programming language, Python. While they have many similarities, there are some key differences that are important to consider when using them in physics. The main difference is that Python 3 is the newer version and is considered to be more modern and efficient. It also has some changes in syntax and features compared to Python 2.

2. Which version of Python is better for physics?

Both versions of Python can be used for physics, but it ultimately depends on your specific needs and preferences. If you are working on a project that already uses Python 2, it may be more convenient to stick with that version. However, if you are starting a new project, it may be beneficial to use Python 3 for its updated features and improved performance.

3. Will my existing Python 2 code work in Python 3 for physics?

Unfortunately, Python 2 code is not always compatible with Python 3. This is due to some changes in syntax and features between the two versions. However, there are tools available that can help with the conversion process, such as the 2to3 tool and compatibility libraries.

4. Can I use Python 2 and Python 3 together in physics?

Yes, it is possible to use both versions of Python in the same project. However, it is important to be aware of the differences between the versions and make sure that any code you write is compatible with both versions. This can become more complicated and may not be worth the effort, so it is usually recommended to stick with one version.

5. Are there any specific physics libraries that only work with Python 2 or Python 3?

Some physics libraries may only work with one version of Python, but many popular libraries have been updated to be compatible with both versions. It is important to check the documentation for the specific library you are interested in to ensure compatibility with the version of Python you are using.

Similar threads

Replies
10
Views
2K
Replies
8
Views
2K
Replies
3
Views
594
Replies
1
Views
927
Replies
13
Views
1K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
17
Views
2K
Back
Top