Python for JavaScript Developers

Mario Pabon on May 01, 2017

So recently I began working at a little startup in New York City by the name of, where I discovered that they had a back-end written ... [Read Full]
markdown guide

"For loops are like JavaScript foreach loops" Actually they're more like ES6's for of loops:

Also when creating classes in Python, you should inherit from object i.e. class Animal(object)


you should inherit from object

This only makes a difference in Python2, where there was 'old style' and 'new style' classes. In Python3 there'n so difference between
class Animal: and class Animal(object):

Also, in 2017 there's little reason to be using Python 3.0.1 when 3.6 is out :)


Aren't they exactly like JavaScript's loops?

The only difference seems to be Python doesn't do prototypes so you don't have to use hasOwnProperty to check the origin of an object's variables


"The for...of statement creates a loop iterating over iterable objects (including Array, Map, Set, String, TypedArray, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property."

In Python you can iterate over iterators or generators. When the end of iteration is reached, an exception is raised that triggers the loop to stop. To me, for..of seems closer to python

You're right.

for .. in iterates over key, i.e. it's comparable to something like:

for (let i = 0, o = Object.keys(X); i < o.length; i++, x=o[i]) { .. }

whereas for .. of iterates over iterables i.e. it's roughly comparable to something like:

for (let x, o; o && o.done === false; o =, x = o && o.value) { .. }

It's slightly more complicated than that because it implicitly converts some objects, such as Arrays or Maps, to iterables.

JavaScript since ES6 also has generators and iterators that work somewhat similar. There are no exceptions, tho, because iterator interface (that generators also need to adher to in general) returns an object on calls to .next() that is of form {done, value}. When done is false you've reached the end of the iterable.

But note that in python for key in {1: 10, 2: 20}: iterates over dictionary's keys.

You can use for value in d.keys():, for (key, value) in d.items(): and (redundant) for k in d.keys():.
These methods exist in both Python 2 and 3, though with some differences (see

You can drop the parenthesis i.e. for k, v in thing.items(). You only need parenthesis for comprehension i.e. {k: v for (k,v) in thing.items()} or [(k,v) for (k,v) in thing.items()]


Python is a very convenient language for programming and scripting. It can be used for creating automated testing frameworks, writing desktop applications, building scripts, system monitoring and logging tools, etc. Usually Python uses such frameworks as Django, Tornado, Flask, Pyramid, Zope, Twisted. Even though Python and JavaScript have a lot of differences, which you have described, it would be a great advantage for JavaScript programmers to learn Python. (


Python is a general purpose language like Javascript, I imagine they do all the same stuff coming from a Javascript developer. Everything you mentioned Javascript also already does such as writing desktop applications to building scripts, system monitoring, etc... Javascript also has a large number of frameworks (I'm referring to well maintained projects and frameworks that are a life and time-saver not some of the over polluted ones on npm) - I think it would also be a great advantage for python users to learn Javascript. Both are general purpose scripting languages and both do about the same thing on same platforms with same resources from the community just different ways of going about it.

Furthermore, and I'm open to correction because I could be wrong, Javascript may have better performance because it's almost always partially or completely compiled down to pure machine code while running and additionally some pretty intense optimizations are applied when compiled, some of them downright ingenious. I've seen some Javascript code outperform C++ like by 100 to 1000 times. I'm aware of Python having compiled code as well but as far as I know it's an explicit action you have to take through the command line to first compile it before running it but I'm not sure if the python interpreter actually compiles as it's running or to the efficiency of modern Javascript interpreters.

Regardless though both are heavily used and both have a lot of practicality in most things given their pretty general modular programming languages with a large community behind them so it'd be beneficial for programmers of one language to learn the other.


On the first run, the python interpreter creates „intermediate machine code“. The second run is cached.

Python interpreter doesn't create machine code. The code it creates is called p-code (or "intermediate representation", or "bytecode"). Incidentally the "p" does not stand for Python, but for Pascal, because the concept was first used in UCSD Pascal implementation. At that time majority of interpreters were interpreting the source text directly (which is much slower than parsing into AST or P representation).

However that is starkly different to what modern JavaScript runtimes do. JavaScript is converted to an AST intermediate representation in the first run but in subsequent runs it is compiled in much the same way that C, or Go, for example, are compiled. I.e. all the way to native, binary machine code for the CPU it's being executed on.

This design is based on JVM, and it's called JIT (just in time) compilation. Obviously due to it's dynamic typing systems a lot of object/variable handling is still done by the runtime rather than the direct code (but in many cases JIT optimizes code paths if it can infer it's exclusively dealing with, say, strings), which is why even V8 often executes code slower than, say, HotSpot, even if they both are very optimized JIT VMs.


JavaScripter and Python developer here :)

This article is going directly to my markers. Pretty good and useful! In fact, for newbies who are looking for job opportunities, JavaScript usually is the best option, cause lot of companies are looking for frontend developers and things like Angular, are on the top.

Python now has a lot of opportunities and I think every programmer should learn at least one scripting language, like this fantastic tool. So, congratulations, you have writed a really good transition guide :)


Nice article. I'm also JavaScript developer, that has to write Python code. One difference that is really surprising (and some time annoying) is lack of type coercion. I assumed that if Python is dynamically typed language, when I passed as an argument numer to method that expected string eg. print, Python did the type coercion internally. However, this is not a case. You have to do it explicitly eg. print(str(1))


Yes, this is the most important distinction. This also frustrates many people coming from languages other than PHP to JavaScript.

Namely, both JavaScript and PHP are not just dynamically typed, but also loosely typed in the sense that values are implicitly casted if it's possible, to match the type expected by function call or operator. Furthermore, in JavaScript there are very odd implicit casting rules that cast operands based on combination of the other operand and operator that aren't always intuitive (the + operator when either operand is a string comes to mind).

Python on the other hand is duck typed, which is a term invented to differentiate it's specific brand of dynamic typing that is not quite strong typing (as it doesn't require explicit interface adherence declaration), but not quite loose typing either (as it doesn't implicitly cast, i.e. it's behavior is more similar to that of, say, Ruby in this regard, than to that of JS or PHP).


Wat? First, why do you call print a method? It is a function. Second, it doesn't "expect" a string, it will take whatever you give to it (its signature starts with *args). And third, of course print(1) works, in every Python I know. What exactly did you try to write and what happened?


Maybe it is worth noting that the boolean operator 'and' and 'or' or in js respectively && and ||. It might fall into the basic information category, but if one purely programmed in js it can be a simple error.


hey! nice and simple article ;)
can I tranlate this to portuguese and post on my blog (with due source link, of course)?


Correction for Lists section:

a_list[2, -2]

doesn't work.
It's same as:

index = (2, -2)  # a tuple

which gives "TypeError: list indices must be integers or slices, not tuple".

Some custom list-like objects, notably NumPy arrays, do support extracting multiple values for multiple indexes. (Except they do this given a list of indixes, as they already interpret tuples for multi-dimensional indexing.)
Anyway, it's not a builtin feature of the language.


Seems to be a mistake on the strings example

name[0] = 'W' equal 'M' instead?


Nice catch! I must have written one of these lines while upside down.


Nice article! Having recently backed away from learning Python... This presents enough for me to see why it might even be a comfortable place to embrace, being somewhat split between Java"s strict typing and Javascript filmy one. Inspired to go have a look.

Best. Response. Ever.



Awesome post, Mario. Thanks for sharing your knowledge with the community! I'm a fan of Josh and everything stands for. I've used them in the past for finding engineering talent at Knotch. Keep up the great work!


If only tutorial books were written like this, great work , helped me get the general gist of the language enough for a confidence that i think i can write this


In the last example (inheritance), shouldn't get_address() method return self.__address instead of self.address?


I like to see more examples of Javascript. Because I am firn with Python and new to Javascript.


Hi Mario, So many years I work on JavaScript language. Now, I'm trying to Python but I struggled with Python concepts. I read you're article on Python. Thanks for writing wonderful article.

code of conduct - report abuse