# 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 values of -1, 1, 0, etc. and a float can take 0.01, 1.2, etc. A Boolean is something which can either be true or false.

• Python 2
• Python 3
```print type(True)
print type(False)
```
```print(type(True))
print(type(False))
```
<class 'bool'>
<class 'bool'>

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

• Python 2
• Python 3
```x = True
y = False
print x
print y
```
```x = True
y = False
print(x)
print(y)
```
True
False

Now, let's do something more.

We have already done x = 10 and we know that it means x is 10. Let's try this:

• Python 2
• Python 3
```x = 10
print x == 10
```
```x = 10
print(x == 10)
```
True

'==' operator is used to compare two values in Python. It is equivalent of asking whether x is 10 or not? Let's do this one more time.

• Python 2
• Python 3
```x = 10
y = 10
print x == y
```
```x = 10
y = 10
print(x == y)
```
True

Here it checks if `x` and `y` are equal or not. Since both `x` and `y` have same values, so `x == y` is `True`

• Python 2
• Python 3
```name = "Sam"
print name == "Sam"
print name == "Aam"
```
```name = "Sam"
print(name == "Sam")
print(name == "Aam")
```
True
Fasle

Here, `=` assigned a value "Sam" to the variable name and `==` checked whether the value of the variable name is "Sam" or not. ## 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 also,
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

• Python 2
• Python 3
```x = 10
y = 20
print x == 10 and y == 20
print x == 3 or y == 20
```
```x = 10
y = 20
print(x == 10 and y == 20)
print(x == 3 or y == 20)
```
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 at least 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. Let's see some more examples.

`not (True or False)` → False.

`True or False` is True because or is used and at least one of the operands is True. So, `not (True)` is False.

• Python 2
• Python 3
```x = 10
y = 20
print not(x == 10 and y == 20)
print not(x == 3 or y == 20)
```
```x = 10
y = 20
print(not(x == 10 and y == 20))
print(not(x == 3 or y == 20))
```
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 they 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 either greater than or equal to the second operand.

Similarly, `< and <=` are less than and less than or equal to operators respectively.

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  