Unlocking Python: Essential Insights and Learning for JavaScript Developers

bryan_manuel_ramos

Bryan Ramos

Posted on June 30, 2024

Unlocking Python: Essential Insights and Learning for JavaScript Developers

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

//Variables
let x = 5
const y = 'John'
console.log(x)
console.log(y)
Enter fullscreen mode Exit fullscreen mode

Python

# Variables
x = 5
y = "John"
print(x)
print(y)
Enter fullscreen mode Exit fullscreen mode
  • Code Blocks and Indentation: Python uses indentation to define code blocks, while JavaScript uses brackets.

JavaScript

if (true) {
   console.log("This is true")
} else {
   console.log("This is false")
}

Enter fullscreen mode Exit fullscreen mode

Python

if True:
    print("This is true")
else:
    print("This is false")

Enter fullscreen mode Exit fullscreen mode
  • Conditionals: Python uses elif, in contrast to JavaScript's else if

Python

age = 18
if age < 18:
    print("Minor")
elif age == 18:
    print("Just an adult")
else:
    print("Adult")
Enter fullscreen mode Exit fullscreen mode

JavaScript

let age = 18
if (age < 18){
   console.log("Minor")
} else if (age === 18){
   console.log("Just an adult")
} else {
   console.log("Adult")
}
Enter fullscreen mode Exit fullscreen mode
  • 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

numbers = [1, 2, 3, 4]
for i in range(len(numbers)):
    print(numbers[i])
Enter fullscreen mode Exit fullscreen mode

Using for loop directly on the list:

Python

numbers = [1, 2, 3, 4]
for number in numbers:
    print(number)
Enter fullscreen mode Exit fullscreen mode
  • 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

//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)

Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode
  • 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

// 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);
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 or Leetcode. 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

💖 💪 🙅 🚩
bryan_manuel_ramos
Bryan Ramos

Posted on June 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024