The title of this lesson should be "From Python to JS," because that’s the way history evolved. Python was born first and it’s way more mature. With Python you are capable of doing much more stuff because it’s a backend language and it has libraries and tools for anything you can think of.

Python and Javascript are friends. Together they make the best possible team to make any major development you could imagine.

Why Python?

With Javascript, you were tied and limited to the browser, you can’t access the client’s computer, and it is basically a rendering language. But Python is different - being a backend language, it runs on your own server – meaning you have access to and can control the entire computer with it. You have access to any application running on the same computer. You have access to the console. You have access to the network where the computer is connected to, and much more.

On the other hand, Python is the fastest growing back-end language in the world. It is the most versatile and easy-to-code language with one of the strongest communities.

When you compare it to other back-end languages, Python is leading in almost every functionality it offers: Data Science, AI, API developments, Web Developments, etc.

Here are some of the reasons Python has come to this point:

Python was meant to be simple and easy. Here is the Python manifest:

Note: No more semicolons or curly brackets anymore, or declaring variables, or the confusing "this" functionality.
Python is faster than Java, PHP, Ruby and 90% of the other backend languages. Only low level languages like C++ (hard to use) or very specialized like Node.js can beat it.

Python scalability has been proven over and over with applications like Google Search Engine, Youtube, Google Apps, etc.
Python is Google’s official language. It’s also one of the oldest languages with huge communities around each of its libraries\tools. MIT uses it to teach code. NASA to build rockets. Quora, Facebook, Yahoo, Amazon, etc. Almost every big company in the world has to use it!Most of the Python libraries are the best at what they do: MathLab (for data processing), Pandas (big data), (web server), Django (web framework), PyBrain (AI), TensorFlow (Machine Learning), etc. We could be here all day! The most amazing thing is that these libraries are only one "pip install" away (just like when using NPM with JS).

Javascript vs Python Syntax

Python and Javascript complement each other. In terms of functionality they have NOTHING in common – they don’t serve the same purpose, they don’t do the same things, they come from different backgrounds, etc.

The things that you will be familiar from Javascript are the basics of any programming language: looping, using conditionals, variables, classes, functions and objects.


There are only a few differences; here is the explanation:

In JavascriptIn Python
NumberPython has the same "Number" data-type but it can accept more options than JS, like fractions (2/3) or complex numbers.
myNumber = 23.23 //float
myNumber = 54 //integer
myNumber = 12.00 //float (even with 00 as decimals).
Undefined/Null is now: NoneThe undefined data-type is not available in Python. Here "undefined" and "null" are the same data-type: None.
myNumber //is None because it was not defined
ArrayIn Python, the Arrays are called "List" and they are similar to JS Arrays but way more flexible and easier to work with.
myArray = ['Juan','John','Steven'] //array of elements with numeric indexes
ObjectIn Javascript, objects and dictionaries are almost the same. You can do whatever you want to an object because you don’t have to declare its Class first and stick to its definition.
myCar = {}
myCar.color = 'blue'

Python, on the other hand, separates the Dictionary data-type form the Object data-type. Objects cannot be informally declared. You must first define their class before being able to instantiate them.

class Car(object):
def __init__(self, color):
self.color = color
myCar = Car('blue')
Sets and TuplesJavascript has nothing similar, they can be very useful: Tuples are ordered; Sets are immutable unordered sequences of values.
StringIs the same in Python.

Packages (Importing from other files)

In Javascript, you can import variables from other files using the import or require command, but you need to export the variables files first.

In Python, you can make any folder a package by creating a file inside of it. Then, you are able to import whatever you want into that folder without having to explicitly export anything.

python syntax With Python

from package1 import module1

from package1.module2 import function1

Package Managers

What NPM is for Javascript, PIP is for Python. Both beasts are amazing but very different inside. The biggest difference being that NPM packages are downloaded locally to a "node_modules" folder while PIP packages are installed on the entire machine – outside the project folder. Another small difference is that NPM uses a package.json and PIP uses a requirements.txt file.

python syntax With Python

# In python subtracting strings will throw an error, instead you should do: 

result = int('5') - int('2') 
#result now is equal to 3

Casting (parsing) Data-Types

Javascript is so flexible that you don’t have to pay much attention to data types. Python does not like that - in Python, you will get used to casting variables and converting them in between data-types.

python syntax With JavaScript

var result = '5' - '2'; 
//result now is equal to 3

python syntax With Python

# In python subtracting strings will throw an error, instead you should do: 

result = int('5') - int('2')
#result now is equal to 3

Printing Values

Python has "print" for writing either into a document or into the console. Remember that, since Python – like any other back-end language – runs before the preload event, it does not have access to the Javascript console.

python syntax With JavaScript

var simpleValue = ‘hello’;
//This will print the content of the variable  
var arrayValue = [‘Hello’,23, 76, ‘World’,43];
//This will print the content of the array and its elements.

python syntax With Python

simpleValue = ‘Hello’
print(simpleValue)  //this will print the content 
arrayValue =[‘Hello’,23,76,’World’,43]
print(arrayValue) //this will work, printing the content of the array in a format like this: [‘Hello’,23,76,’World’,43]

The Lambda Function vs Arrow Function

Finally, in ES2015, Javascript included the "arrow functions." That is a very easy and light way to declare and use functions. Python, on the other hand, has something similar called lambda functions that basically let you use little inline anonymous functions as shortcuts.

python syntax With JavaScript

# Using an arrow function to map a list 

var peopleArray = [{ name: "Mario Peres" },{ name: "Emilio Peres" },{ name: "Yusaiba Peres" }];
var returningMapObject = =>;

python syntax With Python

# Using lambda to map a list 

peopleArray = [{ "name": "Mario Peres" },{ "name": "Emilio Peres" },{ "name": "Yusaiba Peres" }]
returningMapObject = map(lambda obj: obj['name'], peopleArray)
namesArray = list(returningMapObject)

# now stringsArray is a list of just names like ["Mario Peres","Emilio Peres","Yusaiba Peres"]

📺 Here is a weird but amazing video explaining lambda functions:

Looping lists (similar to arrays)

python syntax With JavaScript

//doing a foreach loop in js 
myArray.forEach(function(item,index,array) {

//doing a for loop in js 
for(var i = 0; i < myArray.length; i++){

python syntax With Python

colors = ["red", "green", "blue", "purple"]
for color in colors:

Adding and Removing Items

python syntax With JavaScript

var myArray = [‘Academy’, ‘Coding’];
myArray.push(4Geeks’);  //Adding an item  

//to remove the item in the INDEX position  
myArray.splice(index, 1);

python syntax With Python

myList = ['The', 'earth', 'revolves', 'around', 'sun']
# Output: ['Yes', 'The', 'earth', 'revolves', 'around', 'sun'] 

['The', 'earth', 'revolves', 'around', 'sun']

Sorting Functions for Lists

python syntax With Python

# Ascending Sort 
a = [5, 2, 3, 1, 4]

# Sorting list of object using a "key" parameter 
myArray = [{ "name": "Mario Peres" },{ "name": "Emilio Peres" },{ "name": "Yusaiba Peres" }]
myArray.sort(key=lambda person: person['name'])

📺 Let’s summon Socratica again to understand sorting in Python:

The Switch Statement

There is now a way to do "switch"… but who cares? 🙂

Lists vs Tuples

Python brings a new kind of data-type called a "Tuple". Think about it like a super slim and fast performance List. But, like always, to increase performance we need to decrease functionality.

📺 This is a mandatory video explaining the difference between them:


python syntax With JavaScript

//There are two ways of declaring an object 

//Like an object literal 
var obj = { "name": "Mario", "lastname": "Perez" };

//Like a class 
class Person{
    constructor(){ = "";
         this.lastname = "";

var obj = new Person(); = "Mario";
obj.lastname = "Perez";

python syntaxpython syntaxpython tutorial python class With Python

# In Python we have Classes and Dictionaries 

# Here is how you declare and use a dictionary 
obj = {}
obj['name'] = "Mario"
obj['lastname'] = "Perez"

# Here is how you declare and use an class 
class Person:
    def __init__(self):
        name = ''
        lastname = ''

obj = Person() = "Mario"
obj.lastname = "Perez"

📺 Socratica, our great evolved specimen & friend, explains Objects in a great way: