For this post, we will go over how to utilize basic operators in Python.

Arithmetic Operators

Just as you may have expected with this operator: math. Like any other programming language out there, we can do some math (adding, subtracting, multiplying, and dividing) with the operators.

Here is an example:

x = 1 + 2 * 3 / 4.0
print(x) 

Before running the code above, consider the following: One thing you may be wondering, what order does Python follow to execute the operations? Does it go from left to right? Does it follow PEMDAS rules? Does it go backwards? Does it back whatever number it wants randomly?

What it does is that it targets the multiplication and division operators first, and then the addition and subtraction. If it went left to right, we would have had a different outcome (2.25) rather than the correct output (2.5).

As you may have seen before, there is another interesting operator called the modulo operator. It is represented with a “%” character, and it returns the integer that is the remainder of a division.

Note: dividend % divisor = remainder

x = 420 % 69
print(x) # 6

How do you represent a power relationship? You simply use two multiplication symbols.

squared = 2 ** 2
cubed = 4 ** 3
quartic = 3 ** 4
quintic = 5 ** 5
sextic = 7 ** 6
heptic = 9 ** 7
octic = 8 ** 8

print(squared) # 4
print(cubed) # 64
print(quartic) # 81
print(quintic) # 3125
print(sextic) # 117649
print(heptic) # 4782969
print(octic) # 16777216

Before we continue, please make sure you understand the arithmetic operators and what they do:

Arithmetic Operators:

OperatorDescriptionExample
+Add two operands or unary plusx + y
Subtract right operand from the left or unary minusx – y
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division – division that results into whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of rightx**y (x to the power y)

Using Operators via Strings

Python allows for concatenating strings by utilizing the addition operator. Concatenating is like another word for putting together or combining. An example of this would go as followed:

x = "I am big!" + " " + "It's the pictures that got small!"
print(x) # prints 'I am big! It's the pictures that got small!'

We can also spice things up a bit by multiplying strings to create a repeating sequence:

x = "Nom" * 10
print(x) # NomNomNomNomNomNomNomNomNomNom

Using Operators via Lists

After going over lists in the previous guide, we should have some awareness of how to join lists together via addition operators.

For example:

x = [1,2,3,4,5]
y = [6,7,8,9,10]
z = x + y
print(z) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Similar to how we did a repeating sequence with strings, we can do this with lists too by utilizing the multiplication operator.

print([5,6,7,8] * 2) # [5, 6, 7, 8, 5, 6, 7, 8]

Exercise

Lets try to play with the operators a bit. Our goal, with the exercise, is to generate two lists called “x” and “y,” they both will have “0” (for “x”) and “1” (for “y”): you want to have a dozen of each instances for the variables. Also, we are going to generate a list called “super_list,” this will feature the variables of “x” and “y,” a dozen times each, by concatenating the two lists that were generated. Give the exercise, refer to the solution if you get stuck.

Exercise

x = object()
y = object()

# TODO: tweak this code
x = [0]
y = [1]
super_list = []

print("x contains %d objects" % len(x))
print("y contains %d objects" % len(y))
print("super_list contains %d objects" % len(super_list))

# testing code
if x.count(0) == 12 and y.count(1) == 12:
    print("Almost there...")
if super_list.count(0) == 12 and super_list.count(1) == 12:
    print("Great!")

Solution

x = object()
y = object()

# TODO: change this code
x = [0] * 12
y = [1] * 12
super_list = x + y

print("x contains %d objects" % len(x))
print("y contains %d objects" % len(y))
print("super_list contains %d objects" % len(super_list))

# testing code
if x.count(0) == 12 and y.count(1) == 12:
    print("Almost there...")
if super_list.count(0) == 12 and super_list.count(1) == 12:
    print("Great!")

Output

<script.py> output:
    x contains 12 objects
    y contains 12 objects
    super_list contains 24 objects
    Almost there...
    Great!

For more information

If you seek to learn more about Python, please visit Python’s official documentation.