Operators Python: Comparison, Logical and Identity

In the last section we have seen arithmetic operators in Python. However we didn’t discuss Arithmetic Operators Python precedence.

Therefore that should be the first task that we should address.

First of all, what is arithmetic precedence?

When we have many operations at our hands, such as the following example.

a = 20
b = 10
x = (a + b) * a ** b - b/a 

What will be the value of the variable x?

Certainly, Python will make it happen according to the arithmetic precedence.

That means, it will execute the parenthesis part first. Next, it will do the exponentiation.

After that, it will do the multiplication job and division follows it.

Lastly, it will do the addition and subtraction accordingly.


If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.

But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


Therefore, to remember the rule we learn a word by heart – PEMDAS.

P stands for parenthesis, E for exponentiation, M for multiplication, D for division, A for addition, and finally S for subtraction.

However, there are more operators waiting to be discovered and discussed.

Firstly, we will get a look at the Comparison Operators. 

Let’s see the examples first.

first = 1323
second = 133

print(first > second)

print(first < second)

print(first == second)

print(first != second)

print(first >= second)

print(first <= second)

# output

True
False
False
True
True
False

As we see, the special symbols compare between two values. There are two variables, and we compare the values.

It either returns True or False according to the condition.

Now, we can give the output in a better way.

As a result it will clarify the comparison once we take a look at the values.

first = 1
second = 2


print(f'{first} > {second} is {first > second}')


print(f'{first} < {second} is {first < second}')


print(f'{first} == {second} is {first == second}')

print(f'{first} != {second} is {first != second}')


print(f'{first} >= {second} is {first >= second}')


print(f'{first} <= {second} is {first <= second}')

# output 

1 > 2 is False
1 < 2 is True
1 == 2 is False
1 != 2 is True
1 >= 2 is False
1 <= 2 is True

Now the output makes sense.

Secondly, we will discuss the Logical Operators.

As we progress while learning Python, we will find that logical operators play an important role.

Let’s first see the examples.

true = True
false = False

print(f'true and false is {true and false}')

print(f'true or false is {true or false}')

print(f'not true is {not true}')

# output

true and false is False
true or false is True
not true is False

Certainly, the output clearly tells us about the conditional statements.

What does it say?

It says, when a ‘true’ value adds itself with the ‘false’ value using the AND logical operator, the ‘true’ no longer remains ‘true’. 

It becomes ‘false’.

Remember this way – Good AND Bad is always Bad.

However, the OR logical operator makes a clear distinction between them.

True OR False is always True. 

Remember a Good friend OR a Bad friend?

Always a Good friend.

Finally the NOT logical operator negates any value.

Python Identity Operators have an interesting character. 

We use it to check whether two values are located on the same part of the memory.

However, the terms equal and identical are not the same. 

It confuses us sometimes.

first = 1
second = 2
third = 'John'
fourth = 'John'
fifth = [1,2,33, 68]
sixth = [1,2,3]


print(first is not second)

print(third is fourth)

print(fifth is sixth)

# output
True
True
False

We have not finished yet. There are more operators python that we will discuss in the next section.

So stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

C, C++, Java and Game Development

Twitter

Comments

Leave a Reply