The Versatility of Python - What Makes It Stand Out?

Maciej Zakrzewski - Senior Python Developer
3 minutes read

There is really no limit to which programming language or framework can be used on the backend. Unlike with frontend, which is executed inside a browser, there are no expectations for what the code on the server-side is. Backend is developed as any other computer program – with the most suitable tools for the task. Even though there is no limit in options for selecting them, as in any industry, most effective and practical solutions tend to surface and eventually become commonplace.


Python is one of those solutions. It was created in 1991 by Guido van Rossum as a general-purpose language. With time, it made its way into applications and evolved along with the industry, technology and trends.

Why Not Just Go With PHP?

The first question that one may ask is: can it be done in PHP or Java? And the answer is yes – any programming problem can be solved in PHP, Java, C/C++, assembler or any other language. The simplest example could be the ‘hello world’ program, written in every language under the sun. But this can be extrapolated to more complex programs that connect with databases, process data, render HTTP responses, and so on.

The real question here is choosing the one which provides the best tools for a particular task. The fact that various languages are widely used suggests that all of them have different areas they excel at, and that they are chosen based on their utility in a given context. When it comes to tools with overlapping functionality, there is a natural tendency to gravitate towards something known – it is often rational, but it also poses a risk of choosing a suboptimal approach to solving a particular problem.

Mathematics

Some web applications require complex math computations on large data sets. Python has one of the most developed libraries for that task. Libraries like numpy and scipy can basically cover all mathematical concepts you would find even in advanced math handbooks. It supports a wide variety of computations: matrix, vectors, integrals, statistical functions, and many more.

Web Scraping

Even though it is preferable to have access to structured data, sometimes just downloading a website and extracting data from it must suffice.

In Python this can be tackled at many different levels. Some possibilities are:

  • with libraries like requests and beautifulsoup to fetch and parse XML/HTML,
  • use a dedicated library, like Scrapy, to fetch and process the website for required data,
  • using headless website rendering (pypetteer) to render the website and then parse it.

Some of those are more complex than others – which one is the most suitable depends on the task at hand.

REST API

Although not unique to Python, there is a variety of choices when it comes to tools for implementing REST API. It can be done either with a typical web framework (like Django + django-rest-framework) or with a collection of separate components (for example using aiohttp and SQLAlchemy for database operations).

For example, with Django and django-rest-framework we can produce API endpoint and serialize data models very quickly:

from rest_framework import serializers
from car_app.models import CarManufacturer

class CarManufacturerSerializer(serializers.ModelSerializer):
   class Meta:
       model = CarManufacturer
       fields = '__all__'

And that is it – we have a class which will turn model data into JSON, XML, or another structured format.

Considering the general utility of Python as a backend scripting language, it is often a natural choice for a language used to implement web API.

Syntax

One of the notable characteristics of Python is its syntax – simple, concise and readable. Some of its features (like generators, iterators or decorators), when implemented correctly, allow for code that is easy to understand and modify.

Let's take a simple function which finds even numbers in an array as an example:

def find_even_numbers(input_numbers: list) -> list:
   even_numbers = []

   for n in input_numbers:
       if n % 2 == 0:
           even_numbers.append(n)

   return even_numbers

It is an equivalent of the following code in PHP:

function findEvenNumbers(array $numbers): array
{
   $evenNumbers = [];

   foreach ($numbers as $n) {
       if ($n % 2 == 0) {
           $evenNumbers[] = $n;
       }
   }

   return $evenNumbers;
}

Even though those examples are very similar in length, and both are formatted properly, the code in Python achieves doing the same task and, in the initial example, can be done with just one line using list comprehensions:

[n for n in numbers if n % 2 == 0]

It is also worth noting that such code:

function findEvenNumbers(array $numbers): array { $evenNumbers=[] ;
foreach ($numbers as $n) { if ($n % 2 == 0) {
  $evenNumbers[] = $n;
        } } return $evenNumbers;
}

is completely valid in PHP; this will be the case for most programming languages.

Whereas a similarly misformatted code in Python:

def find_even_numbers(input_numbers: list) -> list: even_numbers = []
    for n in input_numbers:
 if n % 2 == 0:
even_numbers.append(n)
   return even_numbers

will produce a syntax error because a consistent code indentation is a part of its syntax.

Reliance on indentations for code blocks also helps with writing cleaner, better-structured code. Of course, it cannot fully enforce the best practices, but it helps eliminate the most basic and obvious mistakes that could otherwise go unnoticed.

Natural Language Processing

Python may be considered a default language when it comes to NLP. Not only does its language syntax lend itself well to processing text, but it also has a great choice of tools and libraries, like NLTK for example, that help with this complex task.

For large amounts of data, those types of tasks often need to be done in the background. This also is Python’s strong suit, with Celery providing a solid solution to run queued or scheduled jobs.

General Purpose

One of Python’s general characteristics present since its inception was being a general-purpose language. What it really means is that being an effective tool for web development is just a side effect of its versatility.

Of course, there is a multitude of web frameworks to choose from, but it can also be used for any other type of application you may want to run on the backend.

Since it is highly portable between platforms, it can also serve the purpose of creating standalone applications distributed to end-users. Python is widely used as a supplemental language – its interpreter can be embedded within otherwise natively running applications. Or conversely, a native code can be used from a Python application or package – Numpy, for example, has its core written in C and then used from within the Python code. Other examples utilizing this are Ansible, a popular deployment tool with a big portion of the code written in Python, or Blender – a 3D modelling software which uses an embedded Python interpreter for scripting or extensions.

Compared to some other programming languages, practical applications for Python are very wide. No matter if it is a simple utility script or a large-scale web application, it allows us to solve complex problems efficiently; regardless of size, programming in it always comes naturally. It never bogs down the development with overcomplicated syntax, low performance, compilation time or portability issues. With a great suite of packages, it aids the development process from initial prototyping to final release and beyond.

Of course, it is sometimes better to use a more focused, purpose-made solution. But in fact, Python is both versatile and focused – on making an idea into reality.

On-demand webinar: Moving Forward From Legacy Systems

We’ll walk you through how to think about an upgrade, refactor, or migration project to your codebase. By the end of this webinar, you’ll have a step-by-step plan to move away from the legacy system.

moving forward from legacy systems - webinar

Latest blog posts

See more

Ready to talk about your project?

1.

Tell us more

Fill out a quick form describing your needs. You can always add details later on and we’ll reply within a day!

2.

Strategic Planning

We go through recommended tools, technologies and frameworks that best fit the challenges you face.

3.

Workshop Kickoff

Once we arrange the formalities, you can meet your Polcode team members and we’ll begin developing your next project.