Intro to Operators
Files associated with this lesson:
Intro to Operators.ipynb
Intro to Operators¶
We could say that an operator is like a function, packaged in a single (or a few) character(s). Exaple, this is the "addition" operator (an example of an arithmetic operator):
2 + 3
An operator could be written like a function; let's imagine for a second that python doesn't have operators. All these operations should be done with functions instead. For our previous example we'd have something like:
add(2, 3)
>>> 5
This, obviously doesn't work because the add
function doesn't exist. But you can see how an operator closely resembles a function. They have the same purpose.
Operator arity¶
Operators will require a given number of "operands" to be able to work ("operate"). For example, the +
operator requires two elements. This "number of required objects" will determine the arity of the operator.
For example, as previously seen, the +
operator is a binary (arity = 2) operator:
# Binary operators
print(2 + 3)
print(5 + 5)
print(2 * 3)
Some operators will also be unary (un-ary, arity = 1). For example, the -
operator:
x = 3
-x
In that case, -
is applied to only one operand (x
). But it can also be used as a binary operator:
print(5 - 2)
print(10 - 8)
print(2 - 9)
We'll divide operators in 5 different catergories:
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Other Boolean Operators
- Logical Operators
These categories are arbirary, it's the way to divide them that results in the best experience. You're free to "rearrange" them in your mind as you see fit.
There's one lesson per each operator category, along with a few assignments to practice them.