Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to Lessons
Edit on Github

Understanding Python Syntax vs Javascript

Why Python?
JavaScript vs Python Syntax
  • Data-types

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 back-end 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 the advent of Node.js, JavaScript now also encompasses server-side capabilities. However, with JavaScript you were historically tied and limited to the browser, unable to access the client’s computer, as it was primarily seen as a rendering language. But Python was different, being a back-end 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:

SimplicityPerformance
Python was meant to be simple and easy. Here is the Python manifesto:
https://en.wikipedia.org/wiki/Zen_of_Python

Note: No more semicolons or curly brackets anymore, declaring variables, or the confusing this functionality.
Python is faster than Java, PHP, Ruby and 90% of the other back-end 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.
CommunityTools
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: MatLab (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.

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

Data-types

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 (float) 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 "Lists", and they are similar to JS Arrays but way more flexible and easier to work with.
myArray = ['Juan','John','Steven'] # List 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.
let myCar = {}
myCar.color = 'blue'

Python, on the other hand, separates the Dictionary data-type from 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, but they can be very useful: Tuples are ordered; Sets are immutable, unordered sequences of values.
StringIt's 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 variable's 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.

With Python

1from package1 import module1 2 3from package1.module2 import function1 4

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 between other data-types.

With JavaScript

1let result = '5' - '2'; 2// "result" now is equal to 3

With Python

1# In python subtracting strings will throw an error, instead you should do: 2 3result = int('5') - int('2') 4# "result" now is equal to 3

Printing Values

Python has print() for writing either into the console. Remember that, since Python (like any other back-end language) runs before the preload event, and it does not have access to the JavaScript console.

With JavaScript

1let simpleValue = 'Hello'; 2console.log(simpleValue); // This will print the content of the variable 3 4let arrayValue = ['Hello', 23, 76, 'World', 43]; 5console.log(arrayValue); // This will print the content of the array and its elements

With Python

1simpleValue = 'Hello' 2print(simpleValue) # This will print the content of the variable 3 4arrayValue = ['Hello', 23, 76, 'World', 43] 5print(arrayValue) # This will work too, 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." It's 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.

With JavaScript

1// Using an arrow function to map a list 2 3let peopleArray = [{ name: "Mario Peres" }, { name: "Emilio Peres" }, { name: "Yusaiba Peres" }]; 4let returningMapObject = peopleArray.map(person => person.name); 5console.log(returningMapObject);

With Python

1# Using lambda to map a list 2 3people_list = [{ "name": "Mario Peres" }, { "name": "Emilio Peres" }, { "name": "Yusaiba Peres" }] 4returning_map_object = map(lambda obj: obj['name'], people_list) 5names_list = list(returning_map_object) 6print(names_list) 7 8# Now names_list is a list of 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)

With JavaScript

1// Doing a forEach loop in JS 2myArray.forEach(function(item,index,array) { 3 console.log(item); 4}); 5 6// Doing a for loop in JS 7for(let i = 0; i < myArray.length; i++){ 8 console.log(myArray[i]); 9}

With Python

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

Adding and Removing Items

With JavaScript

1let myArray = ['Academy', 'Coding']; 2myArray.push('4Geeks'); // Adding an item 3 4// To remove the item in the index 1 position 5myArray.splice(1, 1);

With Python

1my_list = ['The', 'earth', 'revolves', 'around', 'sun'] 2 3my_list.insert(0, "Yes") 4print(my_list) # ['Yes', 'The', 'earth', 'revolves', 'around', 'sun'] 5 6my_list.remove("Yes") 7print(my_list) # ['The', 'earth', 'revolves', 'around', 'sun']

Sorting Functions for Lists

With Python

1# Ascending Sort 2number_list = [5, 2, 3, 1, 4] 3number_list.sort() 4print(number_list) # [1, 2, 3, 4, 5] 5 6# Sorting list of object using a "key" parameter 7my_list = [{ "name": "Mario Peres" }, { "name": "Emilio Peres" }, { "name": "Yusaiba Peres" }] 8my_list.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 no 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

Objects

With JavaScript

1// There are two ways of declaring an object 2 3// Like an object literal 4let obj = { "name": "Mario", "lastname": "Perez" }; 5 6// Like a class 7class Person{ 8 constructor() { 9 this.name = ""; 10 this.lastname = ""; 11 } 12} 13 14let obj = new Person(); 15obj.name = "Mario"; 16obj.lastname = "Perez";

With Python

1# In Python we have Classes and Dictionaries 2 3# Here is how you declare and use a dictionary 4my_dict = {} 5my_dict['name'] = "Mario" 6my_dict['lastname'] = "Perez" 7 8# Here is how you declare and use a class 9class Person: 10 def __init__(self): 11 self.name = '' 12 self.lastname = '' 13 14my_person = Person() 15my_person.name = "Mario" 16my_person.lastname = "Perez"

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