Becoming a Pythonista

If you feel somehow put off by that word pythonista, know that you are not alone. I personally find the word a bit funny, and I kind of had to look it up, I mean … python programmer / software engineer would have also done the trick, wouldn’t it? Anyways, this post is not about how to call people working with python, but to share my experience learning Python as a Java software engineer.

Before anything else, just right of the bat, it is not my intention to start / fuel a language war, Java vs. Python or the like, my only intention is to convey my experience and my take on what it means to learn a new programming language coming from Java, in this case the new programming language being Python.

So, why am I (re)learning Python? The short answer is that my current company I am working for is switching gears and decided to use more Python to develop its products. This means, I get to use Python in a more professional capacity than I got until now. This is exciting! The slightly longer answer is that Python is preponderantly used for Machine Learning and Data Science, both areas which got my attention. Additionally, being a general purpose language, you can use it for pretty much everything, it’s an employable language to have under your belt.

Actually, this is not the first time I am learning Python, I played with it already some years ago, specifically, beginning of 2020, and from there on I’ve been dabbling in it every now and then. Eventually, I did a Coursera Specialization Python for Everybody and two other courses on edX, Introduction to Computer Science and Programming Using Python and Introduction to Computational Thinking and Data Science. Besides that, every now and then, some coding tasks on Exercism or Codewars. Recently, I’ve started with the Advent of Code challenges, which I plan to solve using different programming languages. Python got the year 2015, the very first in the series of the AoC events. Let’s see how that goes!

Nowadays, as I’m progressing from dabbling in it to using it professionally, I am enrolled in the Intermediate Python Nanodegree offered by Udacity. The best part is that I get to learn Python on the job. Yay!

First impressions

The very first impression I had about Python was that it is a straight to the point kind of language, succinct, without too much ceremony. A typical example you might find is the following:

This is how it looks in Java(this code goes into file):

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello, World!");

Then you do: javac and java HelloWorld or java and you get the greeting.

This is how it looks in Python(this code goes into

print('Hello, World!')

Then you do: python3 and you get the greeting.

As you can see, there is a lot going on in the Java code compared to the Python code. If you copy the Java code as is, it will just work, but there is a lot of prerequisite knowledge to understand what’s going on. Here are some important points:

  • because the class is public is has to be defined in a file with the same name and extension .java, and you can only have one public class in this file
  • if the class is not public, then the names can differ and you can have multiple such classes in the same file
  • we compile with javac and run with java A; new versions of Java can do it in one go, i.e. java, but this comes with other interesting aspects
  • you need this method definition: public static void main(String args[]), otherwise the code will not execute; you can replace args with another name, but it has to be a valid one
  • you have a System.out (!) thing in there
  • you might notice the presence of a HelloWorld.class file, do not look inside it! This is the bytecode you get after javac-ing your Java file, which is run by the Java Virtual Machine, which translates it into machine code

As you can see, that code is packed! As a programmer you just want to print a line of text, Python gives you that in a very straightforward way, Java adds a lot more ceremony to it.

This is a bit unfair, as in Java you have to have a class, so it makes sense to see how Python code might look like when we write the same functionality in a class.

class Greeting:
    def __init__(self) -> None:
        print('Hello, World!')

if __name__ == '__main__':

Now we start seeing the same ceremony, more or less, but somehow more cryptic. Some things which trip me up is the weird looking if with the __thing__ construct and the self in the __init__.

The if, which is not part of the class, mind the indentation as Python uses it to delimit blocks of code, is there to prevent instantiating (i.e. executing the __init__ method) and hence printing the greeting when the class is used in another Python file. On the other hand, when we feed the file to the Python interpreter, i.e. python3, it reads it top-to-bottom, sees the if at the bottom, the condition is True and hence the class is instantiated. This is so because __name__, a special variable, is set to __main__ under the hood. By the way, they call __ dunder, as in double underscore.

It also turns out that self can be called something else, and it will still work, but it is a convention to call it like that. Moreover, it seems there are a lot of conventions one should keep in mind when writing Python code. More on this in future posts.

We could als get rid of the -> None part in the method definition, this just specifies the return type, which in this case is None, i.e. nothing is returned, but people seem to have embraced the statically typed way of writing Python code. One other thing worth mentioning is that instantiating an object requires no new keyword as Java does => less typing.

Python is also quite easy going with " and ' as you can use them interchangeably, i.e. "Hello" and 'Hello' are the same. This is something Java frowns upon.

As you can see from these small examples, Python is less verbose and more flexible than Java, we’ll see in future posts situations where you end up with a couple of orders of magnitude more code in Java than in Python. This also makes Python a nice programming language for coding interviews.

One last thing I’d like to mention is that Java is keen on backwards-compatibility and you won’t experience breaking changes as you might have experienced in Python. A simple example here is the print function in version 3+ compared to versions 2.x:

print('Hi there!') # 3+
print "Hi", "there!" # 2.x

This post is the first in a series of posts on learning Python as a Java developer.