Operators in python

Share:

In this topic we will discuss about operators and kinds of that and  root of operators that means we  will find out where they come from.
First of all, operators are divided into two parts which the first one is comparative operators and the second one is relational operators that each type have two possible answers true or false that means zero and one for computer which zero means false and one means true which we have a data type for that in python which is Boolean, note that which operators idea and its statements comes from logical Algebra, there we call them expression. now we will learn it by details.


1: Comparative operators
Comparative operators are those that we use them to compare two values by the use of them, we have six comparative operators in Python which are listed in below:

   1: less than or equal to         (<=)
   2: more than or equal to       (>=)
   3: equality                            (==)
   4: not equal                           (!=)
   5: greater than                       (>)
   6: smaller than                      (<)

As I mentioned before all of this operators returns true (1) or false (0) for us in python, we usually use comparative operators to check for a condition which we will discuss it in the next topic, now I will write some examples of comparative operators:

      1==1   it is true (1)                                 2==1   it is false (0)
      1! =1    it is false (0)                               2! =1    it is true (1)
      1<2     it is true (1)                                  2<1     it is false (0)
      2>=1   it is true (1)                                  1>=2   it is false (0)
      2<=1   it is false (0)                                 1<=2   it is true (1)

It is all the examples for comparative operators in python programming language it was easy isn’t it? OK now we know enough about comparative operators let’s move on relational operators.

2: relational operators
Relational operators are those that we use them to relate two or more than two comparative operators and we use them when we want to check more than one condition, we have three kind of relational operators that are in below:

   1: and
   2: or
   3: not

And
We use this relational operator to check every condition if all of them was true only then the condition will be true and compiler will run that for us. I will give you an example of that.

      1<2 and 4>3

In here the result will be true because the both condition is true, the result will be false if one of them or both of them be false. Like

      1<2 and 0==1

Or
This relational operator is used to relate two or more than two comparative operators and the result will be true when both or at least one of the conditions are true or in other words if we have even one true in our conditions the result will be true. Look the below example I am sure that you will know that:

      1==0 or 2>1

Above example is true because the second condition is true, now let’s see a false condition which as you guess it is all false condition:

      1==0 or 3<2

Not
Not is very simple to explain it just make the result opposite, it means that it will change true to false and false to true at the end. Like:

      not 1==1

Yes, the answer is true. it always comes before a condition to opposite it’s result.
Now in definition of relational operators we said that “Relational operators are those that we use them to relate two or more than two comparative operators” so in this case we can relate multi comparative operators not only in ‘and’ but we can also do it with ‘or’ and ‘not’ or even we can make more complex conditions, like below examples:

      (2<3) and (0!=1) or not (5==3) and not(6==6)

The result is true in here. now I will give you a question tell me in comment it’s true or false:

      (2<3) and (0==1) or not (5==3) and not(6==6)

No comments