A basic comparison between Python and Javascript - part 1
Tags: javascript, python, programming
Published: 2022-07-28 01:52:00

Hi there! If you know or study Python and/or Javascript, this guide could help you! It could at least reinforce a little your understanding of their syntax.
Python and Javascript are relativily new languages (not that much), and they're are [2 of the most used programming languages in the world](https://www.stackscale.com/blog/popular-programming-languages-2021/). Above I'll shortly compare their:
* Declaration of variables and functions
* Data Types

## Declarations
To work with static or dynamic data, we must know how to properly allocate and change those values.
### Variables
#### Python
Python has no command for declaring a variable. It is created when some value is assigned to it, thus it is direct and simple. Cause there is no command for a variable declaration, there is no problem in reassigning them to another value or other type of value.
```
name = 'Marcos'
age = 28
age = "twenty eight"
```
#### Javascript
There are diferents ways to declare a variable in Javascript, according to its purpose. The 3 must common ways are using the keywords **var** (_older, mainly used until 2015, accepts value reassignment_), **let** (_used to declare mutable data_), or **const** (_used to declare imutable data_). All variables must also be declared with a name, called <u>identifier</u>.
```
var race = 'human'
const hobby = "Gaming"
let rank = 10
```
### Functions
To do a particular task that may be repetitive, we use a block of code called <u>function</u>. It does nothing until it is '_called_' (invoked, or used) somewhere else. It can be declared with or without arguments. Arguments are data that we can pass into a function.
#### Python
To declare a function with Python we must use the **def** keyword, and all lines of interest for its scope must be indented. 
```
def simpler_function():
  simple_data = "anything you want"
  return simple_data + "is on the other side of fear"

simpler_function()

def dynamic_function(any_argument):
  return any_argument + "is on the other side of fear"

dynamic_function("anything you want")
```
#### Javascript
A JavaScript function is defined with the **function** keyword. Its scope is placed inside curly brackets.
```
function getResult() {
  result = 42
  return result
}

getResult()

function getAnswer(problem) {
  return 42
}

getAnswer('Universe and everything else')
```
## Data Types
Programming is all about data dealing, so it's important to understand which kind of data each language works with, and how they do it.
### Numbers
#### Python
Basic numbers in Python are of 2 types: **integers** (_0, negative and positive whole numbers without a fractional part_), and **floats** (_negative and positive numbers with a frational part denoted by the deciman symbol `.`_)
```
integer_value = 8
float_value = 8.0
```
#### Javascript
Javascript has only one type of number. They can be written with or without decimals.

```
let integer_number = 3
let irrational_number = 3.14
```
### String (texts)
#### Python
Any kind of data between single or double quotation marks. 
```
passion = "I love to discover new things."
```
#### Javascript
Exactly the same as Python strings
```
const desire = "I wanna travel all around the world!"
```
### Boolean
#### Python
Only have two values, written with the first letter capitalized: `True` or `False`.
#### Javascript
Also only the same two values, but written all in Small Case letters: `true` or `false`.
### Containers
#### Python
The 3 most used types of data containers in Python are <u>tuples</u>, <u>lists</u>, and <u>dictionaries</u>.
##### Tuple
An immutable sequence of ordered data. After it is declared, cannot be increased or decreased, and neither its items can be changed. In its declaration, it is used a sequence of data between round brackets.
```
imutable_data = ("any", 'kind of data', True, 8)
```
##### List
Created using square brackets, it is mainly used to store ordered multiple items in a single variable. Lists are mutable, as we can modify their items (_using their index_), add new items (_using **append** keyword_), and delete any of them (_using **remove** keyword_).
```
fruits = ["apple", 'orange']
fruits.append("grape")
fruits.remove('apple')
```
##### Dictionary
Dictionaries are used to store data values in _key:value_ pairs. It's mutable, but do not allow duplicated keys. It is declared within curly brackets, and a key is separated from its value by the **comma** symbol (`:`). The values in dictionary items can be of any data type.
```
pets = {
  "cats": 2,
  "dogs": False,
  "names": ["Mira", "Andromeda"]
}
```
#### Javascript
There are 2 basic data containers in Javascript: <u>Arrays</u> and <u>Objects</u>
##### Arrays
Its main structure is exactly the same as Python's List: a special variable, which can hold multiple values of different types, ordered between square brackets. To add new items, we use the method `push()`, and to remove we use `pop()` method.
```
const biggestWishes = ["freedom", "happiness", "love", "imortality"]
biggestWishes.push("wealthy")
biggestWishes.pop("imortality")
```
##### Objects
They are written with curly brackets, to contain multiple _key:value_ pairs, separated by commas.
Don't allow duplicated keys. The values for each key can be of any data.
``` const comparison = {
  languages = ['Python', 'Javascript' ],
  level = 'initial',
  topics = 2
}
```

## Final Considerations
There are many other topics, so I intended to write others to also basic and short comments about them. I have no interest to provide solid knowledge with this article; for that, I suggest Google it, there are bazillions of complete and comprehensive guides for each language. Go for it!
But if anyone could suggest any modification or wanna ask questions, please, reply, I'll take a look ASAP. (=