Why Python for Web Development

The options to develop web apps these days are so many that it would take tens of thousands of words to list and describe each one of them.

Languages like Java, JavaScript, C#, and Python are amongst the most famous for the purpose of developing web apps.

In this article, I will discuss some of the benefits of using Python, specifically, for the development of web apps.

Easy to learn

Python is one of the easiest languages to learn.

If you are an experienced developer, you can learn enough Python in a week to be dangerous and do a lot.

If you are a complete newbie, Python is a great first language, with a clear syntax, and allows you to get started as quickly as one can be.

In any case, if you want a hand in starting out with Python, try my free Python Guide For Beginners to get you up to speed as fast as possible.

This image from xkcd exemplifies this better than I ever could:

Ecosystem

Libraries for everything.

Python has a library for every use case.

From web scraping and simple scripting tasks to machine learning.

The plethora of tools you can find in the Python ecosystem is so big that you can jump many levels of the heavy lifting in your app idea by just importing some super useful libraries.

There is no advantage in reinventing the wheel, the same way there is no benefit in rewriting code that has already been coded.

Especially considering how the most used libraries are battle-tested in levels a single developer could never do.

Frameworks

Python has lots of frameworks for web development.

By far, the most famous are Django and Flask, and as a recent new contender we have FastAPI.

Django

Django is an interesting choice when you don’t want to think too much about all the pieces you are going to use.

Django has “Batteries Included”, which means a very good ORM, Authentication, Admin panel, template engine, and many other features most web apps use.

And if you need a REST API, Django REST Framework is an easy-to-install plugin that makes full use of Django’s built-in structures.

Flask

Flask is minimalistic, known as a micro framework, it gives you the bare minimum to start coding.

For most things, you will need to add a plugin and integrate it into Flask.

SQLAlchemy for ORM for instance is a must if you don’t want to work with raw SQL (which I personally prefer).

On the other hand, Flask is good for those who want full control of their web app and choose the freedom to use whatever they want.

With its version 2.0, Flask now has full support for Async and WebSockets.

FastAPI

I have been using FastAPI for some time now and I have been really enjoying it.

It is very similar to Flask in the sense that is very lean and simple to get started.

At the same time, it comes with full support for Async right from the start and a variety of tools to develop APIs easily, like the auto-generation of documentation with Swagger.

And if you want to develop a standard web app, you can just make use of Jinja 2, the same way Flask does.

Developer Time > Execution Time

When developing a new project, the time it takes to develop a new feature is the single most expensive item.

This is because the longer it takes to develop something, the more Developer Time it will take.

Being able to prototype something really fast is a huge advantage these days and Python and its ecosystem and frameworks are great tools to achieve things quickly.

This is the counterargument for people who say "Python is slow".

Slow for what?

Many languages are faster in execution time, but, as I said in the title "Developer Time > Execution Time".

Another thing to notice is that I/O operations are by far the slowest thing in an app, so good caching strategies ( by using Redis for example) and a better database design will give you better ROI than switching languages in many situations.

And finally, think in terms of Pareto’s 80/20, roughly 80% of consequences come from 20% of the causes

I remember in college when studying in this class of Computer Architecture and there was this chapter about optimization.

In one of the examples, there was a program written in C++, which is an extremely fast language for most purposes.

There was a small part of this program responsible for a huge part of the performance problems, simply because it was heavily used, more than other parts of the code.

What did they do?

They rewrote that single part in pure Assembly.

By Pareto’s logic, this small implementation was responsible for a good boost in performance.

For Python, you can follow the same logic.

In most cases, pure Python is more than enough and gives you the extra speed in development time.

When performance is a must, there are tons of Python libraries that are simply wrappers around C++ or C that are very performatic, which is the case for Pandas and Tensorflow.

And, if you have a specific use case, you can always implement the solution yourself in any other language and simply call it from Python.

There are many ways to do that, either by direct calls or by using another intermediate system like a message broker (Kafka for instance) to make the communication between systems even more transparent.