Python vs Java

I personally believe that out of Python vs Java, Python must be used if your project/product/startup is based on AI/ML or Web Development.

Following are some quick points that show the difference and similarities b/w Java & Python

  1. Differences in Variable Typing
  2. Indentations – Java (don’t care) / Python (cares)
  3. Compilation – Both are compiled

Differences in Variable Typing  РJava (statically typed) / Python (dynamically typed)

Let’s see, what these two term mean.

In Statically Typed languages (like Java) A variable is bound both a name and a type of the variable.
So following code’s second line becomes invalid:

String name = "Akshat";
name = 10; //invalid line of code

However, in dynamically types languages (like Python) a variable is bound only to the name of the variable, not to its type. Which effectively means that any variable name in Python can hold any type of variable. The exact type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time

So following code is perfectly valid:

age = "10 years" //this is a string assigned to age variable
age = 10 //even though this is an integer, but can be easily assigned to the same variable

But that doesn’t mean that, the variables are automatically converted from one type to another in Python. Consider following code,

a = 5
a = "10"
b = 5

print a + b //this will through an error

So this code, returns an error because a & b are of different types, and an integer over here was not automatically converted to string since Python is Strongly typed (variable types are taken seriously!) but dynamically typed (the variable types are inferred automatically).

Indentations – Java (don’t care) / Python (cares)

Java is insensitive to code indentation. This means you can have any number of tabs in your program, and still do fine.

Python on the other hand is sensitive to indentation. For most languages, including Java, its not a requirement but a matter of style to give proper indentation in the programs but for Python it is the language requirement to have proper indentation in the program.

A simple if-else code in both the languages look like below

//properly indented version
if ( a > b ) {
      b = a;
}

//you can also write it like this. The code will run just fine
if (a > b) { b = a; }

Whereas, same code in Python must be written like below.

#however in python, it's compulsory to give tabs/indentation in code
if a > b:
        b = a

Compilation – Both are compiled

A very important thing that you must understand that, even both Java and Python are compiled languages! (On one hand you have PHP or Javascript which are scripting languages, and are not compiled up-front).
So Java is compiled into bytecode and then run on JVM (Java Virtual Machine).
Similarly, Python is also compiled into bytecode, and then run on your computer.
(You can also read about Compiled and Interpreted languages on Wiki also)

[Bonus] There are some common myths that people have about Python, which need to be busted:

  1. Myth #1: Python is a new language
  2. Myth #2: Python is not compiled
  3. Myth #3: Python is not secure
  4. Myth #4: Python is a scripting language
  5. Myth #5: Python is weakly-typed
  6. Myth #6: Python is slow
  7. Myth #7: Python does not scale
  8. Myth #8: Python lacks good concurrency support
  9. Myth #9: Python programmers are scarce
  10. Myth #10: Python is not for big projects

All the above points about Python, are plain wrong! Do read about in greater detail here

References

  1. Myths about Python by Paypal
  2. Image from xkcd
  3. Interpreted Language
  4. Compiled Language
  5. Static vs Dynamic Types – Deep Dive

Comments

comments