Operators

Let's talk about operators. An operator is something between 2 variables / values and does something to them.

For example, the addition operator:

x = 3 + 1
# x = 4

Python supports many maths operators:

3 + 1
3 / 1 # divided by
3 * 4 # times
2 ** 2 # 2 to the power of 4
2 % 3 # 2 mod 3

Now the cool thing, operators don't just work on numbers. They work on strings too. And lists. And dictionaries.

[3, 2] + [6, 7]
# [3, 2, 6, 7]
"Hello, " + "World!"
# "Hello, World!"

Well, operators work on any data structure that has implemented a magic method for them.

Magic methods are rather wild, but require prequesite knolwedge of object orientated programming fiest.

Last updated