Skip to lesson
Python for JS Developers

Syntax BridgeLesson 3 of 4

Numbers & Math

4 min

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

JavaScriptPythonNote
6 / 2 → 36 / 2 → 3.0/ always returns a float
Math.floor(7 / 2)7 // 2Floor division operator
-7 % 3 → -1-7 % 3 → 2Result takes the divisor's sign
2n ** 100n (BigInt)2 ** 100int has unlimited precision
Math.round(2.5) → 3round(2.5) → 2Rounds half to even
parseInt("3.5") → 3int("3.5") → ValueErrorStrict conversion, no NaN
1 / 0 → Infinity1 / 0 → ZeroDivisionErrorRaises instead
x > 1 && x < 51 < x < 5Chained comparison
x++x += 1No ++ or -- operators

Side by side

JavaScript
// 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
# 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 Python

Exercise

Real Python, running in your browser

Write Python code for these tasks:

1. Split 17 cookies among 5 friends: store what each friend gets in per_friend and the leftovers in leftover (use // and %) 2. Store 2 ** 64 in big (no overflow, no BigInt needed) 3. price = "19.99" is a string: convert it to a float called price_num 4. is_close = whether 0.1 + 0.2 is close to 0.3 (use math.isclose) 5. age = 25, and in_range = whether age is between 18 and 30 (inclusive) using ONE chained comparison 6. Print per_friend and leftover on one line
Expected output
3 2

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