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

266

u/K900_ SRE person // please don't DM me your questions Jun 28 '20

It's not about Python being faster or slower, it's about the time it takes you to build something. Python is totally fine, performance wise, for building lots of things. Yes, C can be faster, but you can build a web app in an hour in Python, and it will take you days in C.

33

u/PricePadraic Jun 29 '20

That’s crazy. I have noticed that learning Python as my introduction to programming has definitely spoiled me in a way that is hard to quantify. Now going through a JavaScript course and I keep catching myself thinking... wow this seems like a stupid way of doing this...

52

u/szirith Jun 29 '20

To be fair, JavaScript is pretty stupid.

11

u/keeldude Jun 29 '20

I think it seems stupid at first, but a deep dive into it will reveal many use cases.

7

u/exhuma exhuma Jun 29 '20

I don't write a lot of JS, only a tiny bit for some hobby projects, but clearly JS is an absolutely viable language.

But it requires discipline.

It's not that JS is a stupid language. It's that it let's you do stupid things. And a lot of beginner code is absolutely horrid in JS.

That's not to say that it has no silly corners, like the binding of this is unintuitive. But every language has ugly bits. Even Python.

3

u/szirith Jun 29 '20

Never said it wasn't useful. Just stupid. It is incredibly portable and it's designed like a fucking Jenga Tower.

1

u/absumo Jun 29 '20

And, insanely inefficient for the sake of that portability.

8

u/kor_the_fiend Jun 29 '20

Python "spoils" you mostly through its type system (or lack there of). Writing code in a strongly typed language will feel laborious at first, but eventually those Python "bad habits" will fade away (if you ever move to a strongly typed language which is not a given I suppose). I guess what I mean to say is that using Javascript as a benchmark for judging Python might be missing the point.

9

u/msondergaard Jun 29 '20

Maybe I've misunderstood your point here, but Python is a strongly typed language.

7

u/chason Jun 29 '20

Additionally, Javascript is weakly typed which makes the statement all the stranger.

2

u/Kelak1 Jun 29 '20

Do you think he means that you don't have to declare variable type?

3

u/smurpau Jun 29 '20

I mean, Python does have static type checking with mypy. It's just optional, unlike most languages.

2

u/hugthemachines Jun 29 '20

Python "spoils" you mostly through its type system (or lack there of).

Python has types.

Writing code in a strongly typed language will feel laborious at first

Python is a strongly typed language. Try this in Python: myvariable = "foobar" + 3

Python has dynamic typing though.

87

u/thrallsius Jun 29 '20

it's about the time it takes you to build something

it's not just that, it's the process itself as well. writing code in python is generally more pleasant, because python is closer to human languages and our thought process

17

u/Kriss3d Jun 29 '20

That in turn also makes it much easier to maintain.

15

u/[deleted] Jun 29 '20

Wondering if you might be able to share your feedback. I still don't understand the concept of building a web app with python. I know PHP quite well, I can put html right into a php file and run it right on the browser with dynamic variables OR use php to output json for a vue app.

Python can't seem to do either of these, it's straight up a command line/server language? How do you actually output html or web apps? People always talk about it being fast but it has no output that can be read by a browser?

35

u/kramrm Jun 29 '20

Take a look at Flask or Django frameworks.

17

u/oefd Jun 29 '20

I can put html right into a php file

PHP's behaviour outside of the tags html goes here<?php this_is_code(); ?>more html is effectively the same as

echo 'html goes here';
this_is_code();
echo 'more html';

PHP is unlike basically all other languages in that it has a 'magical' auto-printing mechanism outside of the <?php ; ?> tags, but that's just a difference in how it's written. It's the same behaviour as just printing output using php facilities like echo

In python your http framework like flask effectively just use print() and other facilities for writing output to write things to the socket which connects you to the client's browser. That's the same as what PHP does with your stuff outside of <?php ; ?> tags .

4

u/[deleted] Jun 29 '20

Right but it just seems so insanely easier. I guess I just need to understand python better but I see PHP much faster for rapid development. I've built apps that have generated a lot of money with just PHP/html, it just seems so clear to have your html right in front of you with loops and variables. Python I am assuming you have to print all of your html using a print statement and then concenate everything? Seems like it would be hard to read and write.

Although I am new, I am likely missing something. Just explaining why I have resisted Python so long. I was the same way with using jquery but once I learned VUE I was like "how did I live before this" lol

8

u/ravepeacefully Jun 29 '20

Python is WAY faster and more pleasant to write.

I can make a web app in an hour or 2 in either language with multiple frameworks.

Use a templating engine, jinja is ideal.. something like this..

It’s very similar to php except it doesn’t suck for fitting in variables in an elegant fashion. It’s also preference, they both work well, python does have better libraries though, which is why I normally elect to use python

<ul>
{% for key, item in dict.items() %}
    <li>{{  item[‘detail’]  }}</li>
{% end for %}
</ul>

3

u/oefd Jun 29 '20

it just seems so clear to have your html right in front of you with loops and variables

You can still have that, python can and does support templating languages. Most popularly jinja which you can use with the web frameworks. I'd argue that it's considerably easier to use that for templating html because it's much better optimized for writing just templates instead of being a full programming language with templates glued on as well.

The benefits are big enough that PHP's most popular web frameworks use templating engines too - PHP's original design as a template engine is 25 years old now, and in the intervening years a few good practices were settled on that basically all current templating engines now support in one respect or another, so even PHP has templating engines to let people get at this quality-of-life and security improvements.

Python I am assuming you have to print all of your html using a print statement and then concenate everything? Seems like it would be hard to read and write.

It would be, but just like PHP does all that under the hood for you so too do python frameworks. Your experience as the user is generally going to be writing HTML in templates as per above and your code grabs the GET params or whatever from a request, does the business logic, and returns something like render(the_template, args_for_the_template) which uses your template file.

3

u/thblckjkr Jun 29 '20

Writing html inside php is easier at first. But, it doesn't scale well.

When you have a couple of forms with bootstrap and some MySQL database is ok'ish.

But when you need, let's say vue, and templates or something like that, it makes writing code extremely difficult,and maintaining it is really painful as well.

So, at the end you end up using some framework (Django, Laravel, Symphony, etc) that abstracts most of the code and you end up writing basically the same code in every language, with some just tweaks to take full potential of what a specific language offers.

2

u/Zila768 Jun 29 '20

No you write Html is almost the same way you would write it normally but with some different syntax where you need, take a look at Jinja 2 it’s the html like syntax that helps python feed your website data.

8

u/PyTec-Ari Jun 29 '20 edited Jun 29 '20
  1. A script written in Python will run on a server somewhere.

  2. This server has a public IP and a port to listen for connections on (generally).

  3. When a connection comes in the script is written in a way to know how to handle the connection.

  4. Generally it will take direction from the URL requested and any parameters

    1. eg. example.com/user/johnsmith

  5. The script can interpret the end point /user/<username> and understand what to do with the parameters.

    1. Maybe connect to a database and get the details for the given username

  6. It will then compile the required HTML from a template

  7. It will then insert any data it needs to into this template

    1. eg. <h1>Hello {{ username }}</h1> <p>{{ data }}</p>

  8. It sends this compiled HTML file back to the user requesting it.

  9. On the user end they get the page they're after with the data they were expecting.

The good news is you don't need to write all the code to do that because someone already has. Flask, Django, and Bottle are all really great webframeworks for python. As a beginner I would start with Flask or Bottle, if you can work with them you can work with Django. I find Django is a bit too verbose for learning 'Hello World' type stuff.

Here is all you need to start in Flask:

# main.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():
    return "<h1>Hi there!</h1>"

if __name__ == "__main__":
    app.run(host='localhost', port=5000)

That's it!, install python, pip, and flask and you now have a web server serving HTML.

>> pip3 install flask
>> python3 main.py
>> listening on http://127.0.0.1:5000

Go to http://127.0.0.1:5000 in your browser and you should see the 'Hi there!' show up.

1

u/mnkisala Jun 29 '20

But also if you need speed nothing blocks you from writing a python module in C/C++, getting best of both worlds.

1

u/yensteel Jun 29 '20

In addition, lots of little technical details are handled automatically in Python. Memory management and pointers are tricky to deal with in C++ but are handled seamlessly in Python.

Another example is managing and flushing buffers. There's a lot of gotchas in there. A cin >> input followed by getline() would crash the program if not flushed beforehand. (Just learning recently. This kind of technicality is confusing and I might be wrong.)

There may be nothing wrong with the program logically but there's more attention to detail required with C++.

1

u/[deleted] Jun 29 '20

What if I convert my .py file to .exe . Does it increases the speed?

78

u/[deleted] Jun 28 '20

YouTube was bought by Google at the expense of its own development efforts on Google Video.

The YouTube team was smaller than the Google Video team but was turning out features faster.

Guess which languages the respective teams used.

Python remains a critical language at Google. Sure, they use other languages for performance/resource critical tasks, and even created some of their own (such as Go) but Python remains key.

The vast majority of startups providing cloud based services use Python at least for prototyping and often for production.

Are you aware that most of Instagram is Python based, using the Django web framework?

Python is proven at truly massive scale. It is far from perfect and not always the best choice but it is very flexible hence its popularity in so many fields.

Even on fields where high compute performance is required, Python is often used for orchestration or glue. For the pipeline in the generation of movie special effects, for example.

5

u/PricePadraic Jun 29 '20

Didn’t know that, thank you for the Info! Is there any other language that stands with python or above python in its scalability?

4

u/[deleted] Jun 29 '20

That's a hard question to answer. Most of the popular languages are highly scalable and responsible for some huge monolithic applications.

You might be surprised to find how many critical systems, especially in the insurance, pensions and.finance industry are written in COBOL. Even if they have been hidden from view, wrapped in modern clothing

So much of the modern application landscape is really a complex enterprise architecture of inter-dependent systems written in a variety of languages.

The obvious candidate is Java, hugely popular for corporate work. Also, historically the primary app language for Android (now replaced with Kotlin). Scala has become very popular (it typically runs on the same Java Virtual machine as Java).

JavaScript, especially server side, such as node.js, is very common these days, especially for web front ends and APIs.

C has to be king though although even Microsoft are looking to moving away from it and its variants with candidates such as Rust looking good.

Modern AAA games tend to.be written in C++ (and C#), which has significant development benefits over C for a small performance penalty.

So the answer is, it depends.

2

u/PricePadraic Jun 29 '20

Thank you for the elaboration. You seem to know your stuff despite calling yourself a ‘hobbyist’. Appreciate the info.

2

u/[deleted] Jun 29 '20

The hobbyist tag applies to my skills in Python and my use of it for my personal interests.

I've been a senior IT professional for decades, leading teams at times of well over 100 people in multiple countries on major enterprise system developments, integrations and implementations. I was a professional programmer a very long time ago, using assembly language and Fortran.

I gave up living in hotels and flying around all the time for a better work life balance so had time for hobbies. They included learning to programme again and taking up electronics.

I am though still working, now for a large government department, and am responsible for on premise and private cloud hosting as well as being part of strategy team for public cloud usage.

1

u/kor_the_fiend Jun 29 '20

Only performance penalty for using C++ over C is developer related. There is nothing native to C that cannot be replicated with C++. C provides a more restrictive programming environment that perhaps attracts more skilled developers, which contributes to the mistaken assumption that C is "faster" than C++.

2

u/kor_the_fiend Jun 29 '20

For my money Go hits the sweet spot in terms of performance both from a scalability/compute perspective and from a developer resource perspective. Low barrier to entry and some impressive performance stats out of the box.

0

u/[deleted] Jun 28 '20

[deleted]

2

u/painya Jun 29 '20

Probably with C++ bindings, doesn’t make your point any less true though!

186

u/socal_nerdtastic Jun 28 '20 edited Jun 28 '20

In a very condensed nutshell: Python is fast to program, but slower to run.

In the modern world where computer power is getting very cheap but hiring programmers is getting very expensive, it often makes a lot of sense to buy more computers rather than pay more programmers.

27

u/PricePadraic Jun 29 '20

Great explanation, helped me understand perfectly

17

u/javaHoosier Jun 29 '20

Why not write in binary? Well that’s just ridiculous. I like my sanity.

Why not write in Assembly? A bit easier but still not practical.

Why not write in C? Much better and still fast but difficult for beginners and I prefer a different way to model abstract concepts.

Why not write in C++? Still fast and has object oriented programming but still difficult to understand for some. I’m tired of managing memory.

Why not write in Java? No more memory management and it is platform independent. But there is so much boilerplate and the syntax is messy. Why does it take so much code to do something simple?

Why not write in Python? Very simple syntax to understand and can do a lot with a small amount of code. So many libraries that can do a lot. I’ll use this for my scripts.

Maybe I over simplified it but pros and cons for every language. For each person too.

10

u/kplo Jun 29 '20

Hey, I am a noobie at Python, taking my baby steps. Why is hiring programmers expensive? Is it because there are not many qualified people for the task?

44

u/kor_the_fiend Jun 29 '20

Thank god hiring programmers is expensive. I like paying my rent.

3

u/kplo Jun 29 '20

Lol that's good to hear. I want to be a programmer myself.

1

u/e-rekt-ion Jun 29 '20

Wish it was expensive here in Australia :(

5

u/road_laya 🐧WSL2 + VS code webdev 🤹🏻‍♂️ Jun 29 '20

I work at a Python-based company, we make web-based booking and timekeeping systems. Our servers cost less than one of our ten employees to operate.

40

u/flyingfox12 Jun 28 '20

Some other things that haven't been said here:

Python has lots of modules, you don't need solve as many problems because some one else already has.

Connected to the one above, python has a huge user base, so modules you rely on will be somewhat supported.

Python has a critical mass of users across use cases. So you can jump in to using it whether you're building an IoT device, working as a devops CI/CD specialist, or doing big data analysis with machine learning.

IMO python's biggest issue is GIL, and it's that alone that has stopped it from going further than it already has. That's why Go is a very good language to also work with.

8

u/PricePadraic Jun 29 '20

Do languages like JavaScript, Go, Ruby, C#, etc etc... not have the amount/usability of modules? I know a tiny bit of JavaScript and mostly only have learned in Python thus far. I guess I just expected all languages had modules to download and use... sounds dumb now that I thought that.

6

u/kor_the_fiend Jun 29 '20

Different languages tend to excel in different areas, module wise. Python excels and is the clear leader in certain areas, such as data science/mathematics due to strong user support and community development of modules relates to those areas. At the end of the day, it is up to the users/community of a particular language to create the modules that we will all use.

3

u/flyingfox12 Jun 29 '20

they do, python does that very well, with lots of useful modules being constantly enhanced by big players but available free.

8

u/scrdest Jun 28 '20

GIL is not even a Python problem, it's a CPython interpreter problem. It wouldn't even be a problem if all the attempts rewrites thus far didn't crash and burn. The overhead for the dynamic stuff is unfortunately real no matter the interpreter, though.

4

u/Random_182f2565 Jun 29 '20

Pandas, matplotlib, openpyxl, Django, pyautogui, sklearn, etc Python simply never ends

2

u/AsleepThought Jun 29 '20

IMO python's biggest issue is GIL

gonna have to disagree with this one, I find that the biggest issues with Python are by far the library ecosystem, and the dynamic typing.

The fact that there are so many modules out there becomes more of a curse than a blessing, when your dev's start including random arbitrary modules to do simple things that they are too lazy to write a little boilerplate for, now you start accumulating a laundry list of dependencies, next thing you know you've got dependency hell, you need virtual environments, which lead to containers, now your 500KB Python program needs a 800MB container in order to run, not to mention the man hours needed version-locking all the libraries in the container, setting up and maintaining your own container registry, upgrading one dependency breaks another, etc..

And the simplicity of the code turns your projects into messes as they grow larger. Dev's start writing chicken scratch functions where you cannot tell what data type or data structure any of the input or output parameters are, testing is an afterthought, etc..

Python gives inexperienced dev's plenty of rope to hang themselves with, and bring you with them. Not that other languages are immune either, but this is far, far more severe than GIL and other languages handle this much better.

1

u/flyingfox12 Jun 30 '20

I agree with everything you've said. Really insightful.

My response was more pointed at "why we use python" and not why we should. So when I wrote about the biggest issue in python being GIL I meant for getting started. If you have a project you know will need to scale GIL will stop you from starting with python.

1

u/painya Jun 29 '20

I believe that they’re working on a solution to GIL too! Sub-interpreters are what they’re calling it.

77

u/tonpaternel Jun 28 '20

I like the analogy of “just because a plane is faster than a car doesn’t mean you should prefer it to go to work every morning” It’s all about convenience

7

u/PricePadraic Jun 29 '20

Haha, just imagined myself riding a plane to the grocery. Well put.

12

u/Retinator99 Jun 28 '20

That's a good one

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.

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.

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

13

u/[deleted] Jun 28 '20 edited Jun 28 '20

With a good profiling, time-critical loops in Python need to be recoded in C. Best of both worlds.

7

u/[deleted] Jun 28 '20

Yes and interfacing with C when you need it is fairly easy in python.

4

u/[deleted] Jun 29 '20

How do you do that?

8

u/[deleted] Jun 29 '20

-3

u/kor_the_fiend Jun 29 '20

Yuck. This is a draw back for me, a good language should support optimization natively.

3

u/[deleted] Jun 29 '20 edited Jun 29 '20

This is really beyond just optimization. Python can be optimized fairly well by the way you code.

Python interpreter is actually written in C, and the standard library has performance optimizations written in C, so that’s why this exists. Python just makes it easy. It’s a high level language; not for everyone. Not everyone needs the fastest car out there, but if you need speed and still want to use python you can.

9

u/[deleted] Jun 28 '20

Who knows if some company creates a Python chip.

Back in the 80's when Lisp was the de facto language for AI, Lisp machines where created in order to enhance the speed with a Lisp-in-silicon approach.

6

u/4chanbetterkek Jun 29 '20

Semicolons are for nerds

2

u/[deleted] Jun 29 '20

Would you save time if you program in Python and then translate that to C? (automatically with manual adjustments)

1

u/PricePadraic Jun 29 '20

Curious about this answer also. I see a lot of references to using Cython when necessary. Maybe I need to learn more about Cython to ask a better question; but I wonder if this is the ultimate use of Python why I don’t hear/see more about it. Pardon my ignorance

1

u/[deleted] Jun 29 '20

I think it’s because it’s just used when needed. A lot of the time python doesn’t need a lot of pure performance to get work done.

In places where it does, people use it. It’s a tool like anything else. I’ve rarely needed python to be fast enough to refactor and build a lot of stuff in C, but there are good implementations and you may use them without even knowing it.

Here’s an example:

https://pypi.org/project/ssh2-python/

2

u/Losupa Jun 29 '20

I dont know about automatically translating it to C, but yes you will most likely save time if you write it in python while prototyping and are constantly adding new features and rewriting it, then translate it to C if you find yourself needing more speed eventually. ofc it may be better to just write it in C, or at least certain core functionalities that are time sensitive, so you wouldnt have to inevitably rewrite them.

2

u/ErinMyLungs Jun 29 '20

I haven't done this but you can optimize the slow parts of your code into c and leave the rest as python which I think gets the best of both worlds. Fast prototyping and easy to read and write but you only optimize what's actually slow.

Frequently you'll have one main bottleneck that optimizing will give you immense improvements and the rest is generally inconsequential. Or worse you have a bottleneck that can't be worked around (uploading a large file is limited by internet speed no matter how ruthlessly efficient your code is) so once you're under that it's pretty much stuck.

Tensorflow is a good example of using c and python to do high performance work with easier coding via python.

2

u/[deleted] Jun 29 '20

Some libraries already do that namely Pandas and Numpy (and OpenCV to C++ IIRC) and probably many more.

6

u/[deleted] Jun 29 '20

A lot of what happens in programming world is driven by fashion. But, this isn't the only explanation. There's also a network effect, that keeps amplifying preferences of a group of people, once it reaches certain threshold.

This is not unique to Python, this is, essentially, the story of every technology popular today.

To try to trace Python's history back to its origins, at least as far as I can remember it, it would be more or less like this:

  1. Linux, while, officially, GNU/Linux was slow to come up with a decent programming language for system programming (Guile an implementation of Scheme, which is still not 100% there). Even though GNU stands for "not UNIX", it is a UNIX lookalike, and so, it inherited UNIX' Shell. Vanilla UNIX shell isn't a great language... it doesn't have integers or arrays (only ways to treat strings as if they were such) just to give you an example of inconveniences. Not to mention modularity, error handling, and general correction verification as its major problems. Python was in Linux since its very early days, and so got pretty well integrated into the system.

  2. Python had also an easy way to write bindings around native code, so that a lot of libraries would ship with Python bindings already written and available. This made it easy to use for system programming on Linux. Unsurprisingly, a lot of system utilities on Linux are written in Python as the result.

  3. Since Python was there it was tempting to use it for many things. The web industry was, and still is booming. Eventually CGI were replaced by PHP. At that point web entered the era of shared hosting, with LAMP being the default setup for it. However, there was a lot of discontent around PHP as it was awfully primitive and poorly implemented language. Thus, when the demands for sophistication of the back-ends grew, people started to look for alternatives. This was the time many languages jumped on this bandwagon with their webframeworks. Ruby with RoR, Java with Spring, and Python with Django. Python was never the hottest language in web development, but it earned some popularity nevertheless.

  4. Someone discovered that they can use video adapters for low-precision massively parallel computations. But this functionality is too difficult to make available to mainstream languages due to very outdated model of computer and computation they are based on. The much easier solution is to write a library that does this massively parallel stuff, while exposing something that looks like PDP-11 on the interface level. Since Python was so easy to wrap around native code, NumPy was created. And machine learning took off (not because of NumPy, but it carried Python to the new levels of popularity).

  5. There was also another vector that contributed to Python's popularity: it was used in testing / automation because it was used in system programming and already had ways to interface with the system under test.

Put all of the above together, and you get a very popular language, that's in the interest of a lot of people to maintain because their programs depend on it.


Unfortunately, this has nothing to do with quality. Python was never a quality language, never tried to beat any other language on any characteristic... never tried to be the best at anything. Kind of an underachiever language. It just had this fortune to get inserted into many key positions, which ensured its long-term survival and proliferation... the same happened to C, or JavaScript or... chances are that if you've heard about this language, it's more or less the same story.


So, we use Python because it is popular. Popularity isn't just some dumb high-school prom thing. Popularity in this context means money. It is easier / cheaper to hire programmers who program in popular language. It is easier / cheaper to write large programs because many components have been already written and are freely distributed by other people. It is easy to coach new trainees, it is easy to find development tools that support the popular language... The financial advantage is so huge that people don't even attempt to use other, better languages, not even if they know they are better.

So, we use Python, because there's a monopoly on the minds and preferences, that becomes stronger and bigger the stronger and bigger it becomes. :)

3

u/abueloDelClan Jun 28 '20

In my case, after years with java, I'm think Python has cheaper for a company because reduce the time of production.

3

u/singlebit Jun 29 '20

The syntax is beautiful.

2

u/colorist_io Jun 29 '20

The best way to understand it is to search for some example programs online and see the differences in the code first hand.

Long story short, Python is easier write and much easier to read compared to C++. If all you care is speed, then definitely choose C++ over Python.

2

u/[deleted] Jun 29 '20

I can prototype very quickly in python and I can refactor my prototype into a v1 even quicker in python. its fast because its simple, theres tons of great sample code and canned libraries to leverage. its just a really fast way to develop and end up with a solid app or script.

2

u/[deleted] Jun 29 '20

I feel your question can be rephrased as "why use a high level language when we can use a low level language".

2

u/bpt7594 Jun 29 '20

In my field (finance). Unless you are pricing a complex product using Monte Carlo or you're doing HFT high frequency trading, python is actually the norm because not all finance students have backgrounds in coding so the closeness to human languages of python really help.

1

u/Lewistrick Jun 29 '20

I once heard that data-related libraries (such as numpy) are so optimized that they can outperform C/C++ in the some operations. Never tested or fact-checked this, but with the development speed on the one hand and the immensely efficient C++ optimizations, Python can be a winner on both sides.

1

u/Dogeek Expert - 3.9.1 Jun 29 '20

A program written in C will take you a week to build, and a second to run.

The same program in Python will take a day to build, and an hour to run.

The saying goes 'time is money', and it's exactly why python is more popular than faster languages. Companies would rather pay someone for an hour of work rather than a week. There are a few exceptions though :

  • The program is meant to run billions of time. You want to squeeze out any performance then.

  • The existing code needs a specific language (lots of legacy code use old technologies, older versions of Java, Perl scripts, PHP and COBOL). You kinda have to conform to the existing codebase then, unless you want to rewrite everything.

It's a cost analysis problem. What's cheaper : rewrite the codebase in a new language ? Spend months of dev time for improved performance ? Spend a few days for a PoC in python or JS at the cost of a less performant app ?

Especially now, the balance is skewed towards higher level languages, since processing power is so cheap (at least compared to a programmer). I can rent a dedicated server for less than 500$ a year. My yearly salary is 42k (and it's on the low side). It's just cheaper to spin a few extra servers to spread the load of a less performant app.

1

u/State_ Jun 29 '20

It really depends on what you are doing.

It's like a tool. If you need something that requires the speed of C or hardware programming, then use C. If you want to leverage the ease of python than use python.

1

u/i_like_trains_a_lot1 writes Python for food and shelter Jun 29 '20

- big and active community

- huge ecosystem with a lot of libraries for almost any task

- easy to write code and get things done => increased developer productivity.

- in 90% of cases, the speed of the Python interpreter is not going to be an issue.

1

u/[deleted] Jun 29 '20
  1. Facebook is written in PHP, but it uses a c++ parser/runtime for critical parts of its application.

  2. Instagram is built using Django, but it also uses c++ runtime to speed up critical parts. Also, I read somewhere that the Instagram server takes up to 40 minutes to restart because of python.

These facts don't make one PL better than others. They are only better in their domain.

  1. You could build literally anything using python

  2. Easy to understand (readability)

  3. Massive open source applications and community support

  4. Currently first in line for Data Science / Machine Learning

  5. Swift is hoping to replace python, It is simple as python but runs faster.

  6. Even though it's insanely fast, Go isn't suited for data science, because it wasn't built that way.

  7. And for those who love c++ very much, Go/Rust will replace it (not very soon). These languages were built to tackle memory problems. Microsoft already has a Rust team working on rewriting some parts of the windows. MS is also building a language inspired by Rust (Project Verona).

1

u/bbt133t Jun 29 '20

If Python was not easy to read, it would have become Pascal. 😂

Fact!

1

u/mnkisala Jun 29 '20

Python might be slower if you're trying to make a regular desktop app, but it's really well optimized for programs that require some heavy multitasking (that's why neural nets and server side software are often developed with python). The other thing about python is that it's more human readable than say C/C++, making things like debugging way easier.

1

u/YeeOfficer Jun 29 '20

I like python because it allows me to prototype light things that I want to automate in a short time. If I wanted, say, a prime number finder, I would write it in C, but if I wanted to prototype it, python. If I want an excell sheet automating, python.

1

u/[deleted] Jun 29 '20

Python (or R, in my line of work), is much easier to use than C/C++/Fortran/whatever. If I need to build a machine learning model, I can do it in an hour/several hours, depending on how complex it needs to be, using one of those higher level languages. Using a lower level language, it'd take me days, if it'd even be possible, to shave a few seconds off the run time. For apps or whatever where performance is key, then use a lower level language, but there really is no need for them in what I primarily use Python for, which is data analysis. And besides, most of the in-depth R or Python libraries for data analysis are already written in C / C++ - this takes advantage of their speed, but wraps them in an easy-to use language.

1

u/ahmadjordan Jun 29 '20

I have been programming in C++ and then i am currently using Python especially machine learning packages Pytorch and TF. I can tell you, Python is very user-friendly and it makes much more sense than C/C++. Even in embedded systems, Python will sooner or later replace Cs. It’s a language created to prosper while C++ starting from the namespaces to the pointers and the complicated syntax will become less popular sooner or later.

1

u/xelf Elf Jun 29 '20

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

Because basically you can't. Outside of Perl no other language has python's rapid development time. Yes this comes at a cost in that you won't want to use python for certain highly speed optimized applications, but the truth of the matter is python holds it's own in the speed category to the point that well written Python will out perform mediocre code in a faster language.

1

u/[deleted] Jun 29 '20

I can only answer for myself.

I love working with Python. It's a lot of fun, it's very quick to write a program with, there are a lot of libraries that are easy to install to extend functionality (thanks to PIP), and you can use the REPL part of Idle to experiment and then launch into writing an application very quickly.

As to the execution speed of Python - for 99% of what I write, that speed difference isn't perceptible unless you're running a profiling tool.

1

u/TraditionalGlass Jun 29 '20

You're not going to use Python to make software, however tempting that might be. Python is used for web dev, bot development, and other things that are usually back end. It's also used as "glue" for other languages

1

u/DrBobHope Jun 29 '20

As someone who's not a programmer, but who is just starting to get into it...I believe a large part of it has to do with it's approachability, and it makes sense. Despite being new, I've been able to do quite a bit because the logic is easy to follow and type. The format is simply and straightforward. I initially was going to start with Java, but the formatting looked really ugly and overly complicated. R is a nice, but I found python more general/flexible (plus R had some weird quirks I didn't like, like assigning variables with -->). Matlab/Octave are great as well but too limiting. You can do everything they can do in Python, and more.

I'm afraid I'm too novice to really comment on C or any other languages, but this is why I chose Python. I do plan on eventually learning Java (since its so widely used, might be useful), but I have yet to see any application Java can do, that Python can't (mastery of 1 vs. jack of all?).

1

u/Dragonfruit2153 Sep 10 '24

Python is already hyped in the market that whether it is the better technology or not . alot of supporting libraries was made for it like in datascience & machine learning.

1

u/Educational-North958 Sep 21 '24

For me, it was the KISS test - can you do what you want to do, right out of the box, quickly and easily? My case was to build a single page website that displays one number from a database. This is starting with a fresh domain and hosting service. I have never been able to do that with python. I am sure there is a way, but it's not simple and it's not obvious. Apparently, you have to install a dozen different packages, frameworks, libraries, etc., etc., etc. If you don't know about any of those requirements, getting from start to finish is a long, tedious process, full of wrong turns and dead ends. Now, strap in - I'm going to upset you. The best solution to my test case was PHP. It comes already installed on the server, with most configuration settings usable out of the box. You can write PHP and HTML code in the same page, and they work together seamlessly to create dynamic output for your website. Job done. Now, tell me again about python being so fast and easy to write. It's not so fast if it takes a lifetime to just get it to work.

1

u/justingolden21 Jun 29 '20

Python still runs faster than a lot of other languages, and it's super super easy to read and write, and can be applied to a lot of different purposes, for example back end, data science, machine learning, etc.

It's awful for making programs with UIs that aren't super super simple, and awful for making games imo.

It really shines when handling loads of data, writing simple, fast code, or clean backend code.