Python: Transitioning from Java to Python

29 Feb 2012

When I started to learn programming, my first language was Java. I really liked Java, because at the time it was the only language I found suitable for my projects. One day I had an introduction to Python and it really rocked my world, it was everything Java wasn't. In this tutorial I will explain and compare Java to Python and show how some things in Java are done in Python.

Two types of Python

There are actually two versions of the Python language. You have the 2.x series, with 2.7 being the latest release and 3.x with 3.2 being the latest release. Why ? Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better unicode support (with all text strings being unicode by default) as well as saner bytes/unicode separation.. 2.x still has better library support that 3.x, but when you can, use 3.x. Some of the new features are:

  • strings are unicode by default
  • clean unicode/bytes separation
  • exception chaining
  • function annotations
  • syntax for keyword-only arguments
  • extended tuple unpacking
  • non-local variable declarations
I will use Python 2.x and 3.x in my examples.

Short Comparison between Java and Python

Java Python
Static typing: Java uses static types, one needs to predefine a type (integer or string) Dynamic typing: You don't need to assign a type to a variable, types can be dynamically chosen.
Compiler Interpreter
Class oriented Object oriented
Many words are needed Few words are needed (one can express much in few words)
not compact extremely compact
no multiple inheritance multiple inheritance

Compiling vs. Interpreting

Instead of running the code in a JVM like Java does, Python interprets code. This means you need to go over the code every single time you run it. This is the normal procedure and makes Python run slower than Java, but Python wouldn't be Python if there wasn't a solution to this problem. In Python one can use many run-time implementations:
  • CPython is interpreted and is usually slower than the Java JIT compiler
  • JYthon is interpreted in the JVM
  • IronPython uses the same .NET libraries as C#
  • Python can be translated to native code via PyREX, PyToC and it will perform almost as good as C++


Also something very distinguishable between Python and Java is that Python does not use curly braces to define something, instead it uses indentation to structure code. This forces you into writing clean, structured code. Some people like this, others don't. Every nested block of commands must be indented the same amount. Python has no other way to differentiate between nested and non-nested blocks of commands. A correct example can be seen bellow:

class Hello(object):
    def __init__(self):
        self.helloWorld("Hello world") 
    def helloWorld(self,hello):
        print hello

Bad indendation:

class Hello(object):
def __init__(self):
    self.helloWorld("Hello world") 
    def helloWorld(self,hello):
    print hello

Most of your errors will probably come from using the wrong indentation. You might get frustrated at first, because it needs getting used to.

Hello world

In Java one would do:

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

In python you don't have to define a class to do something as you would do in Java.

print "Hello world"
print("Hello world")

Class definition

In Python you can define a class, but it is not necessary, that's why Python is also such a great scripting language.

class Hello(object):
    def __init__(self):
        self.helloWorld("Hello world") 
    def helloWorld(self,hello):
        print hello

To define a class, you must use: the class keyword and the name of the class (this doesn't need to be the name of your compilation unit as it is done in Java). Then you write a ":". The init method is the constructor. It is called when you make a Hello class. It uses "self" which is the current object and hello parameter. You also need to use self when refering to a method within the same class, e.g.

self.helloWorld("Hello World")

Self can be compared to "this." in Java. We use self in Python, because explicit is better than implicit. It is one of the rules in the Zen of Python. I did not write a Python 3.x example as only the print statement changes.

For loops

Writing a for loop in Python is very much different than in Java:

for (int i=0; i < 5; i++ ){
    def __init__(self):
        for i in range(1, 5):
            print i

The default step is 1. In the next example I shall use a step of 5:

    def __init__(self):
        for i in range(1,100, 5):
            print i

If we want to loop through an ArrayList in Java:

for (Object o: objects){

In Python you can do the same by:

    def __init__(self):
        for o in Object:
            print o;

Suggesting we have different Objects in our List, Python's syntax would not change. I was using the init method to emphasise the need of indentation. From here on out I will just write the code without the method statement. Another example:

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:
   print 'Current fruit:', fruit

When printing you can also use '+' as one would use in Java, however then you can only concatenate strings and it will give an error when you would do:

for i in range(1,100, 5):
    print "test" + i

Unless you type-cast it to a string:

for i in range(1,100, 5):
    print "test" + str(i)

Best practice is to just use just ',' as shown in the first example.

If, then, else statement

if(var == 1){
}else if( var == 2){

Would become:

if var == 1:
    print 1
elif var == 2:
    print 2
    print 3

There are also shorthand if-else statements in Python:

print "a" if var else "b"

This would print a if "var" is True and print b in every other case. Also when your if statement consists only of a single line, it may go on the same line as the header statement:

if ( var == 1 ): print "1"

Lists, Hashes and Iterators

In this chapter we will talk about data structures within Python.


Any array as we use in Java does not really exist in Python. We do however have a List, which works the same as in Java. Like a list you do not need to define the initial length.

breakfast = ["bacon", "eggs", "bread"]

When creating a List, you can mix object types. In the next example I will create a list with strings and integers.

breakfast = ["bacon", "eggs", 12]

To find the length of a List we can use the len function.

breakfast = ["bacon", "eggs", 12]
print len(breakfast)

This would output 3. To append something to a List:

breakfast = ["bacon", "eggs", 12]
print breakfast

This would give ['bacon', 'eggs', 12,'juice']. To add an item at a certain index:

breakfast = ["bacon", "eggs", 12]
print breakfast

This would give ['juice', 'bacon', 'eggs', 12]. To remove something you can use del or remove. Del is used if you know the index, remove to remove the first object that matches the parameter. Also note that after del is called the list automatically shifts.

breakfast = ["bacon", "eggs", 12]
print breakfast

This would output into ['bacon']. There are many more methods in the list object, you can find them here.


Dictionary in Python, in Java called the HashMap, is a list that uses a key-value pair.

myhash = {"hexample": "a cool website", "python": "a programming language", "Seven": 7}

To get a key, simply do:

myhash = {"hexample": "a cool website", "python": "a programming language", "Seven": 7}
a =  myhash["hexample"]
print a

To assign a new key:

myhash = {"hexample": "a cool website", "python": "a programming language", "Seven": 7}
myhash["Ubuntu"] = "Linux for human beings."


To use an iterator for a list you can have to explicitly create it:

mylist = [1,2,3,4,5]
i = iter(mylist)
item = #first item
item = #second item

There are many more datastructures in Python. To learn more visit this website.

Call by reference and call by value

Java uses call by value, this means we get the content of a certain memory position. Python works in the same way.

int x = 1;
int y = 2;
public void run(){
public void add(int var1, int var2){
    var1 += 1;
    var2 += 1;
    System.out.println("var1" + var1);
    System.out.println("var2" + var2);

The variables passed as arguments will still hold their original values. This means if we run this we will get as output 2,3,1,2. If we do this in Java we will get the same result:

x = 1
y = 2
def run(self):
    print x
    print y
def add(self,var1,var2):
    print var1
    print var2

Now if we want to do a call by reference (pointer in C), there is actually no way we can do this in Python. You have two options to do changes to your object: 1) Return the value

def return_a_whole_new_string(the_string):
    new_string = something_to_do_with_the_old_string(the_string)
    return new_string
# then you could call it like
my_string = return_a_whole_new_string(my_string)

2)Create a class in which you can call a function to alter your object

def use_a_wrapper_to_simulate_pass_by_reference(stuff_to_change):
    new_string = something_to_do_with_the_old_string(stuff_to_change[0])
    stuff_to_change[0] = new_string
# then you could call it like
wrapper = [my_string]

Python actually uses call-by-object rather than call-by-value or call-by-reference I suggest reading this article.

Final Word

This is just a brief introduction specially for those people transitioning from Java. In my future posts I will go deeper into subjects like inheritance, threading and Tuples.