Python Series Part 3: Python Operators

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
387
Credits
11,920
In the previous article, Python Basics, we hinted at arithmetic operations. These can include basic math functions, comparative features, and others.

We can go over these operators to help understand these programming abilities.

Arithmetic Operations

As with other programming languages, we can perform basic math functions. Such as:

Addition +

Subtraction -

Multiplication *

Division /

Exponent **

Modulus %


Keep in mind that the values we are performing these operations on can be a variable or an actual number. For example, if we wanted to add 2 and 3, we could use the numbers and print out the answer:

Code:
print (2+3)

If we wanted to use variables, we can do:

Code:
a=2
b=3
print (a+b)

It is possible to mix numbers and variables:

Code:
a=2
print (a+3)

These are fairly straightforward, but let's look at the last two arithmetic operators.

Exponents is when a number has a power. For example, 2 to the third power, 2**3, is two times two times two, or eight.

Modulus is the remainder of a division operation. For example:

Code:
a=10
b=3
print (a/b)
print (a%b)

The first output is:

3.3333333333333335
1


The first line of output is 10 divided by 3. The second line is the remainder of 10 divided by 3. If we divide 10 by 3, we get 3 with a remainder of 1, so the answer is 1.

Arithmetic Assignments

In a lot of cases, you may perform calculations on a variable. Instead of storing the new value in a new variable, you can use the same variable. The difference is that you can use a shorthand way of performing the calculation called an assignment.

So, if we have a game that we are tracking the number of levels that a player reaches, we could do:

Code:
level = (level +1)

The code can be run every time the player levels up.

The benefit of placing the value back into the existing variable and not a new one is so you don't have a lot of variables to track, but also every variable requires more space in memory.

If we were to use an assignment to increase the level variable, we would use:

Code:
level += 1

The line basically means to take the value in the variable 'level' and increase it by one.

There are assignments for all operations:

Code:
variable += 5 Add 5 to the value in variable
variable -= 3 Subtract 3 from the value in variable
variable *= 2 Multiply the value in variable by 5
variable /= 4 Divide the value in variable by 4
variable **= 2 Put the value in variable to the power of 2
variable %= 5 Place the remainder of value in variable divided by 5

This can reduce the amount of typing for a line when performing such an operation.

Comparison Operators

Sometimes we need to compare values or variables. These will be used more in 'if' statements, but we can show how this works now.

When we compare two values, we will get a result of 'True' or 'False'.

We have a few comparisons we can make:

  • == is equal to
  • != is not equal to
  • > greater than
  • >= greater than or equal to
  • < less than
  • <= less than or equal to

So, I can test this with the command:

Code:
print (10==20)

The answer would be 'False'. The statement is testing whether '10' is equal to '20', which it is not.
Now, I were to change it to:

Code:
print (10 != 20)

The answer is True since '10' does not equal '20'.

The rest are simple comparative operations.

Logical Operations

Sometimes we need to compare two or more conditions to determine if something must happen.

Let's assume two conditions. One condition for an example game is that the player must be level 10 or higher. The second condition is that the player must have over '100,000' points. If both conditions are met, then the player wins. The code may be similar to:

Code:
((level >=10) and (points >= 100000))

Notice that each condition is inside its own parenthesis. The whole statement, including the 'and', is enclosed in parenthesis.

If one of the conditions is not met, then the statement is 'False'. The only way for it to be 'True' is that both conditions are met.

We can also have two conditions that when either is met, just one or more, the statement is 'True'. Let's say that a player can win if they reach level 10 or get 100,000 points. The statement can also be 'True' if bot conditions are met. We should try an example:

Code:
level=9
points=100001
print ((level >=10) or (points>=100000))

The result should be 'True'. Since the points are over '100,000', one condition is true so the whole statement is true.

Of course you can have more conditions than two to use the 'and' or the 'or' operator.

It is possible to combine 'and' with 'or' conditions.

We could assume that in a game, if a special award is granted to a player, they win. Our other stipulations still apply. The code would be:

Code:
(((level>=10) and (points>=100000)) or (award==1))

If the following is the values of the variables:

Code:
level=9
points=100000
award=0

Then the first condition, level>=10, is false. The second condition, points>=100000, is true. The last condition, award==1, is false.

The conditions are then:

Code:
(((false) and (true)) or (false))

The 'and' portion is broken down as ((false) and (true)), which is then 'false'. To be 'true', both conditions must be true.

Now, we have the rest to handle, which is ((false) or (false)). For an 'or' operation, only one condition needs to be 'true' which they aren't. So, the whole statement in 'false'.

Let's try something a little different. What if we tried the following coding:

Code:
level=9
points=100000
award=0
print (level>=10 and points>=100000 or award==1)

What happens now?

The line will be processed the same as before. The reason is that 'and' conditions are tested before 'or' conditions. The way the parenthesis were used, made the 'and' condition tested first.

Be aware how you place the parenthesis.

The last logical operation is 'not'. Basically, if all of the '>' and '<' were backwards, then we would want the opposite to be found. So, what once was 'False' is now 'True' and what was 'True' is now 'False'. The statement would be:

Code:
print (not(((level>=10) and (points>=100000)) or (award==1)))

What was 'False' is now reported as 'True'.

Conclusion

The various operations can be very important in any programming language.

Be sure you understand these before continuing. These can be very important to know depending on the project you are writing.
 
Last edited:

Members online


Latest posts

Top