From Javascript to Python
Tags: javascript, python, beginners
Published: 2019-12-01 21:16:18

So you know javascript but you get this amazing offer to work at this great company in the perfect city and JS is nowhere to be found in their tech stack.  Instead, they use the Python-based Django web development framework.  So where do you start?  In this post, I will (hopefully) lay that foundation.  We will cover:
 1.  What is python?
 2.  Why learn python?
 3.  Basic(simple) datatypes
 4.  Lists
 4.  Variable declaration
 6.  Writing functions

It is expected that the reader is familiar with javascript so if that isn't the case, I recommend checking out this [intro to python](https://www.udemy.com/course/the-modern-python3-bootcamp) on Udemy where no prior programming knowledge of any kind is needed.

## What is Python?

From their website: *Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.*  **What??**  Yeah, that's what I thought the first time I read it, so let's break that sentence down:
 1. *interpreted* - just like javascript, *Python* is translated directly into code that a computer can read.  In javascript, this is usually handled by an interpreter like Chrome's V8 engine or Node.js on our computer or server.  Python has it's own interpreter that can be downloaded [here](https://www.python.org/downloads/).
 2. *object-oriented* refers to the way a Python program is organized/structured.  We use classes and objects in python to separate concerns, DRY up our code, and allow our programs to represent things in the **Real World**.  In contrast, *Javascript* is a functional programming language and uses functions to accomplish many of the same organizational strategies as Python.  Conceptually, there is a ton of overlap here that we could discuss for hours.  Coming from javascript, just know that a **Class** in Python is not a special type of function like it is in Javascript.
 3. *high-level programming language* - this refers to languages that are more comparable to human languages.  Basically, it is easy to read, even for non-programmers.  Javascript also is a high-level language though I would argue that it is not quite as easy to read compared to python.  We will revisit this comparison with code examples in a bit.
 4. *dynamic semantics* - basically, we can assign and reassign variables to point to different values and datatypes.  We can do this in javascript as well.

## Why Python?

According to Stack Overflow, it is the fastest growing programming language.  Python ranked 4th in most popular technology in the [2019 Developers Survey](https://insights.stackoverflow.com/survey/2019) on stack overflow.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ertbeezlube09wgfq9dl.png)

## Basic (Simple) Datatypes
*Time to see what Python is all about.  To try out any of the following code examples, I recommend using [repl.it](https://repl.it/languages/python3) or you can download the [python interpreter](https://www.python.org/downloads/)*.  We will be using python's built-in `type()` function for type-checking.  This is the equivalent of javascripts `typeof`.  To log values upon execution, we use python's console.log equivalent `print()`
 -  In javascript, we just have numbers.  Python breaks numbers into three categories: Ints, Floats, Complex Numbers.
 ```python
# int
print(type(10))
# >>> <class 'int'>

# float
print((type(10.11)))
# >>> <class 'float'>

# complex
print((type(10 + 5j)))
# >>> <class 'complex'>
```
 -  Strings and booleans are really similar to javascript.  Notice that **booleans are capitalized in python**.
```python
print(type("I am a string"))
# >>> <class 'str'>

# float
print((type(True)))
# >>> <class 'bool'>
```
  -  **None** datatype.  There is no null or undefined in python.  Instead, we have `None`
```python
print(type(None))
# >>> <class 'NoneType'>
```
*also notice that comments are written with # in python compared to // in javascript*

## Lists and other complex datatypes
*A list in python is pretty much exactly like an array in javascript.* Values (elements) are contained within **[]** and comma-separated.  We can also use bracket notation for access.
```python
my_list = [1,2,3,4]

print(my_list[2])

# >>> 3
```
Note that elements in a list may NOT be separated with spaces and like in javascript, a list is zero-indexed.
Other complex datatypes include:
  - tuples: immutable lists `my_tuple = (1,2,3,4)`
  - sets: unordered (not indexed) lists `my_set = {1,2,3,4}`
  - dictionaries: equivalent to objects in javascript (key-value pairs)
```python
my_dictionary = {
  "name": "Avery",
  "age": 32,
  "city": "New Orleans"
}

print(my_dictionary["name"])

# >>> Avery
```


## Variable assignment
When writing variables in javascript, we have quite a few choices to make.  Should we use `const`, `let`, `var`, or define a global variable without a keyword (never do this...)?  Well in python, we don't use any keyword, ever.  Convention for naming variables is to use snake case.  And just like javascript, variables are case sensitive.
```python
my_dog = "Jasmine"
x = 10
X = 100

print(my_dog)
print(x)
print(X)

# >>> Jasmine
# >>> 10
# >>> 100
```
## Writing functions
Functions in python are written with the `def` (short for define) keyword.  Unlike javascript, we use indentation to define the function body (no mustache brackets here!).  The norm is an indentation of **4** whitespaces, but you may indent however you like as long as you are consistent.
```python
def multiply_by_two(x):
    print(x * 2)

multiply_by_two(30)

# >>> 60
``` 
##What next?
Clearly, we have just scratched the surface of Python.  But as you can see, it shares many similarities with javascript.  Javascript developers familiar with ES6 Class syntax will be right at home when working with python classes.  Moving forward, I highly recommend Colt Steele's [The Modern Python 3 bootcamp](https://www.udemy.com/course/the-modern-python3-bootcamp).  The saying goes that learning your first language is always the hardest -- that certainly was the case from me as picking up python after javascript has been a smooth ride so far.  Best of luck on your python journey!