5 reasons why I love Python

These are a few of my favorite things about Python.
396 readers like this.
Raspberry Pi and Python

Raspberry Pi Foundation. CC BY-SA 4.0.

I have been using Python since it was a little-known language in 1998. It was a time when Perl was quite popular in the open source world, but I believed in Python from the moment I found it. My parents like to remind me that I used to say things like, "Python is going to be a big deal" and "I'll be able to find a job using it one day." It took a while, but my predictions came true.

There is so much to love about the language. Here are my top 5 reasons why I continue to love Python so much (in reverse order, to build anticipation).

5. Python reads like executable pseudocode

Pseudocode is the concept of writing out programming logic without it following the exact syntax and grammar of a specific language. I have stopped writing much pseudocode since becoming a Python programmer because its actual design meets my needs.

Python can be easy to read even if you don't know the language well and that is very much by design. It is reasonably famous for whitespace requirements for code to be able to run. Whitespace is necessary for any language–it allows us to see each of the words in this sentence as distinct. Most languages have suggestions or  "best practices" around whitespace usage, but Python takes a bold step by requiring standardization. For me, that makes it incredibly straightforward to read through code and see exactly what it's doing.

For example, here is an implementation of the classic bubble sort algorithm.

def bubble_sort(things):

    needs_pass = True

    while needs_pass:

        needs_pass = False

        for idx in range(1, len(things)):

            if things[idx - 1] > things[idx]:

                things[idx - 1], things[idx] = things[idx], things[idx - 1]

                needs_pass = True

Now let's compare that with this implementation in Java.

public static int[] bubblesort(int[] numbers) {
    boolean swapped = true;
    for(int i = numbers.length - 1; i > 0 && swapped; i--) {
        swapped = false;
        for (int j = 0; j < i; j++) {
            if (numbers[j] > numbers[j+1]) {
                int temp = numbers[j];
                numbers[j] = numbers[j+1];
                numbers[j+1] = temp;
                swapped = true;
            }
        }
    }
    return numbers;
}

I appreciate that Python requires indentation to indicate nesting of blocks. While our Java example also uses indentation quite nicely, it is not required. The curly brackets are what determine the beginning and end of the block, not the spacing. Since Python uses whitespace as syntax, there is no need for beginning { and end } notation throughout the other code. 

Python also avoids the need for semicolons, which is a syntactic sugar needed to make other languages human-readable. Python is much easier to read on my eyes and it feels so close to pseudocode it sometimes surprises me what is runnable!

4. Python has powerful primitives

In programming language design, a primitive is the simplest available element. The fact that Python is easy to read does not mean it is not a powerful language, and that stems from its use of primitives. My favorite example of what makes Python both easy to use and advanced is its concept of generators

Imagine you have a simple binary tree structure with value, left, and right. You want to easily iterate over it in order. You usually are looking for "small" elements, in order to exit as soon as the right value is found. That sounds simple so far. However, there are many kinds of algorithms to make a decision on the element.

Other languages would have you write a visitor, where you invert control by putting your "is this the right element?" in a function and call it via function pointers. You can do this in Python. But you don't have to.

def in_order(tree):

    if tree is None:

        return

    yield from in_order(tree.left)

    yield tree.value

    yield from in_order(tree.right)

This generator function will return an iterator that, if used in a for loop, will only execute as much as needed but no more. That's powerful.

3. The Python standard library

Python has a great standard library with many hidden gems I did not know about until I took the time to walk through the list of all available functions, constants, types, and much more. One of my personal favorites is the itertools module, which is listed under the functional programming modules (yes, Python supports functional programming!).

It is great for playing jokes on your tech interviewer, for example with this nifty little solution to the classic FizzBuzz interview question:

fizz = itertools.cycle(itertools.chain(['Fizz'], itertools.repeat('', 2)))

buzz = itertools.cycle(itertools.chain(['Buzz'], itertools.repeat('', 4)))

fizz_buzz = map(operator.add, fizz, buzz)

numbers = itertools.islice(itertools.count(), 100)

combo = zip(fizz_buzz, numbers)

for fzbz, n in combo:

    print(fzbz or n)

A quick web search will show that this is not the most straight-forward way to solve for FizzBuzz, but it sure is fun!

Beyond jokes, the itertools module, as well as the heapq and functools modules are a trove of treasures that come by default in your Python implementation.

2. The Python ecosystem is massive

For everything that is not in the standard library, there is an enormous ecosystem to support the new Pythonista, from exciting packages to text editor plugins specifically for the language. With around 200,000 projects hosted on PyPi (at the time of writing) and growing, there is something for everyone: data science, async frameworks, web frameworks, or just tools to make remote automation easier.

1. The Python community is special

The Python community is amazing. It was one of the first to adopt a code of conduct, first for the Python Software Foundation and then for PyCon. There is a real commitment to diversity and inclusion: blog posts and conference talks on this theme are frequent, thoughtful, and well-read by Python community members.

While the community is global, there is a lot of great activity in the local community as well. Local Python meet-ups are a great place to meet wonderful people who are smart, experienced, and eager to help. A lot of meet-ups will explicitly have time set aside for experienced people to help newcomers who want to learn a new concept or to get past an issue with their code. My local community took the time to support me as I began my Python journey, and I am privileged to continue to give back to new developers.

Whether you can attend a local community meet-up or you spend time with the online Python community across IRC, Slack, and Twitter, I am sure you will meet lovely people who want to help you succeed as a developer. 

Wrapping it up

There is so much to love about Python, and now you know my favorite part is definitely the people.

I have found kind, thoughtful Pythonistas in the community throughout the world, and the amount of community investment provide to those in need is incredibly encouraging. In addition to those I've met, the simple, clean, and powerful Python language gives any developer more than enough to master on their journey toward a career in software development or as a hobbyist enjoying playing around with a fun language. If you are interested in learning your first or a new language, consider Python and let me know how I can help. 

What to read next

Why I use Java

There are probably better languages than Java, depending on work requirements. But I haven't seen anything yet to pull me away.

Tags
Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
Moshe has been involved in the Linux community since 1998, helping in Linux "installation parties". He has been programming Python since 1999, and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility and other such things.

3 Comments

Community as #1. Mirrors my own thoughts ?

I have some experience with java but i was considering move to python, your article is really interesting and encouraging to start my journey with python

Thank you

I like and dislike Python.

What I actually like about python is that it is, at least at present, somewhat less fragmented than Perl. Perl has two weakness. Firstly, its objects are bolted-on and not very clean. Python does not have that issue with objects. But the other issue is with third party modules that were not written in Perl (i.e., in C) or which were not maintained over time as Perl changed. Python will eventually experience the same kind of library rot, though perhaps a bit less since its base library is more extensive.

Why I hate Python... is mostly because its authors and community have a "thing" about not allowing programmers who actually like braces for block notation to use them and treating them with disrespect. It isn't like allowing braces would break anything. And as someone who studied and has written small compilers, I really chafe at the whitespace ambiguity - and have experienced issues with that kind of thing with files that use different whitespace/tabbing conventions than I prefer to use. I have no problem with the existing notation (: and whitespace) being the default, I just don't like having it forced down my throat when a braced alternative would be so easy. (And, yeah, I have seen the silly #} jokes.)

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.