Python Operators. Managing variables and their values

In Python, operators are symbols that perform operations on variables and their values.

Let’s see a few examples.

Begin by initiating a REPL.

In Window you type py and press enter. In Mac and Linux you would type python3.

The Python REPL is now running.

Typing py in Windows start a REPL running.
(click on the image to open in a new tab)

The assignment operator

We have seen examples of assignment operators in previous posts.

The assignment operator is just the equal sign =.

I typed name equals and after I did that I assigned the value ‘Lambros‘ to the name variable.

Now I can get that value back by typing that variable in the REPL.

Assigning a value to a variable i s done using the equals sign.
(click on the image to open in a new tab)

Arithmetic operators

Arithmetic operators are known from math.

First we have the addition.

We can make additions in Python.
(click on the image to open in a new tab)

So we can use Python just like a calculator. We add two numbers together.

We can also do subtraction.

A typical subtraction.
(click on the image to open in a new tab)

And also multiplication.

We can also hold multiplications.
(click on the image to open in a new tab)

And lastly division.

Division operator works like in Math.
(click on the image to open in a new tab)

In Python there is a second type of division. The floor division. This type of division, contrary to the typical one that can return a decimal number, returns a rounded down number.

The Floor Division uses two slashes as an operator.

The floor division rounds down a decimal.
(click on the image to open in a new tab)

If we want to round a division up, we can use the round function.

We can round a division up with the round function.
(click on the image to open in a new tab)

We see that the round function returns different results than the floor division operator.

But what if we want the remainder of this division?

We can use the percentage sigh %. We call it the remainder operator and it returns exactly that. The remainder of a division.

Example of the remainder operator.
(click on the image to open in a new tab)

We can also use exponents. If want to calculate three to the power of two we can do that by using two asterisks.

Example of using an exponent.
(click on the image to open in a new tab)

Combining Assignment operator with arithmetic operators

We can combine assignment and arithmetic operators when using variables.

For example we can combine the assignment operator with the plus sign.

Example of combining the equal operator with the plus operator.
(click on the image to open in a new tab)

This is the equivalent of

number = number + 1

Likewise we can take the “number” variable and use the combination of the assignment operator with the minus operator or any other arithmetic operator for that matter.

number -= 3 

Concatenation

We can use the plus operator for concatenating two different strings.

Example of concatenating two strings with the plus operator.
(click on the image to open in a new tab)

We add two different strings together and one string is returned that has concatenated the two strings.

Comparison Operators

If we want to check if one number is equal to another we use the double equals operator.

Example of double equals operator.
(click on the image to open in a new tab)

Since the two numbers are obviously not equal the response is False.

If the two numbers were equals it would be True.

Since the double equal operator can return only False or True is called a Boolean operator.

Another Boolean operator is the Not-Equal operator !=.

If we want to check say 3 != 3 it will return False.

If we check 3 != 4 it will return True.

Other Boolean operators are the ‘Greater Than> and ‘Greater Less< operators.

Examples of the Greater Than and Greater Less operators.
(click on the image to open in a new tab)

There are also the ‘Greater Than and Equal‘ and ‘Greater Less and Equal‘ operators.

Examples of the Greater Than and Equal and Greter Less and Equal operators.
(click on the image to open in a new tab)

Although my blog doesn’t support comments, feel free to reply via email or X.