Introduction to Operators

code guest
Python Journey
Published in
4 min readAug 14, 2019

What are operators

Besides working with data types like lists and strings, we also use a lot of operators in our Python code. Operators are the technical term for special symbols and keywords that act on data values.

Operators are the tools with which we can use Python to work with data values. There are a lot of operations that Python operators can help us with — here are some examples (not exhaustive):

  • If we want to perform mathematical operations, we can use use arithmetic operators like +, -, * etc
  • If we want to compare between data values, we can use relational operators like ==, !=, <, > etc
  • If we want to perform logical operations (like logic gates), we can use logical operators like and, not, or etc
  • If we want to assign some data value to a variable, we can use the assignment operator which is =

Mini-quiz: Can you spot the special symbols and keywords in the code snippets below?

  • “abc”[1]
  • True and False
  • “a” in “abc”

The special symbols are the square brackets in “abc”[1]; keywords are and as well as in.

Notice that the operators are acting on data values:

  • the square brackets act on string “abc”
  • and acts on boolean values True and False
  • in acts on string “a” and string “abc”

There are a number of special symbols and keywords in Python but not all of them are operators because operators specifically act on data values(see this stackoverflow post for details on keywords in Python).

Operators provide us with lots of functionalities (credits: undraw.co)

Understanding operators

To understand operators, we will start with some technical terms related to operators which you will encounter in your Python journey.

Operands

The data value or data values that an operator acts on are called operands.

  • In 101 + 299, 101 and 299 are operands
  • In 3 < 5, 3 and 5 are operands
  • In (1+2) / (5+1), there are seven operators — brackets, plus symbols, slash symbol and four operands — two 1s, 2 and 5.

Although the examples above all show operators taking two operands, there are operators that can take one operand only too.

  • In 10++, there is only one operand which is 10
  • In “abc”[0], there is only one operand which is “abc”

Operators and operand(s) can evaluate to a data value

When we see operators and the operand or operands that they act on, we are typically interested in the end result.

  • Take mathematical operations like 101 + 299 or (1+2) / (5+1) for example, we want the code to evaluate the operands & operators to give us the end result of 400 and 0.5 respectively.
  • In comparison operations like 3 < 5, we want to know the end result which is True
  • In 10++, we want to increment 10 by one and obtain the end result of 11
  • In “abc”[0], we want to retrieve the character at index 0 which is “a”

Notice that whether an operator is working with one or two operands, the operator-operand(s) evaluate to a data value which is the end result we are looking for.

However, not all operator-operands(s) evaluate to a data value —such as in the case of assigning some data value to a variable.

Operator-operands typically evaluate to a data value — but there are exceptions like assignment operation (credits: undraw.co)

In subsequent articles, we will take an in-depth look at the different varieties of operators and their operations.

References

Summary

  • Operators refer to special symbols and keywords that act on data values.
  • Data values acted upon by operators are called operands
  • Operators and operands can evaluate to a data value

Quiz

Which of the following options are correct?

a) The only operators in Python are +, -, *, /, <,>, and, or, not, =

b) Operators can exist on their own in Python (ie. they are stand-alone)

c) The list [3+5, 7–2] is equivalent to [8, 5]

d) Operators are not a data type

Quiz explanation

a) There are quite a number of operators other than those listed in option (a) such as in, [], () and more.

b) Operators cannot exist on their own — they act on operands.

c) Since operator-operands evaluate to a data value, Python sees 3+5 as 8 and 7–2 as 5.

d) Operators don’t contain data on their own

Quiz answers: c & d

--

--