Operators & Expressions
An operator is a symbol that does something with values, such as + adding two numbers or > comparing them. A piece of code that produces a value, such as price * 3 or marks >= 33, is called an expression. Expressions are how programs calculate, compare and decide.
In this lesson you will learn the three main groups of operators (arithmetic, comparison and logical), the order in which they are worked out, and the small differences between Python and JavaScript that cause the most bugs.
Arithmetic operators
You already know most of these from school maths. The new ones are %, which gives the remainder after dividing, and **, which means "to the power of".
| Operation | Python | JavaScript | Example | Result |
|---|---|---|---|---|
| Add | + | + | 7 + 2 | 9 |
| Subtract | - | - | 7 - 2 | 5 |
| Multiply | * | * | 7 * 2 | 14 |
| Divide | / | / | 7 / 2 | 3.5 |
| Whole-number divide | // | Math.floor(7 / 2) | 7 // 2 | 3 |
| Remainder | % | % | 7 % 2 | 1 |
| Power | ** | ** | 7 ** 2 | 49 |
Here is a real use. Seventeen chocolates are shared equally among five children. How many does each child get, and how many are left?
chocolates = 17
children = 5
print("Each child gets:", chocolates // children)
print("Left over:", chocolates % children)
print("Exact share:", chocolates / children)
print("2 to the power 10:", 2 ** 10)Each child gets: 3 Left over: 2 Exact share: 3.4 2 to the power 10: 1024
JavaScript has no // operator. Instead, divide normally and round down with Math.floor():
const chocolates = 17;
const children = 5;
console.log("Each child gets:", Math.floor(chocolates / children));
console.log("Left over:", chocolates % children);
console.log("Exact share:", chocolates / children);
console.log("2 to the power 10:", 2 ** 10);Each child gets: 3 Left over: 2 Exact share: 3.4 2 to the power 10: 1024
The remainder operator % is surprisingly useful. A number is even when number % 2 is 0, and number % 10 gives its last digit.
Order of operations
You may have learnt BODMAS at school: brackets first, then division and multiplication, then addition and subtraction. Programming languages follow the same idea, called operator precedence. Multiplication and division happen before addition and subtraction, and brackets come first of all:
print(2 + 3 * 4)
print((2 + 3) * 4)
maths, science, english = 78, 85, 92
wrong_average = maths + science + english / 3
right_average = (maths + science + english) / 3
print("Wrong:", wrong_average)
print("Right:", right_average)14 20 Wrong: 193.66666666666666 Right: 85.0
Without brackets, only english was divided by 3. The program ran without any error, but the answer was nonsense. When in doubt, add brackets: they cost nothing and make your meaning clear.
Comparison operators
A comparison checks two values and gives a boolean: True/true or False/false. You will use comparisons in every decision your programs make.
| Meaning | Python | JavaScript |
|---|---|---|
| Equal to | == | === |
| Not equal to | != | !== |
| Greater than, less than | >, < | >, < |
| Greater or equal, less or equal | >=, <= | >=, <= |
const marks = 45;
console.log(marks >= 33);
console.log(marks === 100);
console.log(marks !== 100);
console.log("5" == 5);
console.log("5" === 5);true false true true false
JavaScript has two kinds of equality. The double == converts types before comparing, so the text "5" counts as equal to the number 5. The triple === checks that the type is the same too. Always use === and !== in JavaScript; the loose versions cause hard-to-find bugs. Python has only ==, which behaves like JavaScript's strict version.
Logical operators: and, or, not
To combine conditions, use logical operators. Python uses words; JavaScript uses symbols.
| Meaning | Python | JavaScript | True when... |
|---|---|---|---|
| Both | and | && | both sides are true |
| Either | or | || | at least one side is true |
| Opposite | not | ! | the value is false |
Suppose a school library lets a student in if they have an ID card, or if a teacher is with them. Separately, a student can borrow a book only if they have an ID card and fewer than 3 books already:
has_id_card = True
with_teacher = False
books_borrowed = 3
print("Can enter:", has_id_card or with_teacher)
print("Can borrow:", has_id_card and books_borrowed < 3)
print("Needs ID:", not has_id_card)Can enter: True Can borrow: False Needs ID: False
The student can enter (has an ID card), but cannot borrow (already has 3 books), so the and is false.
Shortcut operators
Updating a variable with its own value is so common that there are short forms. total += 50 means total = total + 50. The same works with -=, *= and /= in both languages. JavaScript also has count++ to add 1, which Python does not have; in Python write count += 1.
total = 0
total += 299 # total is now 299
total += 150 # total is now 449
total -= 50 # a ₹50 coupon, total is now 399Common mistakes
- Using
=to compare.if marks = 100is an error in Python. One=stores a value;==compares. - Using
==in JavaScript."0" == 0and"" == 0are bothtrue, which is rarely what you want. Use===. - Forgetting precedence.
a + b / 2is not the average ofaandb. Write(a + b) / 2. - Expecting
/to give a whole number. In both languages7 / 2is3.5. Use//in Python orMath.floor()in JavaScript for whole-number division.
Key points
- Arithmetic operators:
+ - * / % **, plus Python's//for whole-number division. %gives the remainder: useful for even/odd checks and last digits.- Multiplication and division happen before addition and subtraction; brackets come first.
- Comparisons give booleans; JavaScript should use
===and!==. - Combine conditions with
and,or,not(Python) or&&,||,!(JavaScript). x += 5is short forx = x + 5.
Exercise
A video is 500 seconds long. Write a program that prints its length as minutes and seconds, like 8 minutes 20 seconds. Then print whether the number of minutes is even. Write it in Python, and then in JavaScript using Math.floor().
Show solution
total_seconds = 500
minutes = total_seconds // 60
seconds = total_seconds % 60
print(minutes, "minutes", seconds, "seconds")
print("Minutes are even:", minutes % 2 == 0)8 minutes 20 seconds Minutes are even: True
const totalSeconds = 500;
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
console.log(minutes, "minutes", seconds, "seconds");
console.log("Minutes are even:", minutes % 2 === 0);8 minutes 20 seconds Minutes are even: true
Whole-number division gives the full minutes, and the remainder gives the seconds left over. A number is even when its remainder after dividing by 2 is 0.
Quick Quiz
1. What is 17 % 5?
% gives the remainder: 17 = 5 × 3 + 2.
2. What does 2 + 3 * 4 give?
Multiplication happens before addition, so it is 2 + 12.
3. Which JavaScript comparison is false?
=== also checks the type; a number and a string are different types.
4. With a = True and b = False, what is a and not b in Python?
not b is True, and True and True is True.
Learn Intro to Programming with a mentor
Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
