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.
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:
Simplicity | Performance |
---|---|
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. |
Community | Tools |
---|---|
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). |
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.
There are only a few differences; here is the explanation:
In Javascript | In Python |
---|---|
Number | Python 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: None | The 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 |
Array | In 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 |
Object | In 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 Tuples | JavaScript has nothing similar, but they can be very useful: Tuples are ordered; Sets are immutable, unordered sequences of values. |
String | It's the same in Python. |
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.
1from package1 import module1 2 3from package1.module2 import function1 4
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.
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.
1let result = '5' - '2'; 2// "result" now is equal to 3
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
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.
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
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]
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.
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);
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
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}
1colors = ["red", "green", "blue", "purple"] 2for color in colors: 3 print(color)
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);
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']
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
There is no way to do "switch"... but who cares? ๐
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
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";
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