Operators
Have you ever heard the phrase,
"The whole is greater than the sum of the parts"? In this
lesson on operators we will put this phrase into practice. Although the term
operators might initially sound complex, there are some operators
that are already familiar concepts, such as +
for addition and
-
for subtraction. By the end of this lesson, we'll see how these
simple elements combine in powerful ways, creating a synergy where their
collective use and interaction yield results that transcend their
individual capabilities.
An operator is a symbol that indicates a particular operation, function, or transformation that needs to be carried out. Essentially, a concise and intuitive cue for the interpreter to execute mathematical, logical, or relational operations.
In this lesson we will learn the operators + (plus)
,
- (subtract)
, * (multiply)
,
/ (divide)
, ** (power)
,
// (integer divide)
, % (modulo)
,
and += (addition assignment)
. We will
cover additional operators and see further uses of these in the remainder
of this section.
The plus +
, subtract -
,
multiply *
, and divide /
operators
Think of addition +
, subtraction -
,
multiplication *
, and division /
as
familiar friends from the world of programming. They work just like
the math operations we've known since our school days in algebra.
There are no tricky surprises here. Welcome to the fun world of programming,
where math becomes a helpful tool on our journey to creating
amazing things!
The addition and multiplication operators can also be used with strings. The addition operator concatenates two strings together, while the multiplication operator repeats a string a given number of times.
The power **
, and integer divide
//
operators
The power operator, **
, is used to raise a number to the
power of another, while the integer division operator, //
,
performs division but rounds down to the nearest integer, behaving
similarly to the math.floor
function we learned in the
Datatypes - Integers and Floats lesson.
The modulo %
operator
Meet the modulo operator %
- it might seem a bit tricky at
first, but it can be super helpful in just the right situations! Imagine
we're dividing two numbers, but instead of wanting to know how many
times one fits into the other, we're curious about what's left over.
That's where the modulo operator comes in. It divides the number on
the left by the number on the right, and then gives us back just
the remainder of that division. Don't worry if this sounds a bit abstract
now; it'll make a lot more sense once we see it in action in a
loops lesson later.
The addition assignment +=
The final operator we will look at in this lesson is more of a shorthand
operator. This is the addition assignment operator +=
.
As the name implies, this operator does addition and it does assignment!
For example The line i += 1
is equivalent to i = i +
1
. This shorthand can also done with any operator that returns a numeric such as:
multiplication -=
, and integer division /=
.
In the next lesson, we will see how some of these operators may be applied to strings.