With the help of AI, I can now handle both frontend and backend development for a single feature. The backend language we use at my company is Python, so I'm keeping a record of what I learn.
Boolean Logic
Python uses and, not, or
def is_dev():
return (
not is_prod()
and not is_staging()
)
JavaScript uses !, &&, ||
function is_dev() {
return !isProd() && !is_staging()
}
if conditional statements
In Python, if does not use parentheses, and the condition must be followed by a colon.
def is_a_greater_than_b(a, b):
if a > b:
return "a is greater than b"
return "a is not greater than b"
In JavaScript, the condition needs parentheses, and no colon is required.
function is_a_greater_than_b(a, b) {
if (a > b) return "a is greater than b"
return "a is not greater than b"
}
Operators
In Python there is no ===, only ==.
def value_of_card(card):
if card in {"J", "Q", "K"}:
return 10
# Python has no === ; == does not do JS-style type coercion
if card == 'A':
return 1
return int(card)
javascript has ===
function value_of_card(card) {
const faceSet = new Set(["J", "Q", "K"])
if (faceSet.has(card)) return 10
if (card === "A") return 1
return parseInt(card)
}
Comparing three values
In Python, you have chained comparison.
def can_double_down(card_one, card_two):
values = {'J': 10, 'Q': 10, 'K': 10, 'A': 1}
v1 = int(values.get(card_one, card_one))
v2 = int(values.get(card_two, card_two))
return 9 <= v1 + v2 <= 11
In JavaScript you can only handle it separately.
function can_double_down(card_one, card_two) {
const values = { J: 10, Q: 10, K: 10, A: 1 }
const v1 = parseInt(values[card_one] || card_one)
const v2 = parseInt(values[card_two] || card_two)
const total = v1 + v2
return total >= 9 && total <= 11
}
Python strip vs JS trim
Python strings have a method for removing leading and trailing whitespace:
" hello world ".strip() # "hello world"
JavaScript's trim() does the same thing:
" hello world ".trim() // "hello world"
But Python's version takes an argument, which makes it a bit more powerful. The key thing about the chars parameter in str.strip(chars): it's a set of characters, not a prefix or suffix to match.
- Python breaks chars apart into individual code points and treats them as a set.
- It then walks the string from the left, one character at a time: if the character is in the set, it gets removed and Python moves on to the next one; the moment it hits a character that isn't in the set, it stops.
- The same happens from the right, working backwards from the last character.
- Everything in the middle is left untouched.
The order and the number of times these characters appear doesn't matter — any combination of them gets stripped off.
"xxyyxhelloyxyy".strip("xy") # "hello"
"hello world".strip("dlohe ") # "wor"
In the second example, h, e, l, l, o and the space are all in the set and get removed from the left, until w stops it; on the right, d and l are removed, and r stops it.
The classic trap is using it to strip a prefix:
"https://example.com".strip("https://") # 'example.com' looks right
"https://sample.com".strip("https://") # 'ample.com' the leading s got eaten too
That's because s is in the set {h, t, p, s, :, /} — after removing //, it just keeps going.
If you actually want to remove a fixed prefix or suffix, use removeprefix() / removesuffix() (Python 3.9+):
"https://sample.com".removeprefix("https://") # 'sample.com'
With no argument, chars defaults to all whitespace characters (space, \t, \n, \r, etc.), and again any combination of them is stripped: " \t\n hi \n ".strip() → 'hi'. There are also lstrip() and rstrip() if you only want to handle one end.
python join and js join
Python's join works much like JavaScript's, but the syntax differs. The separator goes first:
def make_word_groups(vocab_words):
prefix = vocab_words[0]
# The prefix is embedded in the separator: join only inserts between
# elements, so the first item stays as the bare prefix
return (" :: " + prefix).join(vocab_words)
make_word_groups(['en', 'close', 'joy', 'lighten'])
# en :: enclose :: enjoy :: enlighten
In JavaScript, join comes after:
function make_word_groups(vocab_words) {
const prefix = vocab_words[0]
return vocab_words.join(` :: ${prefix}`) // 'en :: enclose :: enjoy :: enlighten'
}
Python's str.join() requires all elements to be strings; it will throw a TypeError if it encounters numbers, so you need to use map(str, ...) first. JavaScript's Array.prototype.join() will automatically convert the strings, and null and undefined will be converted to empty strings.
Summing an array
Python has a built-in sum(list) function that returns the total of a list directly.
def card_average(hand):
"""Calculate and returns the average card value from the list.
Parameters:
hand (list): The cards in the hand.
Returns:
float: The average value of the cards in the hand.
"""
return sum(hand) / len(hand)
JavaScript has no built-in method for summing an array, so you have to do it manually:
function card_average(hand) {
const sum = hand.reduce((sum, item) => sum + item, 0)
return sum / hand.length
}
Floor division
Python's // is similar to JavaScript's Math.floor. In Python
mid = (low + high) // 2
In js
mid = Math.floor((low + high) / 2)
Counting elements that satisfy a condition
In Python, you can use a generator expression and pass it directly to sum:
def count_failed_students(student_scores):
return sum(1 for score in student_scores if score <= 40)
In JavaScript, you need to use filter:
function count_failed_students(student_scores) {
return student_scores.filter((score) => score <= 40).length
}