Unlocking Python: Essential Insights and Learning for JavaScript Developers
Published: 2024-06-30 03:53:09

**`JavaScript`** was the first programming language I learned as a developer, but I chose to learn **`Python`** soon after for several reasons. In this blog, I will discuss the advantages of picking up **`Python`** and my experience learning the language, while also comparing its basic syntax to **`JavaScript's`**. Be sure to read until the end, where I'll be sharing my tips and tricks for **`JavaScript`** developers who are interested in learning **`Python`**.


## Why Python?
**Versatility-**
**`Python`** is a very versatile language, and by learning it you will open the doors to many domains that may seem out of reach for you. With the rapid growth of machine learning and big data, **`Python`** has become an indispensable tool in the tech industry. It also has an  extensive ecosystem of libraries and frameworks, which makes it an ideal choice for these advanced applications.

**Ease of learning-**
**`Python`** is known for being very easy to learn when compared to other programming languages, especially for those with prior experience. This can be attributed to its simple syntax and readability. In the realm of web development, using **`Python`** in conjunction with **`JavaScript`** can make for a very powerful application, so the fact that you can learn it fairly quickly and start utilizing it in your own projects is very advantageous.

**Real-World Application**
In a real-world job setting, you may be required to work with multiple technologies and languages simultaneously. By being comfortable with learning new technologies and adopting the skill of self-learning, you will increase your skill as a programmer and also make yourself a valuable asset for potential employers in the tech industry.

## Essential Python Syntax Compared To Javascript

- `Variable Declaration`: **`Python`** uses dynamic typing (x = 5), whereas **`JavaScript`** uses var, let, or const. 


**JavaScript**
```js
//Variables
let x = 5
const y = 'John'
console.log(x)
console.log(y)
```
**Python**
```python
# Variables
x = 5
y = "John"
print(x)
print(y)
```


- `Code Blocks and Indentation:` **`Python`** uses indentation to define code blocks, while **`JavaScript`** uses brackets.

**JavaScript**
```js
if (true) {
   console.log("This is true")
} else {
   console.log("This is false")
}

```

**Python**
```python
if True:
    print("This is true")
else:
    print("This is false")

```

- `Conditionals`: **`Python`** uses `elif`, in contrast to **`JavaScript's`** `else if`

**Python**
```python
age = 18
if age < 18:
    print("Minor")
elif age == 18:
    print("Just an adult")
else:
    print("Adult")
```

**JavaScript**
```js
let age = 18
if (age < 18){
   console.log("Minor")
} else if (age === 18){
   console.log("Just an adult")
} else {
   console.log("Adult")
}
```

- `Iteration`: **`JavaScript`** vs. **`Python`**

**`JavaScript`** uses for, while loops, for...of, and array methods like forEach, map, and filter for iteration.


**`Python`** primarily uses for and while loops.
The **`Python`** range function is commonly used in for loops to generate sequences of numbers (Parallel of `for let` loop in **`JavaScript`**.)

Using for loop with range:

**Python**
```python
numbers = [1, 2, 3, 4]
for i in range(len(numbers)):
    print(numbers[i])
```
Using for loop directly on the list:

**Python**
```python
numbers = [1, 2, 3, 4]
for number in numbers:
    print(number)
```

- `Function definition && arrow functions vs lambda functions`
**`Python`** uses def, while **`JavaScript`** uses function or arrow functions. **`Python`** lambda functions are used for simple, single-expression anonymous functions, while **`JavaScript`** arrow functions offer more flexibility and maintain `this` context for complex operations.

**JavaScript**
```javascript
//Function declaration
function add(a, b) {
   return a + b
}
console.log(add(1,2))
//Arrow function
const addArrow = (a,b) => a + b
console.log(addArrow(1,2)

```
**Python**
```python
#Function declaration
def add(a,b):
   return a + b
print(add(1,2))
#Lambda function
addLambda = lambda a, b: a + b
print(addLambda(1, 2))
```

- `Data structures`: 
Lists and Arrays: **`Python`** has lists that are similar to JavaScript arrays but more versatile.
Dictionaries and Objects: **`Python`** uses dictionaries for key-value pairs, similar to JavaScript objects.

**JavaScript**
```javascript
// Creating an array
const fruits = ["apple", "banana", "cherry"];

// Accessing elements
console.log(fruits[0]);  

// Adding an element
fruits.push("orange");
console.log(fruits); 

//Creating an object
const person = {
    name: "John",
    age: 30,
    city: "New York"
};

// Accessing values
console.log(person.name);  

// Adding a new key-value pair
person.email = "john@example.com";
console.log(person);
```
**Python**
```python
# Creating a list
fruits = ["apple", "banana", "cherry"]

# Accessing elements
print(fruits[0]) 

# Adding an element
fruits.append("orange")
print(fruits)


# Creating a dictionary
person = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Accessing values
print(person["name"]) 

# Adding a new key-value pair
person["email"] = "john@example.com"
print(person)
```

## My Tips for Learning Python as a Javascript Developer

The biggest advice I would give to those learning **`Python`** as a **`JavaScript`** developer is to start by familiarizing yourself with **`Python's`** syntax, which is quite different from **`JavaScript’s`**. Once you have a basic understanding of the syntax, dive into code challenges to reinforce your learning. You can find some on [Codewars](codewars.com) or [Leetcode](https://leetcode.com/). I chose to redo code problems I was already comfortable with in **`JavaScript`**, in **`Python`**. It allowed me to learn through practical application, and made it easy for me to  apply **`Python`** methods and best-practices hands-on. Additionally, project-based learning is invaluable; by working on projects, you’ll encounter real-world problems and solutions, solidifying your knowledge and giving you a deeper understanding of how to use **`Python`** effectively in different contexts. When I was first learning **`Python`**, I chose to implement it into a full-stack project to facilitate data analysis and data cleaning. By doing so, I ended up not only getting more comfortable with the language, but also more comfortable with back-end development. This approach of learning syntax, tackling code challenges, and engaging in project-based learning ensures a comprehensive and practical mastery of **`Python`**.

## Resources for Learning Python
Automate the Boring Stuff with Python: https://automatetheboringstuff.com
Learn Python 3 Course https://www.codecademy.com/courses/learn-python-3
Official Python Documentation: https://python.org