JavaScript to Python 🐍

I recently applied for a software engineering role and instantly received a response, asking me to take on a code challenge. Now, the programming languages I am familiar with are Ruby and JavaScript, specifically JavaScript when tackling code challenges on LeetCode, HackerRank etc. However…their email read: “Our interview challenge is in Python — if you don’t know Python, that’s OK! The challenge is about testing how you solve problems, not how well you know Python.” 👀
They kindly gave 2 links to pick up python syntax and how things function. As I only had about 2 days to do my test, I had to quickly read up on those sources and try a few code challenges using Python.
So, what did I learn going from JavaScript to Python?
Numbers
JavaScript only has numbers which could either be a whole integer or a decimal. In Python, we have three types of numbers: Int, Float and Complex Numbers.
#Int
108#Float
2334.45#Complex number
9879jx
Variables
In JavaScript, we have three keywords when writing variables: var
, let
& const
but in Python we don’t need any keyword.
// JavaScript - Convention is to name variables using camelCasevar sayHello = "Hey!"
let randomNum = 5
const bye = "Bye"# Python - Convention is to name variables using snake_casesay_hello = "Hey!"
random_number = 21
bye = "adios!"
Arrays & Lists
Python does not have in-built support for Arrays like JavaScript but Lists, the closest thing to arrays for Python, can be a work around for implementing arrays in Python. Python also makes use of tuples which are immutable, while Lists are mutable.
Code blocks
This takes some time getting used to, as there is indentation to count for in Python. A block of code in JavaScript is defined by using curly braces {}
, whereas Python uses indentation.
// JavaScript loopfor (i = 0; i < example.length; i++) {
//some code
}# Python loopfor i in example:
# some code (indented)...Yes that is basically it for Python! Nothing else is required.
Also to end a statement in Javascript we use ;
but for Python we just go to a new line.
Dictionaries
Python makes use of dictionaries, or you may know them as hash tables, which are in-built however, JavaScript has no provision for built-in hash table support in any form.
There are many more differences and also some similarities between these programming languages and as my journey goes on, I will learn more and share more of these comparisons.