Introduction

Python is probably the easiest language when it comes to programming. But it can be even easier if one does it the pythonic way. Although the pythonic way can be subjective to each programmer, there does exist an official style guide one can refer to if he wishes. In this particular post, we will be discussing the pythonic way and differentiating them from the newcomer way of coding. This post will be specific for aspiring data scientists although everything discussed will be as useful for general-purpose programming. Again, one can check the official guide for a broader and in-depth understanding.

What is pythonic?

Pythonic means writing the code just as the community does it. There are no solidly defined standards for calling something pythonic. The general sense of a piece of code being called pythonic is when it is shorter, simple to write and read. It will make it easy to share one’s code with other python programmers. It will also make our code work more efficiently. Sometimes operations that require many lines of code can be written in a single line. So it will also make our writing process easier. One thing about writing in pythonic code is that it makes reading the program easy. One might literally read them as a sentence that makes sense instead of trying to logically connect the whole code in their minds. We’ll see that soon.

Pythonic programming for data science?

There is nothing called pythonic programming for data science. Everything we discuss in this post applies to all general-purpose programming using python. The post covers specific parts of pythonic programming where we deal with data science more frequently. There are a lot more small and specific aspects of pythonic programming that will be beyond the scope of this article.

if statements

These are the decision-makers in a python program. These are also the easiest and most basic things one will learn in python. Here is the newcomer way of writing an if statement.

x = 10
if x > 9:
    print(x)  # prints 1

We can write the same if-statement in a single line which is arguably pythonic but only if one can easily read and understand it.

if x > 9: print(x)  # prints 10

If one wishes to use elif and else, he may write them in separate lines which are good for readability but no one is stopping them from writing them in a single line. The below example checks if a number is even or odd.

x = 10

checker = 'even' if x % 2 == 0 else 'odd'

# even

In this case, the use of if and else can be called pythonic. However, the whole purposes of being pythonic is to easily understand the code. While one might get used to this syntax, it is not the best syntax as far as readability is concerned especially when expressions become more complex. Pythonic here is completely subjective.

while loop

A while loop iterates until the condition becomes false. We can also write a while loop as a single statement. We will compare both the non-pythonic way and the pythonic way to do it.

flag = 1
while flag:
    print("while loop")  # non-pythonic

while flag: print("While loop")  # pythonic

However, if our while loop contains more than one statement, we will have to write it normally.

Declaring list, tuple & dictionary the pythonic way

# Declaring a list

list_numbers = []  # pythonic
list_numbers = list()  # creating an instance of list class
list_numbers = [1, 2, 3]  # list literal

# Declaring a tuple

tuple_numbers = ()  # pythonic
tuple_numbers = tuple()  # creating an instance of tuple class
tuple_numbers = (1, 2, 3)  # tuple literal

# Declaring a dictionary

our_dict = {}  # pythonic
our_dict = dict()  # creating an instance of dict class
our_dict = {"a": 1, "b": 2, "c": 3}  # dictionary literal

As one can see, we can easily declare a list, tuple, or dictionary by assigning the name to their respective braces. However, we cannot declare a set in this method. That’s because sets use curly braces {} which are already being used by the dictionary. The best way to declare a set is to simply create an instance of the set class.

# Declaring a set

our_set = set()  # Creating an instance of set class
our_set = {1, 5, 3, 4}  # set liter literal

for loop

A for loop is used to iterate for a predefined number of times. We will see both the non-pythonic and pythonic way to utilize for loops. This particularly becomes useful in our upcoming topic, which is list comprehensions. In the below example, we are creating a list of numbers from 0 to 9.

# creating a list of numbers from 0 to 9

numbers = []
for i in range(10):
    numbers.append(i)

# numbers become [0, 1, 2, 3, 4, 5, ]

One must have noticed how we wrote 3 separate lines of code. Now let’s take a look at the pythonic way to do the same.

new_numbers = [i for i in range(10)]

# new_numbers become [0, 1, 2, 3, 4, 5, 6, 7, 8, 9

We finished the entire program in just one line. All we did was declare the list in the pythonic way and contain the entire for loop inside the brackets. Here the for loop will not iterate unless the list is called. When we write a sufficiently big python program, one must have noticed how the program is slow to process. This is especially true when we have for loops with in operations which is hard to avoid. Writing for loops in the pythonic way means we will only run them if required speeding up our program.

List comprehensions

Perhaps this is the most useful pythonic method. List comprehensions are used when we want to transform our list into another list. It also allows us to cherry-pick the elements of our new list from an existing list. We can also modify the elements of the existing list before passing them to the new list. This is done by utilizing the pythonic method of for loop. The above example is already a list comprehension. The range(10) in the example represents a list from 0 to 9. Hence we were just creating a list from an existing list without any cherry-picking. Now let’s see how list comprehension can be used more favourably.

# creating a list of even numbers

even_numbers = [i for i in range(10) if i % 2 == 0]  # [0, 2, 4, 6, 8]

# creating a list of square numbers

squares = [i * i for i in range(10)]  # [0, 1, 4, 9, 16, 25, 36, 49, 64,

In the first case, we managed to create a new list from an existing list by cherry-picking the elements from the existing list. In the second case, we managed to modify the elements from an existing list before passing them to create a new list. In both cases, our job was done in a single line. Our for loop will also not run if we are not using the lists themselves hence making our program efficient.

We can also create dictionaries from lists using list comprehension.

square_dict = {i: i * i for i in range(10)}

# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

If one drops the i: inside the curly braces {}, it will become a set instead.

square_set = {i * i for i in range(10)}

# {0, 1, 64, 4, 36, 9, 16, 49, 81, 2

If the value of the existing list is not needed, then we can use _ instead of a variable. In this case, we will create a list that will have an equal length to the existing list but we provide the values inside the list.

zeros = [0 for _ in range(10)]

# [0, 0, 0, 0, 0, 0, 0, 0, 0,

Using _ is very useful when have to conduct operations that relate to the length of a list.

We can use for multiple times in a list comprehension to create a list of lists.

pairs = [(x, y) for x in range(10) for y in range(10)]

# [(0, 0), (0, 1), (0, 2) ... (9, 7), (9, 8), (9, 9)]

triples = [(x, y, z) for x in range(10) for y in range(10) for z in range(10)]

# [(0, 0, 0), (0, 0, 1), (0, 0, 2) ... (9, 9, 7), (9, 9, 8), (9, 9, 9

One can write each for loop in different lines for visual reasons.

Conclusion

We briefly went through the most important cases of pythonic programming useful for a typical data science project. We started from the simple decision-maker (if-statements) and learned to write them in a single line. Then we moved to learn the pythonic way of while and for loops. We also learned to declare lists, tuples & sets in the pythonic way. Most importantly, we learned list comprehensions which is a lifesaver in the data science universe.

Written by Aravind Sanjeev, an India-based blogger and web developer. Read all his posts. You can also find him on twitter.