Why I Write Python

scottwrobinson | 25 November, 2015 06:19

One of the easiest languages to learn out there is Python thanks to its ultra simple syntax, huge standard library, and helpful community. Beginning programming isn't easy, so finding the right language to start out with is key. This is exactly why I recommend most people start out with Python, which is mostly due to many of the points stated here.

Lets take a look at a few examples and see what this language is capable of doing.

import random
def fisher_yates(items): for i in range(len(items)-1, 0, -1): j = random.randrange(i + 1) items[i], items[j] = items[j], items[i]

Here we have what's called the Fisher-Yates shuffle. This algorithm takes in a list of items and shuffles them in to a random order. As you can see, there is very little code required here to implement the algorithm. Much of this is thanks to Pythons concise syntax, in addition to the simplicity of the algorithm.

Let's take a look at some of the more interesting syntax in the code above:

for i in range(len(items)-1, 0, -1)

This line of code allows us to iterate over the items, starting from the end. In my opinion, the "for i in range()" syntax is much easier to read than the typical "for (int i = 0; i < len(items); i+)" syntax.

Additionally, since Python requires you to indent different scopes of code, it becomes inherently easier to read. While many languages out there are typically indented, Python requires it, which then allows them to get rid of the curly braces ("{ }"), making the code much less cluttered and more readable.

The last line of the algorithm above also saves us from having to write a few additional lines of code and from using more variables. In most languages, we'd have to use a temporary variable to swap the values. But since Python allows us to use this "dual assign" method, the code is shorter and uses less variables.

These are just a few of the reasons why I really enjoy using Python. I'll continue to write about some of its more simple features, and later I'll start bringing up some of the more confusing ones, like "magic methods". If you have any suggestions, I'd love to hear them!

Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LT - Design by BalearWeb