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.

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?

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.