← Back to Lessons
Edit on Github

Understanding Python Syntax vs Javascript

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.py (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.
python>myNumber = 23.23 //float
python> myNumber = 54 //integer
python>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.
python>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.
python>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.
python>myCar = {}
python>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.

python>class Car(object):
python>def __init__(self, color):
python>self.color = color
python>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 _init_.py 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.

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’; console.log(simpleValue); //This will print the content of the variable var arrayValue = [‘Hello’,23, 76, ‘World’,43]; console.log(arrayValue); //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 = peopleArray.map(person => person.name); console.log(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) print(namesArray) # 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: https://www.youtube.com/watch?v=25ovCm9jKfA

Looping lists (similar to arrays)

python syntax With JavaScript

//doing a foreach loop in js myArray.forEach(function(item,index,array) { console.log(item); }); //doing a for loop in js for(var i = 0; i < myArray.length; i++){ console.log(myArray[i]); }

python syntax With Python

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

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'] myList.insert(0,"Yes") print(myList) # Output: ['Yes', 'The', 'earth', 'revolves', 'around', 'sun'] myList.remove("Yes") print(myList) ['The', 'earth', 'revolves', 'around', 'sun']

Sorting Functions for Lists

python syntax With Python

# Ascending Sort a = [5, 2, 3, 1, 4] a.sort() # 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: https://www.youtube.com/watch?v=QtwhlHP_tqc

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: https://www.youtube.com/watch?v=NI26dqhs2Rk


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.name = ""; this.lastname = ""; } } var obj = new Person(); obj.name = "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() obj.name = "Mario" obj.lastname = "Perez"

📺 Socratica, our great evolved specimen & friend, explains Objects in a great way: https://www.youtube.com/watch?v=apACNr7DC_s

Subscribe for more!






The most efficient way to learn: Join a cohort with classmates like yourself, live streamings, coding jam sessions, live mentorships with real experts and keep the motivation.


From zero to getting paid as a developer, learn the skills of the present and future. Boost your professional career and get hired by a tech company.


Start a career in data science and analytics. A hands-on approach with interactive exercises, chat support, and access to mentorships.


Keep your motivation with this 30 day challenge. Join hundreds of other developers coding a little every day.


Start with Python and Data Science, Machine Learning, Deep Learning and maintaining a production environment in A.I.

©4Geeks Academy LLC 2019

Privacy policies

Cookies policies

Terms & Conditions