OPERATORS

Photo of author

By Jackson Taylor

Operators are the symbols that tell a programming language to do something with values: add two numbers, compare them, combine conditions or store a result. Nearly every program uses them, so understanding them is one of the first steps in learning to code.

This article explains what operators are, goes through the main types, and walks through a short example that uses the common arithmetic operators. The syntax shown is Python-style. Other languages use very similar symbols, though details differ.

What is an operator?

An operator is a symbol or keyword that performs an operation on one or more values. The values it works on are called operands. In the expression 3 + 2, the plus sign is the operator and 3 and 2 are the operands.

Operators are described by how many operands they take:

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  • Unary operators take one operand, such as the minus sign in -5.
  • Binary operators take two operands, such as a + b.
  • Ternary operators take three. Many languages have a conditional form that picks one of two values based on a test.

A simple example program

The program below stores two numbers in variables and then applies each of the basic arithmetic operators to them. It is a good way to see what each symbol does.

a=2;
print a;
b=3;
print b;
print b+a;
print b-a;
print b*a;
print b/a;
print a**4; #exponential
print a%2;   #modulus

The code is written in an older, loose style. In current Python 3, print needs parentheses and the semicolons are not required, so the same program looks like this:

a = 2
print(a)
b = 3
print(b)
print(b + a)
print(b - a)
print(b * a)
print(b / a)
print(a ** 4)   # exponent
print(a % 2)    # modulus

Running the Python 3 version prints these values, one per line: 2, 3, 5, 1, 6, 1.5, 16 and 0.

Here is a short video that goes through the same program:

Arithmetic operators

Arithmetic operators handle basic maths. These are the ones used in the example above:

  • + adds two values. b + a gives 5.
  • – subtracts the right value from the left. b – a gives 1.
  • * multiplies. b * a gives 6.
  • / divides. b / a gives 1.5 in Python 3.
  • ** raises a number to a power. a ** 4 means 2 to the power of 4, which is 16.
  • % is the modulus operator. It returns the remainder after division, so a % 2 is 0 because 2 divides evenly by 2.
See also
H-BRIDGE USING RELAY SIMULATION

One thing to watch is division. In Python 3, / always returns a decimal result. In some other languages, dividing two integers drops the fractional part, so 3 / 2 would give 1. Python also has // for floor division, which rounds the result down to a whole number.

What the modulus operator is good for

Modulus looks odd at first, but it is very practical. A few common uses:

  • Checking whether a number is even or odd: n % 2 is 0 for even numbers.
  • Running something every Nth time in a loop, such as every 10th item.
  • Wrapping a value around, for example cycling through a list of fixed length.

Assignment operators

The equals sign = assigns a value to a variable. In the example, a = 2 stores the number 2 in a. It does not mean "equal to" in the mathematical sense.

Most languages also offer shortcut forms that combine an operation with assignment:

  • x += 1 is the same as x = x + 1.
  • x -= 1 subtracts 1 from x.
  • x *= 2 doubles x.

These shortcuts make code shorter and often easier to read, especially for counters.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Comparison operators

Comparison operators compare two values and return a true or false result. They are the basis of most decisions in a program.

  • == checks whether two values are equal.
  • != checks whether they are different.
  • > and < check greater than and less than.
  • >= and <= include the case where the values are equal.

A very common beginner mistake is using a single = where a double == is needed. The first assigns a value, the second compares.

Logical operators

Logical operators combine true or false conditions. In Python they are written as words; many other languages use symbols.

  • and is true only when both conditions are true (&& in many languages).
  • or is true when at least one condition is true (||).
  • not reverses a condition (!).

For example, age >= 18 and has_ticket is true only when both parts are true.

Other common operators

Depending on the language, you will also meet these:

  • Membership operators, such as Python's in, which checks whether a value is inside a list or string.
  • Identity operators, such as Python's is, which checks whether two names refer to the same object.
  • Bitwise operators, such as &, | and ^, which work on the individual bits of integers. They are used in low-level code, flags and some performance-sensitive tasks.
See also
ADVANCED TRAFFIC CONTROL AND RESCUE SYSTEM

Operator precedence

When an expression has several operators, the language follows a fixed order, known as precedence. Multiplication, division and modulus are evaluated before addition and subtraction, just as in school maths. Exponentiation comes before those.

So 2 + 3 * 4 gives 14, not 20. If you want the addition first, use parentheses: (2 + 3) * 4 gives 20. Even when you know the rules, parentheses make complicated expressions easier for other people to read.

Tips for using operators well

  • Use parentheses to make the intended order clear.
  • Check how your language handles integer division before relying on the result.
  • Keep each expression short. Split a long one into several lines with named variables.
  • Test small examples, like the program above, in an interactive interpreter to confirm what an operator does.

Frequently asked questions

What is the difference between = and ==?

A single = assigns a value to a variable. A double == compares two values and returns true or false.

What does the % operator do?

It returns the remainder of a division. For example, 7 % 3 is 1, because 3 goes into 7 twice with 1 left over.

How do I calculate a power in Python?

Use the ** operator. For example, 2 ** 4 returns 16.

Do all programming languages use the same operators?

Most share the basic arithmetic and comparison symbols, but there are differences. Logical operators, integer division and the exponent symbol vary between languages, so check the documentation for the one you use.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works