## Operators

We were often asked by our teachers in younger classes what is the sum/difference of two numbers and as we grew up, we were asked logical concepts such as is 100 > 99? This is the foundation of programming; we manipulate all these logical expressions using operators, how operands relate to each other, and more complex concepts to create a program that runs the way we desire.

In Python (including other programming languages), we use operators (+, -, *, / etc.) and operands (numbers, strings {texts} and variables) to form an expression that we can manipulate to produce the desired result.

We could write a python code to achieve the following, let us assume a student is asked to write two tests with each test having a maximum score of 10 marks. Then we want python to total the scores and print the final score.

test_1_score = 9 test_2_score = 8 final_score = 9 + 8 print("The student scored: ", final_score)

Here we created 2 variables, ‘test_1_score’ and ‘test_2_score’, which would store the scores of test 1 and test 2 respectively. Then we created a variable called ‘final_score’ which would store the sum of the scores of the 2 stores, and finally, we then printed it out. Here in the expression “9 + 8”, the operator is the + sign and the operands are 9 & 8.

This was a very simple example of operators, (specifically arithmetic operators) in Python and as seen in the previous blog we can use all of the available arithmetic operators. Python has a range of operators, and they are arithmetic, assignment, bitwise, comparison, identity, logical, and membership operators.

## Relational and Logical Operators

Let’s dive deeper into Relational operators, these are operators that determine how operand(s) are related to one another. Conceptually these include operators such as is greater than, is less than, is the same as (or is equal to), is greater than or equal to, is lesser than or equal to, and is not equal to. When Python comes across such operators, it prints out one of two values, ‘True’ or ‘False’ (called Boolean values), and depending on this one value we modify the behavior of a program.

print(5 > 4)

If we were to run this single line of code, take a moment to guess what the output could be? Have you thought about it? We know that 5 is indeed greater than 4, so Python then prints the value ‘True’ onto the terminal. Let’s try another one.

print(101 < 102)

We know that 101 is less than 102 and that is a valid statement, but so does Python. As such Python will print onto the terminal ‘True’. How about we try an inaccurate statement?

print(1003 < 999)

Obviously, 999 is the smaller number, and so Python will print ‘False’ on the terminal.

print(1 == 5)

Let’s consider this, is the number ‘1’ equal to the number ‘5’? Certainly not, so Python will print the value ‘False’, but you must be wondering why are there two ‘=’ symbols? This is quite simple, in programming the convention to assign a value to a variable is the single ‘=’ symbol, and double ‘=’ symbols are used to ask Python if the first value is the same as the other.

We can combine operators to ask Python if one value is greater than or equal to another?

print(11 >= 10)

We are asking Python if 11 is greater than 10 or is 11 the same as 10, and if any of these comparisons are valid then Python will return ‘True’ but if both are invalid then Python will return ‘False’. Thus here, the output would print ‘True’.

Let’s now look at ‘not’ operations.

print(5 != 5)

Here we are asking Python if the number ‘5’ is not the same as the number ‘5’? Indisputably it is, and Python will print the value ‘False’. We use the ‘!’ symbol to represent ‘not’ in arithmetic operators.

Take a look at the following lines of code, guess what would be the output, and then execute it to see if you got it right.

print(5.65 > 5.59) print("Bruce" == "Bruce_") print(7.0 != 7)

Now let us jump into Logical operators. Here the operands we consider are Boolean values, and there are 3 types of these operators:

- and
- Only if both the operands are True, it returns True.
- If one or both of the operands are False, it returns False.

x = 21 y = 25 if x > 15 and y <= 25: print("Both are True")

- or
- Only if both the operands are False, it returns False.
- If one or both of the operands are True, it returns True.

x = 21 y = 25

- not
- Negates our operand.

if x == "John" or y == 25: print("Only one is True")

z = False if not z: print("z is False"

Tip:

- If you are confused about the ‘and’ & ‘or’ operator, remember ‘and’ as multiplication and ‘or’ as addition.
- Remember ‘True’ as 1 and ‘False’ as 0, so “True and False” would be “1*0” which is “0” and thus the result would be “False”.
- “True or False”, would be “1+0” which is “1” and thus the result would be “True”.
- Follow the Binary notation, in which 0+0 is 0 and 1+1 is 1, so “False or False” is “False” and “True or True” is “True”.

We can combine Relational and Logical operators as shown below.

print(not (51 > 55)) # True

### What have we learned?

- What are operators?
- What are the different types of operators?
- What are the different types of relational operators?
- What is the difference between ‘=’ and ‘==’ in programming?
- What are the different types of logical operators?
- How we can combine different operators?