Python vs Java

I recently completed the JavaScript and Java courses on Codecademy*. (Tableau has a JavaScript API that can be harnessed to create really cool stuff.) But, as soon as I figured out Java, I needed to brush up on Python for an upcoming Data Science course.

Transitioning between the two is notoriously tricky. I found myself adding so many unnecessary characters that I decided to build a table to look at them side by side.

 

Python Java
Comment # //
Multiline Comment “”” /*
Boolean True true
Character n/a ‘A’
String “A” or ‘A’ “A”
x = 1

x = “A”

Legal illegal
Concatenation x = 3

s = “The answer is ” + str(x)

int x = 3

String s = “The answer is ” +  x;

Functions def square(x):

return x*x

public static int square(int x){

return x*x

}

If if x > 0:

  print ‘positive’

elif x < 0:

  print ‘negative’

else:

  print ‘zero’

if (x > 0) {

   System.out.println(“positive”);

} else if (x < 0) {

System.out.println(“negative”);

} else {

System.out.println(“zero”);

}

Boolean Operators and or not &&, ||, !
Loops for w in words:

   w = w.upper()

   print w

for i in range(2, 9, 3):

   print i

for (String w: words) {

   w = w.toUppercase();

   System.out.println(w);

}

for (int i = 2; i < 9; i +=3)

   System.out.println(i);

Dictionaries map = dict()

map[‘Jose’] = ‘773-000-1234’ map[‘Mary’] = ‘312-555-9999’

print map[‘Jose’]  

for key in map:

   print key

   print map[key]

Map<String, String> map = new HashMap<String, String>();

map.put(“Jose”, “773-000-1234”);

map.put(“Mary”, “312-555-9999”);

System.out.println(map.get(‘Jose’));

for (String key : map.keySet()) {

    System.out.println(key);

    System.out.println(map.get(key));

}

From <http://anh.cs.luc.edu/331/notes/JavaVsPython.html>

*The new Learn ___ courses seem crappy. The projects are behind a paywall, and the exercises seem much, much easier than legacy ones.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s