Skip to lesson
Python for JS Developers

Syntax BridgeLesson 4 of 4

Conditions & Truthiness

4 min

Conditionals in Python look almost like pseudocode. No parentheses around the condition, no curly braces for the body. Instead, a colon after the condition and indentation for the block. This feels liberating once you adjust, but the indentation is not optional: it IS the syntax.

Two things will trip you up immediately. First: it is elif, not 'else if'. Your fingers will type 'else if' for days. Second: boolean operators are words, not symbols. Use and instead of &&, or instead of ||, and not instead of !. Like && and || in JS, and and or short-circuit and return one of their operands, so name or "Anonymous" works the way you expect.

Truthiness rules differ in an important way. In JS, empty arrays [] and objects {} are truthy. In Python, empty lists, dicts, sets, tuples, and strings are ALL falsy, as are 0, 0.0, None, and False. This makes emptiness checks more intuitive: you just write 'if my_list:' instead of 'if (myList.length > 0)'. One reverse surprise: NaN is falsy in JS, but float('nan') is truthy in Python.

Indentation rules: Python does not require a specific number of spaces, but every line in a block must be indented by the same amount. An inconsistent indent raises IndentationError, and mixing tabs and spaces raises TabError. The PEP 8 standard is 4 spaces per level, and practically every Python codebase uses it. Configure your editor to insert 4 spaces when you press Tab and you will never think about this again.

One more thing that surprises JS developers: Python has no block scope. A variable created inside an if, for, or while block remains accessible after the block ends. Only modules, functions, and classes create new scopes (comprehensions also get their own small scope).

Quick reference

JavaScriptPythonNote
if (cond) { }if cond:No parens, no braces, colon + indent
else ifelifOne word, not two
&& / || / !and / or / notEnglish words
x === yx == yNo str/number coercion, but 1 == 1.0
cond ? a : ba if cond else bDifferent order!
[] is truthy[] is falsyEmpty collections are falsy

Side by side

JavaScript
// JavaScript
const score = 85;

if (score >= 90) {
  console.log("A");
} else if (score >= 80) {
  console.log("B");
} else {
  console.log("C");
}

const grade = score >= 90 ? "A" : "B";
// Falsy: 0, "", null, undefined, NaN, false
// Truthy: everything else (including [] and {})
Python
# Python
score = 85

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
else:
    print("C")

grade = "A" if score >= 90 else "B"
# Falsy: 0, 0.0, "", None, False, [], {}, set(), ()
# [] and {} are falsy here! float("nan") is truthy.

if score > 80 and score < 100:   # or simply: 80 < score < 100
    print("Great!")

Exercise

Real Python, running in your browser

Convert this JavaScript to Python. Watch the differences!

JavaScript to convert
// JavaScript
const temp = 35;
let message;

if (temp > 40) {
  message = "Too hot!";
} else if (temp > 30) {
  message = "Warm";
} else if (temp > 20) {
  message = "Nice";
} else {
  message = "Cold";
}

const isHot = temp > 30 ? true : false;
console.log(message + " | Hot: " + isHot);
Expected output
Warm | Hot: True

Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment