JavaScript is my bread and butter, but I don’t dislike other programming languages, at all. I always had a crush for Python which I used over the years ranging from scripts to API applications with Flask and Django.

In this post I’ll guide you through a 10.000 feet comparison between these two languages.

To follow along you need at least a basic understanding of JavaScript and its quirks.

You can try the code examples for JavaScript in a browsers’s console or in the Node.js REPL. For Python, you can use the Python REPL. Every time you see >>> in the code examples, that’s the REPL.

Enjoy the reading!

  • Arithmetic operators in Python and JavaScript
  • Increment and decrement operators in Python and JavaScript
  • Comparison operators in Python and JavaScript
  • Logical operators in Python and JavaScript
  • Basic data types in Python and JavaScript
  • Regular expressions in Python and JavaScript
  • Python data types and JavaScript: immutability and variables
  • Complex data types in Python and JavaScript
  • Python data types: list and tuples
  • Python data types: dictionaries and JavaScript objects
  • No switch, no party
  • JavaScript object spread/merge, Python dict spread/merge
  • Python data types: Set and JavaScript Set
  • The membership operator in Python and JavaScript
  • Instance operators: a disclaimer
  • Python classes, isinstance, and JavaScript instanceof
  • Python type() and JavaScript typeof
  • How to handle exceptions in Python: what is an exception?
  • Handling synchronous exceptions in Python and JavaScript
  • How to handle exceptions: using the error message in Python and JavaScript
  • Raising your own exceptions in Python and JavaScript
  • Code documentation in Python with reStructuredText
  • A bit of type theory: dynamic typing vs static typing
  • Gradual typing in Python with reStructuredText
  • Static Typing in Python and JavaScript
  • Checking types with mypy
  • Python for JavaScript developers: going further

Arithmetic operators in Python and JavaScript

Let’s start with arithmetics!

Both Python and JavaScript share more or less the same arithmetic operators. In addition (no pun intended) to the division operator, Python has also a floor division operator.

Floor division returns the integer quotient of the division between two numbers. Consider the following example in Python:

>>> 89 / 4

## Output: 22.25

To get an integer instead of a float we can use floor division. (// in JavaScript is a comment):

>>> 89 // 4

## Output: 22

To replicate the same result in JavaScript you would use Math.floor:

Math.floor(89 / 4)

// Output: 22

Python’s arithmetic operators do not operate exclusively on numbers. For example, you can use multiplication on strings to repeat a pattern:

>>> "a" * 9

## Output: 'aaaaaaaaa'

Or addition as well to concatenate simple strings:

>>> "a" + "aa"

## Output: 'aaa'

For everything else Python raises a TypeError. That means you cannot sum number and string together:

>>> "a" + 9

## TypeError: can only concatenate str (not "int") to str

Nor divide them (besides making no sense):

>>> "a" / 9

## TypeError: unsupported operand type(s) for /: 'str' and 'int'

On this front JavaScript is a complete mess in the eye of an external observer because of the infamous type coercion. Not only JavaScript’s arithmetic operators are free to convert numbers to string:

"a" + 9

// Output: "a9"

They do not raise any error in case of invalid arithmetic operations:

"a" / 9

// Output: NaN

Instead we get NaN, a special JavaScript value resulting from invalid arithmetic operations, difficulty to deal with, until ECMAScript 2015 Number.isNaN.

Increment and decrement operators in Python and JavaScript

JavaScript has an increment and a decrement operator. Both can be used prefix (before the number):

var a = 34;
++a

or postfix (after the number):

var a = 34;
a++

In the first case (prefix), the operator increments the number and returns the new number:

var a = 34;
++a
// returns 35

In the second case (postfix), the operator increments the number, but returns the original number:

var a = 34;
a++
// returns 34
// increments to 35

The same rules are true for the decrement operator.

In Python instead there is no such thing as a decrement/increment operator. Rather you have to use an assignment operator. Here’s an increment assignment in Python:

>>> a = 34
>>> a += 1
## a is now 35

And here’s a decrement assignment:

>>> a = 35
>>> a -= 2
## a is now 33

Comparison operators in Python and JavaScript

JavaScript and Python have the same comparison operators, except for the triple equal, emblematic of JavaScript’s weird coercion rules.

To be precise Python has also the is operator (not discussed here) which for me falls more in the identity operators family.

What matters here is that Python is predictable when comparing values:

>>> 9 == "9"

## Output: False

JavaScript on the other hand performs a conversion every time the abstract comparison operator is involved:

9 == "9"

// Output: true

Here the first operator 9 is converted to a string right before the comparison. To avoid the conversion you must use the “strict comparison operator”, also called triple-equal:

9 === "9"

// Output: false

#python #javascript #web-development #programming #developer

Python for JavaScript Developers
2.60 GEEK