logo codesdope

Operators in Python


Before stepping into more programming, let's study some basic stuff but of great importance; 'Boolean'.
Just as an integer can take a value of -1, 1, 0, etc
         and float can take 0.01, 1.2, etc
        Boolean is something which can either be true or false
.

print type(True)
print type(False)
Output
<type 'bool'>
<type 'bool'>

Type of 'True' and 'False' is bool (bool is for Boolean). And we can also assign any variable 'True' or 'False'. See the example given below.

x = True
y = False
print x
print y
Output
True
False

Now, let's do something more. We have already done x = 10 and we know that it means x is 10.
Try this:

x = 10
print x == 10
Output
True

'==' operator is used to compare. It is equivalent of asking whether x is 10 or not?
Do this one more time.

x = 10
y = 10
print x == y
Output
True

Here it checks if x and y are equal.

name = "Sam"
print name == "Sam"
print name == "Aam"
Output
True
Fasle

Here, = assigns a value "Sam" to name and == checks whether name is "Sam" or not.

== in python

AND and OR


See this chart first.

Exp1 Operator Exp2 Boolean
True and True True
True and False False
False and False False
True or True True
True or False True
False or False False

So, if we use and with any two operands and if both of them are True, then the result is True. Otherwise, it is False
And if we are using or and if any of the two operands is True, then it is True and it will be False if both the operands are False.

'and' can be understood as both ( first and second both )
'or' can be understood as either ( first or second any ). See the next line to understand it more clearly.

'and' and 'or' of programming are very much similar to English words 'and' and 'or'.
In English,
A and B - Both A and B.
A or B - Either A or B.
In programming,
A and B - Both A and B.
A or B - Either A or B or both.
So, if you are writing A and B, then the expression is true if both A and B are true. Whereas, if you are writing A or B, then the expression is true if either A or B or both are true.

True or False -> As 'or' is used, either of the two is true -> True
True and False -> As 'and' is used, both are not true -> False

Try to understand this or just remember the chart.

x = 10
y = 20
print x == 10 and y == 20
print x == 3 or y == 20
Output
True
True

Here, x is 10 and y is 20. So, x==10 is True and also y == 20 is True. So, x == 10 and y == 20 is also True as both operands are true ( and is used ).

In the next line, x == 3 is False but y == 20 is True. So, x == 3 or y == 20 is True because atleast one operand is True ( or is used ).

not


not -> You can understand 'not' by thinking that it will do the opposite.
not False is True
not True is False
Yeah! It is simple.
E.g.-
not (True or False) -> False
How?
( True or False ) is True because or is used and at least one of the operands is True. Then ( not (True) ) is False.

x = 10
y = 20
print not(x == 10 and y == 20)
print not(x == 3 or y == 20)
Output
False
False

This is the same example as the previous one. We just used not here and see the answers got reversed. Earlier, those were True but now those are False.

Some operators


First look at the following table:

Operator Description Example
!= Not equal to (5 != 2) is True, (5 != 5) is False
> Greater than (5 > 5) is False
< Less than (5 < 5) is False
>= Greater than or equal to (2 >= 2) is True
<= Less than or equal to (5 <= 2) is false

!= is not equal to operator. It gives True if both the operands are not equal else it gives False.
> is greater than or equal to operator. It gives True if the first operand is greater than the second operand.
>= is greater than or equal to operator. It gives True if the first operand is greater than or equal to the second operand.
Similarly, < and <= are less than and less than or equal to operators respectively.
You will learn more about operators in the next chapter.

Now go through our practice section of Boolean to make your knowledge more concrete.

Talent is good, Practice is better, and Passion is Best
-Frank Lloyd Wright


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.