4Geeks logo
4Geeks logo
About us

Learning library

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

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

Full-Stack Software Developer

Data Science and Machine Learning - 16 wks

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

← Back to Lessons

Continue learning for free about:

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 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:

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

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.
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: 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.



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 (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 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, 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

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


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

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

python syntax 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 a document or 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.



python syntax With JavaScript

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

python syntax With Python

1simpleValue = ‘Hello’ 2print(simpleValue) //this will print the content 3arrayValue =[‘Hello’,23,76,’World’,43] 4print(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

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


python syntax With Python

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

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(var i = 0; i < myArray.length; i++){ 8 console.log(myArray[i]); 9}


python syntax With Python

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


Adding and Removing Items



python syntax With JavaScript

1 2var myArray = [‘Academy’, ‘Coding’]; 3myArray.push(‘4Geeks’); //Adding an item 4 5//to remove the item in the INDEX position 6myArray.splice(index, 1);

python syntax With Python

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

Sorting Functions for Lists

python syntax With Python

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

Objects

python syntax With JavaScript

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

python syntaxpython syntaxpython tutorial python class With Python

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

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