Python and JavaScript

Differences between Python & JavaScript in coding

Anna Wong
Analytics Vidhya

--

Learning 2 languages at the same time is not easy. I’ve encountered this during my last semester while completing my Diploma in Web and Programming from Singapore Polytechnic.

Both languages are similar, yet different. At times, I do get mixed up between the different syntax.

Differences

JavaScript uses curly braces while Python uses indentation.

JavaScript is a loosely-typed language while Python is more structured.

Python is widely used for data analytics, machine learning, back-end web applications and desktop applications.

JavaScript is used mainly for web applications, front-end web development and games development.

Identifiers

An identifier is a name used to identify a variable, function, class, module or property.

JavaScript identifiers can contain dollar sign ($) but Python does not allow punctuation characters such as @, $, and %.

Both identifier can start with a letter but not begin with a digit. Letters A to Z or a to z or an underscore (_) are commonly used within the identifier.

Control Structure

Using the same variable name, you would notice the difference in the way it’s coded.

If-else

JavaScript uses if and else. Semi-colon (;) in JavaScript is used to separate statements.

if (marks >= 80)
grade = "A";
else if (marks >= 70)
grade = "B";
else if (marks >= 60)
grade = "C";
else if (marks >= 50)
grade = "D";
else
grade = "F";

Python uses if, elif and else. Note that colon (:) is place after each of them.
In JavaScript, else if is used in replacing elif in Python.

if marks >= 80:
grade = “A”
elif marks >= 70:
grade = “B”
elif marks >= 60:
grade = “C”
elif marks >= 50:
grade = “D”
else:
grade = “F”

For loop

Let’s use the example below as an output that we want to achieve using both languages.

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

This is how JavaScript would look.

for (i = 0; i < 5; i++) {
text += “The number is “ + i + “<br>”;
}

And this is how Python would look.

for i in range(0, 5):
print(“The number is “, i,”\n”)

While loop

Using the same output example, let’s see how JavaScript would look:

var text = “”;
var i = 0;
while (i < 5) {
text += “<br>The number is “ + i;
i++;
}

And this is how Python would look:

i =- 1
while (i < 4):
i += 1
print(“The number is “, i,”\n”)

Notice the difference between i++ and i += 1? This is the difference which at times, I myself would also get confused.

JavaScript has another do-while loop which Python does not have.

Functions

If we need the output of 15, this is how JavaScript would look:

function simpleMath() {
return 5 * 3;
}

And this is how Python would look:

def simpleMath():
return 5 * 3

Using the same function name, the difference here is Python uses def with colon.

Similarities

Both languages identifiers are case sensitive. E.g. Apple and apple are 2 different identifiers.

Identifier cannot be same as reserved word for both languages.

Camel case is used in both languages. In fact, camel case is used for all programming languages.

Both have similar data type (list, array, strings, boolean), operators and control structure.

Conclusion

Learning both languages at the same time is achievable.

Here’s a tip, you may wish to study Python first, then dive into JavaScript later as Python is more structured and easier to learn. For myself, I learnt Python first before I headed towards JavaScript (some background in HTML and CSS does help!).

Next, allocated different day slot for yourself if you wish to learn both during the same period. This is to steer clear of confusion.

For example, if you learn Python on Saturday, then allocate Sunday to learn JavaScript. Try to schedule different day to learn different language. Concentrate each on each day.

--

--

Anna Wong
Analytics Vidhya

A tech-savvy individual equipped with some coding skills