Menu Close

Operators (and their uses)

In the world of computer science, operators are symbols that represent and perform a specific computational command. There are different operator types, and they each perform a different action. The object that is being acted upon is called an operand. The different operator types include arithmetic operators, relational operators, assignment operators, member operators, and logical operators.

Arithmetic Operators

Computer programming requires lots of mathematics. Arithmetic operators help perform basic arithmetic calculations. Below is a table of arithmetic operators and their meanings.

Operator Meaning Usage
+
Addition
x = 3 + 4 #x = 7
-
Subtraction
x = 4 – 5 #x = -1
*
Multiplication
x = 6 * 7 #x = 42
/
Division
x = 5 / 2 #x = 2.5
%
Modulo*
x = 20 % 6 #x = 2
**
Exponent
x = 3 ** 3 #x = 27

 

*Modulo returns the remainder of the given operation. For example, 20 divided by 6 is 3 remainder 2. Modulo will only return the remainder.

Relational Operators

Often in conditionals, programmers use relational operators to test whether a given operation should be performed. For this, relational operators help test whether a given statement is true or not.

Operator Meaning Usage
==
Is equal to*
while x == y:
is
Is equal to*
if x is y:
!=
Not equal to
if x != y:
>
Greater than
if 4 > 3: #True
>=
Greater than or equal to
if 2 >= 5 #False
<
Less than
if 2 < 1 #False
<=
Less than or equal to
If 5 <= 5 #True
in
If item inside list
if x in list:

*Both the keyword “is” and the operator == both test for equality, however there are key differences between the two.

The == operator tests whether the values of two operands are the same, whereas the “is” keyword checks whether the values of the two operands share the same location in memory.

Consider the following segment of code:

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

if x == y:
     print(True)
else:
     print(False)

Output :

True
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

if x is y:
     print(True)
else:
     print(False)

Output :

False

As observed in the code segment above, the first segment of code works as the list contains the exact same values. However, in the second segment of code, the conditional fails as x and y are two separate objects which occupy different locations in memory.

Assignment operators

The assignment operator sets the value for an operand. In python, the simple assignment operator both sets the value for a variable and declares it too. Except for the simple assignment operator, all other assignment operators are called compound assignment operators.

Operator Usage Meaning
=
x = 5
Simple assignment
+=
x += 3
x = x + 3
-=
x -= 2
x = x – 2
%=
x %= 5
x = x % 5
/=
x /= 32
x = x/32
*=
x *= 5
x = x * 5
**=
x **= 5
x = x ** 5

 

Logical Operators

Often, the basic conditionals are not enough to fulfill the needs of a given task. Hence, we use logical operators to check more complex conditions.

Operator Meaning Usage
or
If either condition is true
if x < 0 or x > 3:
^
Exclusive or (xOR)*
If x < 5 ^ x != 5
and
If both conditions are true
if x < 3 and x > 0:
not
Not
if x not == 3:

 

*The exclusive or works only when the two operands are different. Even if both operands are True, it will return false.

Operator Precedence:

Given the following piece of code, predict the output.

if True or False and False:
     print(True)

else:
     print(False)

when examining the order, it seems logical to assume that the output would be False, as the order shows that it would eventually evaluate “True and False”, which would return false.

However, this is not the case, as there is an order as to which operators and keywords get evaluated first. This is known as operator precedence.

Below is the precedence of operators, from highest to lowest:

  1. (), not
  2. *, /, %, **
  3. +, –
  4. >, >=, <, <=, ==, !=
  5. and
  6. or, ^
  7. =, +=, -=, *=, **=, /=, %=

During runtime, the code will calculate every line in this order. However, if you wanted to do one operation before another, you can use brackets much like how they would be used in mathematics.

Operator precedence practice questions:

Determine the output for the following segments of code

1.

x = 5
if x < 3 ^ x == 5:
     print(True)
else:
     print(False)

2.

x = 2 + 5 * 2 * (5 – 2)
print(x)

3.

x = 5

if x >= 5 and not x == 7:
     print(True)
else:
     print(False)
Posted in Articles, Embedded Programming Language, Python

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!