# Python Operators

Hello guys…! I’m back with the third tutorial of the Python Tutorials Pack. As I said in the last tutorial, Today I’m going to tell you about python operators. OK, Let’s start.

Basically, We can categorize Python Operators as follows.

- Arithmetic Operators
- Relational / Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators

#### Arithmetic Operators

Operator | Description | Examples |
---|---|---|

+ | Addition | 3 + 2 = 5 1.6 + 4 = 5.6 'ab' + 'cd' = 'abcd' |

- | Subtraction | 1 – 2 = -1 5.5 – 5 = 0.5 |

* | Multiplication | 4.0 * 5 = 20.0 4 * 5 = 20 |

/ | Division | 4 / 2 = 2.0 4.0 / 2 = 2.0 |

// | Integer Division | 9 // 2 = 4 |

% | Module | 5 % 3 = 2 5.0 % 3 = 2.0 |

** | Exponent | 4 ** 2 = 16 4.0 ** 2 = 16.0 |

There are few things to be highlighted.

- When we use the
**Division Operator (/)**, the output will always be a floating point number. - If we use the
**Integer Division Operator (//)**, The output will always be an Integer. That means the digits after the decimal point will be removed from the result. - We use
**Module Operator (%)**to find the remainder . See following examples.- 10%5 equals to 0 since there is no remainder after dividing 10 by 5. [ (5×2)+0 = 9 ]
- 9%4 equals to 1 since there is a remainder of 1 when we divided 9 by 4. [ (4×2)+1 = 9 ]
- 9%5 equals to 4 since there is a remainder of 4 when we divided 9 by 5. [ (5×1)+4 = 9 ]

#### Relational / Comparison Operators

When we use these operators in a comparison, the result will be given in as a Boolean type (True or False).

Operator | Description | Examples |
---|---|---|

== | Equal | print (1 == 3)The result will be False |

!= | Not Equal | print (1 != 3)The result will be True |

> | Greater than | print (1 > 3)The result will be False |

< | Less Than | print (1 < 3)The result will be True |

>= | Greater Than or Equal | print (1 >= 3)The result will be False___________________ print (5 >= 3)The result will be True___________________ print (3 >= 3)The result will be True |

<= | Less Than or Equal | print (1 <= 3)The result will be True___________________ print (5 <= 3)The result will be False___________________ print (3 <= 3)The result will be True |

#### Logical Operators

Operator | Description | Examples |
---|---|---|

and | Logical AND | print ((1 > 2) and (4 > 2))The answer will be False |

or | Logical OR | print ((1 > 2) or (4 > 2))The answer will be True |

not | Logical NOT | print (not True)The answer will be False |

Let me give you two key points you should know about these operators.

- Always we should use Boolean type of operands in logical operations.
- Both
**Logical And Operator**and**Logical OR Operator**are considered as**Short Circuit Operators**. We call them as Short Circuit Operators since,- When we use Logical AND Operator, it checks the second operand if and only if the first operand is True.
- When we use Logical OR Operator, it checks the second operand if and only if the first operand is False.

#### Assignment Operators

Operator | Examples |
---|---|

= | x = y |

+= | x += y is equal to x = x + y |

-+ | x -= y is equal to x = x - y |

*= | x *= y is equal to x = x * y |

/= | x /= y is equal to x = x / y |

%= | x %= y is equal to x = x % y |

**= | x **= y is equal to x = x ** y |

//= | x //= y is equal to x = x // y |

Let me explain above examples with numbers.

Let’s assume there are two variables in your program named x and y are assigned with the values 10 and 3 respectively.

x=10y=3

- The output of the
**x+=y**is**13**. It’s the same output of the**x=x+y (x=10+3)** - The output of the
**x-=y**is**7**. It’s the same output of the**x=x-y (x=10-3)** - The output of the
**x*=y**is**30**. It’s the same output of the**x=x*y (x=10*3)** - The output of the
**x/=y**is**3.3333333333333335**. It’s the same output of the**x=x/y (x=10/3)** - The output of the
**x%=y**is**1**. It’s the same output of the**x=x%y (x=10%3)** - The output of the
**x**=y**is**1000**. It’s the same output of the**x=x**y (x=10**3)** - The output of the
**x//=y**is**3**. It’s the same output of the**x=x//y (x=10//3)**

#### Bitwise Operators

Assume there are two variables in your program as follows.

a=70 b=35

Examples given in the table are based on above variables.

Operator | Description | Examples |
---|---|---|

~ | Negation | ~ aThe result will be -71 |

& | Bitwise AND | a & bThe result will be 2 |

| | Bitwise OR | a | bThe result will be 103 |

^ | Bitwise XOR | a ^ bThe result will be 101 |

<< | Left Shift | a << 3The result will be 560 |

>> | Right Shift | a >> 3The result will be 8 |

Now you may want to know how these Bitwise Operators are working. But I think, that’s enough for today. Because if I starts to explain about those Bitwise operations today, you guys will be exhausted by reading a long article. So I will not explain that now and definitely the next week tutorial will be on “How Bitwise Operators work”.

I admire your comments and I really love to answer your questions. So, do not hesitate to use the comment area below. And, if you think this tutorial is worth to share with your friends, please do it. I really appreciate that. And finally, I request you to subscribe Aiten Tutorials.