Becoming a Pythonista

If the term “pythonista” seems unusual or off-putting to you, rest assured that you are not the only one. Personally, I find the word somewhat amusing, and I had to look it up initially. After all, “Python programmer” or “Python software engineer” would suffice, right? Regardless, this post is not about what we call individuals who work with Python; rather, it’s about sharing my experience of learning Python as a Java software engineer.

First and foremost, I want to clarify that my intention is not to ignite or fuel a language debate, such as Java versus Python. Instead, my sole purpose is to share my experience and perspective on learning a new programming language, particularly Python, while coming from a Java background.

So, why am I (re)learning Python? The brief answer is that my current company is shifting its focus and has chosen to utilize Python more extensively for product development. As a result, I have the opportunity to use Python in a more professional capacity than before, which is exciting! A more detailed explanation is that Python is predominantly used in Machine Learning and Data Science—both fields that have captured my interest. Moreover, as a general-purpose language, Python can be used for a wide range of applications, making it a valuable skill to possess.

In fact, this isn’t my first encounter with Python; I experimented with it a few years ago, specifically in early 2020, and have dabbled in it occasionally since then. I completed the Coursera Specialization Python for Everybody and two edX courses: Introduction to Computer Science and Programming Using Python and Introduction to Computational Thinking and Data Science.Additionally, I tackled coding tasks on Exercism and Codewars from time to time. Recently, I began participating in the Advent of Code challenges, which I plan to solve using various programming languages. Python was chosen for the year 2015, the inaugural event in the AoC series. I’m eager to see how it unfolds!

Currently, as I transition from casually experimenting with Python to utilizing it professionally, I am enrolled in Udacity’s Intermediate Python Nanodegree. The best part is that I have the opportunity to learn Python while working. It’s fantastic!

First impressions

My initial impression of Python was that it’s a straightforward, succinct language that doesn’t require much ceremony. A typical example you might come across is the following:

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

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

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

This is how it looks in Python(this code goes into hello-python.py):

print('Hello, World!')

Then you do: python3 hello-world.py and you get the greeting.

As you can see, the Java code appears more complex compared to the Python code. While the Java code will work as is, it requires a fair amount of prerequisite knowledge to fully comprehend what’s happening. Here are some key points to consider:

  • the class must be defined as public within a file with the same name and a .java extension. You can only have one public class in this file.
  • if the class is not public, the names can differ, and you can have multiple such classes within the same file.
  • to compile, use javac A.java and to run, use java A. Newer versions of Java can compile and run in one command, i.e., java A.java, but this introduces other interesting aspects.
  • the required method definition is public static void main(String args[]); otherwise, the code will not execute. You can replace args with another valid name.
  • the code includes a System.out statement.
  • after compiling, you may notice a HelloWorld.class file. Do not look inside it! This file contains the bytecode generated after using javac on your Java file. The Java Virtual Machine runs this bytecode, translating it into machine code.

As you can see, the code is quite dense! As a programmer, if your goal is simply to print a line of text, Python offers a straightforward approach, whereas Java introduces more ceremony to achieve the same result.

This comparison is somewhat unfair since Java requires a class for the code to function. Therefore, it would be more reasonable to examine how Python code would appear when implementing the same functionality within a class.

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


if __name__ == '__main__':
    Greeting()

Now we begin to see a similar level of ceremony, albeit somewhat more cryptic. Some aspects that I find confusing include the unusual if statement with the __thing__ construct and the use of self in the __init__ method.

The if statement, which is outside the class (note the indentation, as Python uses it to delimit blocks of code), prevents the instantiation of the class (i.e., executing the __init__ method) and thus the printing of the greeting when the class is used in another Python file. However, when we run the file using the Python interpreter (i.e., python3 my-file.py), it reads the file from top to bottom, encounters the if statement at the bottom, and the condition evaluates to True, leading to the class instantiation. This occurs because the special variable __name__ is set to __main__ internally. By the way, the double underscore __ is referred to as a “dunder”, which stands for double underscore.

Interestingly, the self parameter can be named differently, and the code will still function. However, it is conventional to use the term self. Furthermore, it appears that numerous conventions should be considered when writing Python code. We will delve into these conventions in upcoming posts.

We could also remove the -> None portion in the method definition, as it simply specifies the return type, which in this case is None, meaning nothing is returned. However, it seems that people have embraced the statically typed approach when writing Python code. Another notable difference is that instantiating an object in Python doesn’t require the new keyword, as Java does. This leads to less typing.

Python is more lenient with the use of double " and single ' quotes, allowing them to be used interchangeably — i.e., “Hello” and ‘Hello’ are considered the same. Java, on the other hand, does not permit such flexibility.

As illustrated by these small examples, Python is less verbose and more adaptable than Java. In future posts, we’ll explore situations where Java code may require significantly more lines than Python. This conciseness also makes Python an appealing choice for coding interviews.

Another point worth mentioning is Java’s commitment to backward compatibility. As a result, you’re less likely to encounter breaking changes in Java compared to Python. A simple example of this is the print function in Python version 3+ versus versions 2.x:

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

This post marks the beginning of a series dedicated to exploring the process of learning Python from the perspective of a Java developer. If you’re interested in the other parts of this series, you can find them below: