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

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.

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.

→ More replies (1)

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.

→ More replies (1)

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.

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.

→ More replies (1)

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++.

→ More replies (2)