Python Reference for JavaScript Developers

“a python that has keys of a keyboard on it and hands typing on the keys, anime” DALL-E

I’ve known python for quite some time, but rarely use it. As clients are moving more and more into using OpenAI, some are preferring to leverage python over Java or JavaScript. As a seasoned JavaScript developer, I asked ChatGPT:

I am a highly experienced JavaScript developer, working both front end and back end with node. I would like to learn how to write python. Help me learn to write python drawing from my experience with JavaScript be sure to provide context and comparisons and contrast between the two languages, you can explain it to me in one piece at a time and I will ask you to continue as I learn

And the output was amazing (below). I could’ve googled all of this, as the need arose, but quickly reviewing this gave me some great context to get up to speed quickly on python. I then later asked for specifics when dealing with specific things, like arrays.

I am sharing this as a great use case for ChatGPT, but also for my own reference later on when I forget things.

Variables and data types:

JavaScript:

  • Variables can be declared using var, let, or const.
  • JavaScript has six data types: Number, String, Boolean, Null, Undefined, and Object.
var age = 30;
let name = "John Doe";
const pi = 3.14159;

Python:

  • Variables don’t require any keyword for declaration.
  • Python has several data types like int, float, str, bool, list, tuple, set, and dict.
age = 30
name = "John Doe"
pi = 3.14159

Functions:

JavaScript:

  • Functions are defined using the function keyword and can be named or anonymous.
function add(a, b) {
  return a + b;
}

const subtract = function(a, b) {
  return a - b;
};

Python:

  • Functions are defined using the def keyword.
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Arrays and Lists:

JavaScript:

  • Arrays are used to store multiple values in a single variable.
let fruits = ["apple", "banana", "cherry"];

Python:

  • Lists are used for the same purpose as arrays in JavaScript.
fruits = ["apple", "banana", "cherry"]

Conditional statements:

JavaScript:

if (age > 18) {
  console.log("You are an adult");
} else {
  console.log("You are not an adult");
}

Python:

if age > 18:
    print("You are an adult")
else:
    print("You are not an adult")

Looping:

JavaScript:

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

fruits.forEach(function(fruit) {
  console.log(fruit);
});

Python:

for i in range(len(fruits)):
    print(fruits[i])

for fruit in fruits:
    print(fruit)

indexOf (JavaScript) / find (Python):

JavaScript:

The indexOf method returns the index of the first occurrence of the specified value in the string, or -1 if not found.

let str = “Hello, world!”;
let index = str.indexOf(“world”);
console.log(index); // 7

Python:

The equivalent method in Python is find. It returns the index of the first occurrence of the specified value in the string, or -1 if not found.

str = “Hello, world!”
index = str.find(“world”)
print(index)  # 7

replace:

JavaScript:

The replace method searches for a specified value in the string and replaces it with another value.

let str = “I love apples!”;
let newStr = str.replace(“apples”, “bananas”);
console.log(newStr); // “I love bananas!”

Python:

The replace method in Python works similarly.

str = “I love apples!”
new_str = str.replace(“apples”, “bananas”)
print(new_str)  # “I love bananas!”

length (JavaScript) / len() (Python):

JavaScript:

The length ~property~ returns the length of a string.

let str = “Hello, world!”;
let strLength = str.length;
console.log(strLength); // 13

Python:

In Python, you can use the len() ~function~ to get the length of a string.

str = “Hello, world!”
str_length = len(str)
print(str_length)  # 13

toLowerCase() (JavaScript) / lower() (Python): Converts the string to lowercase.

JavaScript

let str = “Hello, World!”;
let lowerStr = str.toLowerCase();
console.log(lowerStr); // “hello, world!”

Python

str = “Hello, World!”
lower_str = str.lower()
print(lower_str)  # “hello, world!”

toUpperCase() (JavaScript) / upper() (Python): Converts the string to uppercase.

JavaScript

let str = “Hello, World!”;
let upperStr = str.toUpperCase();
console.log(upperStr); // “HELLO, WORLD!”

Python

str = “Hello, World!”
upper_str = str.upper()
print(upper_str)  # “HELLO, WORLD!”

split():

JavaScript

let str = “apple,banana,cherry”;
let fruits = str.split(“,”);
console.log(fruits); // [“apple”, “banana”, “cherry”]

Python

str = “apple,banana,cherry”
fruits = str.split(“,”)
print(fruits)  # [‘apple’, ‘banana’, ‘cherry’]

Equality (== in JavaScript, == in Python):

JavaScript:

let x = 5;
let y = "5";
console.log(x == y); // true (compares values, not types)

Python:

x = 5
y = "5"
print(x == y)  # False (compares both values and types)

Inequality (!= in JavaScript, != in Python):

JavaScript:

let x = 5;
let y = "5";
console.log(x != y); // false

Python:

x = 5
y = "5"
print(x != y)  # True

Strict equality (=== in JavaScript):

JavaScript:

let x = 5;
let y = "5";
console.log(x === y); // false (compares both values and types)

Python:

Python does not have a strict equality operator. The regular equality operator (==) compares both values and types.

Strict inequality (!== in JavaScript):

JavaScript:

let x = 5;
let y = "5";
console.log(x !== y); // true

Python:

Python does not have a strict inequality operator. The regular inequality operator (!=) compares both values and types.

Greater than (>):

JavaScript:

console.log(10 > 5); // true

Python:

print(10 > 5)  # True

Less than (<):

JavaScript:

console.log(5 < 10); // true

Python:

print(5 < 10)  # True

Greater than or equal to (>=):

JavaScript:

console.log(5 >= 5); // true

Python:

print(5 >= 5)  # True

Less than or equal to (<=):

JavaScript:

console.log(5 <= 5); // true

Python:

print(5 <= 5)  # True

NOT operator:

JavaScript:

let age = 16;
let is_adult = !(age < 18);
console.log(is_adult); // false

Python:

age = 16
is_adult = not (age < 18)
print(is_adult)  # False

find (JavaScript) / list comprehension + if or next + filter (Python):

JavaScript:

const numbers = [1, 2, 3, 4, 5];
const found = numbers.find(number => number > 3);
console.log(found); // 4

Python:

numbers = [1, 2, 3, 4, 5]

# Using list comprehension and if
found = [number for number in numbers if number > 3][0]
print(found)  # 4

# Using next and filter
found = next(number for number in numbers if number > 3)
print(found)  # 4

map (JavaScript) / map() or list comprehension (Python):

JavaScript:

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(number => number * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

Python:

numbers = [1, 2, 3, 4, 5]

# Using map()
doubled = list(map(lambda number: number * 2, numbers))
print(doubled)  # [2, 4, 6, 8, 10]

# Using list comprehension
doubled = [number * 2 for number in numbers]
print(doubled)  # [2, 4, 6, 8, 10]

forEach (JavaScript) / for loop (Python):

JavaScript:

const numbers = [1, 2, 3, 4, 5];
numbers.forEach(number => console.log(number));
// Output:
// 1
// 2
// 3
// 4
// 5

Python:

numbers = [1, 2, 3, 4, 5]
for number in numbers:
    print(number)
# Output:
# 1
# 2
# 3
# 4
# 5

Leave a Reply

Up ↑

%d bloggers like this: