Syntax BridgeLesson 3 of 4
Numbers & Math
JS has a single number type: every Number is a 64-bit float, and BigInt was bolted on later for big integers. Python has two everyday types instead: int and float. A float is the same 64-bit float you know from JS. An int, however, has unlimited precision. 2 ** 100 is computed exactly, and there is no MAX_SAFE_INTEGER to worry about.
Division is where the difference shows. The / operator ALWAYS returns a float in Python: 7 / 2 is 3.5 and even 6 / 2 is 3.0. When you want whole-number division, use the floor division operator //: 7 // 2 is 3. It floors toward negative infinity, so -7 // 2 is -4, not -3. The modulo operator % follows the same logic: the result takes the sign of the divisor, so -7 % 3 is 2 in Python but -1 in JS. That makes % reliable for things like wrapping list indexes. Exponentiation is ** in both languages.
Floats behave exactly like in JS, including 0.1 + 0.2 == 0.30000000000000004. Compare floats with math.isclose(a, b), and use decimal.Decimal (created from strings, like Decimal("0.10")) when you need exact decimal arithmetic such as money. Another surprise: round() uses banker's rounding (round half to even), so round(2.5) is 2 and round(3.5) is 4, while JS's Math.round(2.5) gives 3.
Conversions are strict. int("42") works, but int("3.5") and int("abc") raise ValueError, where JS's parseInt would quietly return 3 or NaN. int(3.9) truncates toward zero to 3. Dividing by zero raises ZeroDivisionError instead of returning Infinity.
Two syntax gifts and one loss. You can chain comparisons: 1 < x < 5 means exactly what it looks like. You can write 1_000_000 for readability (JS has numeric separators too). But there is no ++ or --. Write x += 1. Beware: ++x is valid Python, but it means +(+x) and does nothing at all.
Quick reference
| JavaScript | Python | Note |
|---|---|---|
| 6 / 2 → 3 | 6 / 2 → 3.0 | / always returns a float |
| Math.floor(7 / 2) | 7 // 2 | Floor division operator |
| -7 % 3 → -1 | -7 % 3 → 2 | Result takes the divisor's sign |
| 2n ** 100n (BigInt) | 2 ** 100 | int has unlimited precision |
| Math.round(2.5) → 3 | round(2.5) → 2 | Rounds half to even |
| parseInt("3.5") → 3 | int("3.5") → ValueError | Strict conversion, no NaN |
| 1 / 0 → Infinity | 1 / 0 → ZeroDivisionError | Raises instead |
| x > 1 && x < 5 | 1 < x < 5 | Chained comparison |
| x++ | x += 1 | No ++ or -- operators |
Side by side
// JavaScript
console.log(7 / 2); // 3.5
console.log(Math.floor(7 / 2)); // 3
console.log(-7 % 3); // -1
console.log(2 ** 53 + 1); // 9007199254740992 (precision lost!)
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(Math.round(2.5)); // 3
console.log(parseInt("3.5")); // 3
console.log(Number("abc")); // NaN
let count = 0;
count++;# Python
print(7 / 2) # 3.5
print(6 / 2) # 3.0 (always a float)
print(7 // 2) # 3 (floor division)
print(-7 // 2) # -4 (floors toward -infinity)
print(-7 % 3) # 2 (sign of the divisor)
print(2 ** 53 + 1) # 9007199254740993 (exact!)
import math
print(0.1 + 0.2) # 0.30000000000000004
print(math.isclose(0.1 + 0.2, 0.3)) # True
print(round(2.5), round(3.5)) # 2 4 (half to even)
print(int("42"), float("3.5")) # 42 3.5
# int("3.5") -> ValueError
# int("abc") -> ValueError (not NaN)
x = 3
print(1 < x < 5) # True (chained comparison)
count = 0
count += 1 # no ++ in PythonExercise
Real Python, running in your browserWrite Python code for these tasks:
3 2
Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment