r/learnpython • • Jun 28 '20

Can anyone explain why we use Python

I keep reading people say things like ‘because Python is built on C’ and ‘Python is much slower than C++ or Go’, ‘Thats why those languages are used to make most popular games’. I also see people refer to Python as a ‘machine’ language, etc etc. So... why use python then if you can do things faster with another language? And why is Python so popular if this is the case? Seems like python is easier to understand for me at least and much easier to read, but is there some other benefit I’m not understanding? Sorry if this had been asked before, I can’t find any direct comparison information between Python and other languages.

EDIT: Thanks everyone! I feel a much better understanding of why python is fast in a different way from other languages, saving programming time rather than computing time. Definitely got the answers I was looking for.

Another edit: Great insights all around. Thanks to everyone taking the time to answer! Looks like I need to do some more research on Cython as I see a few people mentioning it for when speed is required.

336 Upvotes

120 comments sorted by

View all comments

285

u/[deleted] Jun 28 '20

So... why use python then if you can do things faster with another language?

Because you can write faster than in other languages, and programmer time is way more expensive than computer time.

75

u/saltyhasp Jun 28 '20

I would say the programmers time is often more important than execution speed... but this is not always the case. When it's not the case you write some or all of the code in another language.

0

u/[deleted] Jun 29 '20

[deleted]

5

u/[deleted] Jun 29 '20

Yeah, I write performance sensitive code for financial simulations. Pure Python code could take days to run. For me, the most computationally expensive pieces of code are fairly isolated, so it’s easy to write those parts in C (as extensions) and speed up the code ~100-1000x. The vast majority of it is still in pure python.

4

u/ryrythe3rd Jun 29 '20

run code for literally a year or more of CPU time

Can you explain this? Do you mean by parallelizing computations?

12

u/[deleted] Jun 29 '20

Yes, the wall clock time is much less, because I run it in batches on a supercomputer. Usually on the order of 72 hours. But if you strung the simulations in a line it'd be that long.

6

u/Kriss3d Jun 29 '20

Working in a similar enviorment yes. You can split out tasks on serveral computers. A years worth of CPU time can easily be done in a day or even less depending on how big your cluster is.

2

u/HardstyleJaw5 Jun 29 '20

You aren't doing MD though? As far as I know there aren't any python implementations of it. I just typically use python for analysis myself but every MD code I know is built on C++ for speed cause God knows it needs it.

3

u/[deleted] Jun 29 '20

No, spatial ecological reaction diffusion equations with hundreds of state variables.

2

u/HardstyleJaw5 Jun 29 '20

Ah I've dabbled a little in chemical master equations/rdmes with Lattice Microbes and it was a bear to keep everything sorted. I can only imagine having hundreds of state variables

3

u/[deleted] Jun 29 '20

Many of them I don't actually have to analyze, but they still are part of the system and need to be present for the model to work correctly. The variables I care about are usually species biomasses, and some resource concentrations. But due to the type of model, there is also tracking of many other resources that only might get used by a species--so they're there, and take up CPU time running diffusion calculations on them, but may end up not being examined. Similar with some intracellular information, like reaction fluxes.

0

u/[deleted] Jun 29 '20

[deleted]

11

u/[deleted] Jun 29 '20

I don't recall if it's the most concise way to dive into this but this podcast episode (Talk Python to Me) talks about it.

3

u/PricePadraic Jun 29 '20

Definitely will listen to that, thank you!

2

u/[deleted] Jun 29 '20

Well, runtime resolution of symbol names is slower, typically.

1

u/Kriss3d Jun 29 '20

Because python has extensive sets of libaries for pretty much anything you could imagine. You dont need to sit and program things like making a graph or how to parse excel.

1

u/yensteel Jun 29 '20

It's close to "There's a library for that"

1

u/Kriss3d Jun 29 '20

Exactly.

0

u/[deleted] Jun 29 '20

[deleted]

1

u/Kriss3d Jun 30 '20

Yes that's pretty much how libaries works. Companies that have say a database you can hook into might have a liabry you install and it let's you use the api

1

u/Kriss3d Jun 29 '20

Great answer really. We had a case where alot of bookings of rooms from certain groups and the output of that is alot of excel files thats basically the database of bookings.

One of my coworkers who was tasked to turn alot of these files with entries in serveral thousands into statistics to see who had booked x amount of hours on each room spent a few days concocting the script. Before that some poor clerk had to manually sort and add up all each room and each groups bookings then move on to the next and so on.
It took about a week each time.

But spending 2-3 days on this script. It now takes a few minutes and thats it. For this case it wont matter if it takes 5 or 10 minutes to complete. So python is king here. Because it can run on any system.