Python 2 vs Python 3 for Computational Tasks

In summary: I'm not sure what that means.This is the point where many people who are considering learning Python switch to Python 3 because packages like numpy and scipy do work with Python 3.x. However, it is important to note that this is not always the case, as there have been a number of changes made between Python 2.x and 3.x which can make some code incompatible.
  • #1
thegreenlaser
525
16
I've decided to learn Python (having a background in C++ and Matlab), and I'm a little unsure about which version to get. I've read in a few places that Python 2.x is better because packages like numpy and scipy don't work with Python 3.x, but this seems to be out of date since as far as I can tell, these packages do support Python 3.x now. I plan to do mostly do computations (e.g. modeling/simulation) and then visualizations of those computations. Have most of the important packages for this type of thing been ported over to Python 3.x or am I safer going with Python 2.x?
 
Technology news on Phys.org
  • #2
https://python3wos.appspot.com/ may help you with your decision. In my lab we use Python 2 since it's more supported. Python 3 is a cleaner, better language, but Python 2 still has a lot of inertia.

My advice would be to go with Python 3 if you can, keeping in mind much of the available Python code is still targeted at Python 2.

If you do use Python 2, it's good practice to write code that is forward-compatible with Python 3. A simple example is the print functionality, which is a statement in Python 2, used as:
Code:
[color=#008000][b]print[/b][/color] foo
but a function in Python 3:
Code:
[color=#008000][b]print[/b][/color](bar)
The Python 3 functionality can be acheived by calling
Code:
[color=#008000][b]from[/b][/color] [color=#0000FF][b]__future__[/b][/color] [color=#008000][b]import[/b][/color] print_function
[color=#008000][b]print[/b][/color](bar)
There are other changes between the versions which you can find online.
 
  • #3
jhae2.718 said:
My advice would be to go with Python 3 if you can, keeping in mind much of the available Python code is still targeted at Python 2.
That's funny...when I started to read the advice and thought the opposite was going to be recommended. Since it wasn't...I will recommend the opposite.

Kind of learn Python 2 and 3 in a encompassing way so that you write Python 2 in a 3-compatible manner...that way, you know for sure that no module is going to stop you from doing your work; THEN, when all modules have been ported and 3 is as complete as 2 is today...your code should be easy to port...I thought at some point they were writing a program that would convert your Python 2 code to Python 3...I bet it works good when Python 2 has already been written with Python 3 in mind.
 
  • #4
This is the sort of issue that keeps "not for profit" software as a minority interest, unfortunately.

If the inventors of Python 2 have invented a "better" but incompatible language, good for them. But calling it "Python 3" and assuming that the rest of the Python community would instantly drop whatever else they were working on and port all their existing code was somewhere between "dumb" and "naive" IMO.

As the old proverb says, sometimes "the best is the enemy of the good".
 
  • #5
gsal said:
...I thought at some point they were writing a program that would convert your Python 2 code to Python 3...I bet it works good when Python 2 has already been written with Python 3 in mind.

There's 2to3, or something similar, but I don't know how well it works.
 
  • #6
AlephZero said:
This is the sort of issue that keeps "not for profit" software as a minority interest, unfortunately.

Hhhhmmm...I don't know, AlephZero, I think I could argue that you may have it backwards.

It is precisely because Python is open source and "not for profit" that thegreenlaser actually has a choice! Nobody is forcing s/he to pick one or the other or to "move forward" onto a specific version. If it was proprietary software, licensed, etc...s/he could very well have no choice but to start using the present version of a software regardless of base, popularity, bug-iness, etc. and simply because the previous one is no longer available.

Anyway, that's just one of the things software freedom is about.
 
  • #7
AlephZero said:
As the old proverb says, sometimes "the best is the enemy of the good".

Er, that proverb usually refers to the situation where people reject using a good solution in favor searching for a mythical best solution -- not the situation where people are using something good and then something better comes along.
 
  • #8
thegreenlaser said:
I've read in a few places that Python 2.x is better because packages like numpy and scipy don't work with Python 3.x, but this seems to be out of date

By no means!.. Python 2 is the most common version for all uses from commercial software to scientific purposes and Python 3 is a future version and no one knows when it will be widely used, and most professional Python programmers use Python 2 these days.

In addition, More versions of Python 2 are still released, what tells that Python 2 will not be out of date soon!
 
  • #9
ultrasmart said:
More versions of Python 2 are still released, what tells that Python 2 will not be out of date soon!

Python 2.7 will be the last major release in the 2.x series, which is going into what the PSF is calling an "extended maintenance period."

http://www.python.org/download/releases/2.7.3/
 
  • #10
Thanks everyone. I guess there's no real clear consensus on this issue, so I've decided to go with Python 2 just because of bigger library and better supporting documentation. As a casual programmer doing stuff for myself I'm guessing it won't make all that much of a difference in the end.
 
  • #11
thegreenlaser said:
Thanks everyone. I guess there's no real clear consensus on this issue, so I've decided to go with Python 2 just because of bigger library and better supporting documentation. As a casual programmer doing stuff for myself I'm guessing it won't make all that much of a difference in the end.

I would just check what are the major differences to keep them in mind while programming, so that the code would be easier to port in future (in case it will be necessary).
 
  • #12
Sorry for digging up an old thread, but I wanted to ask the same question, only I have no experience with programming at all (besides a little bit of Matlab), and I want to start by learning Python. I can't really tell from this thread which version is best, lol.

On their website they say

If you don't know which version to use, try Python 3.3. Some existing third-party software is not yet compatible with Python 3; if you need to use such software, you can download Python 2.7.x instead.

Since I'm just starting out, I suppose the 3rd-party stuff is not important, but from what I gather, version 2 is still the most commonly used, so which do I get?

Thanks.

Edit: saying "if you don't know, try Python 3.3" sounds a little sneaky to me...
I did read this page, and it's all Greek to me, no help at all.
 
  • #13
qspeechc said:
Edit: saying "if you don't know, try Python 3.3" sounds a little sneaky to me...

Sounds quite logical to me, and I believe it to be the correct approach (even if my experience with Python is very limited).
 
  • #14
Borek said:
Sounds quite logical to me, and I believe it to be the correct approach (even if my experience with Python is very limited).

I was referring to the language they chose to use.
"Here, try this..."
"What is it?"
"Just try it..."
I would feel more assured if they said "If you don't know which version to use, download 3.3.1" or something like that, but that's being silly, I know.
 
  • #15
AlephZero said:
assuming that the rest of the Python community would instantly drop whatever else they were working on and port all their existing code was somewhere between "dumb" and "naive" IMO.

I second that.

For my business, the most tragic victim of such a "drop" would be our usage of omniORBpy for CORBA prototyping and development scripting.

No way.
None at all.
 
  • #16
I have still found this thread of very little help. Accidentally I found this, from this page (under "Warnings for Beginners"):

A programmer may try to get you to install Python 3 and learn that. You should tell them, "When all of the python code on your computer is Python 3, then I'll try to learn it." That should keep them busy for about 10 years.

Unfortunately, when I read that, I had already downloaded Python 3 and started learning it with another book written for Python 2.

I just wanted to add this for other beginners who can find no guidance on this topic.
 
  • #17
Have you run into any problems because you use 3.3?
 
  • #18
The author of the book I'm using says it can be used by both Python 3 and 2 users, but some of the things in the book do not work in Python 3, so it seems he doesn't even use Python 3. It's quite a pain trawling the internet trying to see how things should be done in Python 3, otherwise you have to figure it out on your own, which is terrible for a complete beginner.

Most of the help for beginners is actually written for other programmers, i.e. it's not actually comprehensible to real beginners. And most of the stuff on the web seems to be for Python 2 as well.

That's the end of my rant, thank you, lol.
 
  • #19
Somewhat offtopic
AlephZero said:
This is the sort of issue that keeps "not for profit" software as a minority interest, unfortunately.

I think one of the odds "not for profit software" often suffers from is missing the right point for a platform freeze.

Some features of higher versions may be handy, but more often than not, the alleged need for a platform feature is in fact a design problem.

The most compelling case of recent days is
http://en.wikipedia.org/wiki/Lightspark

I really would fancy an alternative to adobe development environment, esp because, being a Linux guy, I don't know know yet if I should mourn or applaud the enforced "transition" from official support to pepperflash-only.

But compiling Lightspark would force so much need for updates and replacements on us, that I would hesitate to give a go for that - I simply cannot rule out side-effects when I replace core components of our current "Squeeze" platform.

That's quite a pity, and esp because I somehow doubt that all feats of Lightspark's platform are really needed for it.



S.
 

1. What are the main differences between Python 2 and Python 3 for computational tasks?

The main differences between Python 2 and Python 3 for computational tasks include the way strings and Unicode are handled, the print function, integer division, and the use of the xrange function in Python 2 which is replaced by range in Python 3.

2. Which version of Python is better for computational tasks?

It depends on the specific task and the libraries being used. Generally, Python 3 is recommended as it is the newer version and has more support and updates. However, some libraries may still be more compatible with Python 2.

3. Can code written in Python 2 be used in Python 3 for computational tasks?

Yes, but it may require some modifications. Python 2 code can be converted to Python 3 using tools such as 2to3, but some changes may need to be made manually.

4. Are there any performance differences between Python 2 and Python 3 for computational tasks?

In general, there are not significant performance differences between Python 2 and Python 3 for computational tasks. However, Python 3 may have some performance improvements in certain areas.

5. Is it necessary to switch from Python 2 to Python 3 for computational tasks?

It is not necessary, but it is recommended to switch to Python 3 as support for Python 2 will end in 2020. This means that no further updates or bug fixes will be provided for Python 2, making it less secure and potentially causing compatibility issues in the future.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
878
  • Programming and Computer Science
Replies
3
Views
919
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top