EthicsComputerCourses
Join training +91 90532 10052

Operators & Expressions

Intro to Programming · Lesson 11 of 25 Beginner 6 min read Updated

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".

OperationPythonJavaScriptExampleResult
Add++7 + 29
Subtract--7 - 25
Multiply**7 * 214
Divide//7 / 23.5
Whole-number divide//Math.floor(7 / 2)7 // 23
Remainder%%7 % 21
Power****7 ** 249

Here is a real use. Seventeen chocolates are shared equally among five children. How many does each child get, and how many are left?

PythonRun it yourself
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)
Output
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():

JavaScriptRun it yourself
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);
Output
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:

PythonRun it yourself
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)
Output
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.

MeaningPythonJavaScript
Equal to=====
Not equal to!=!==
Greater than, less than>, <>, <
Greater or equal, less or equal>=, <=>=, <=
JavaScriptRun it yourself
const marks = 45;
console.log(marks >= 33);
console.log(marks === 100);
console.log(marks !== 100);
console.log("5" == 5);
console.log("5" === 5);
Output
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.

MeaningPythonJavaScriptTrue when...
Bothand&&both sides are true
Eitheror||at least one side is true
Oppositenot!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:

PythonRun it yourself
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)
Output
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.

Python
total = 0
total += 299   # total is now 299
total += 150   # total is now 449
total -= 50    # a ₹50 coupon, total is now 399

Common mistakes

  • Using = to compare. if marks = 100 is an error in Python. One = stores a value; == compares.
  • Using == in JavaScript. "0" == 0 and "" == 0 are both true, which is rarely what you want. Use ===.
  • Forgetting precedence. a + b / 2 is not the average of a and b. Write (a + b) / 2.
  • Expecting / to give a whole number. In both languages 7 / 2 is 3.5. Use // in Python or Math.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 += 5 is short for x = 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
Python
total_seconds = 500
minutes = total_seconds // 60
seconds = total_seconds % 60
print(minutes, "minutes", seconds, "seconds")
print("Minutes are even:", minutes % 2 == 0)
Output
8 minutes 20 seconds
Minutes are even: True
JavaScript
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);
Output
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?

2. What does 2 + 3 * 4 give?

3. Which JavaScript comparison is false?

4. With a = True and b = False, what is a and not b in Python?

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