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

OperatorDescriptionExamples
+Addition3 + 2 = 5
1.6 + 4 = 5.6
'ab' + 'cd' = 'abcd'
-Subtraction1 – 2 = -1
5.5 – 5 = 0.5
*Multiplication4.0 * 5 = 20.0
4 * 5 = 20
/Division4 / 2 = 2.0
4.0 / 2 = 2.0
//Integer Division9 // 2 = 4
%Module5 % 3 = 2
5.0 % 3 = 2.0
**Exponent4 ** 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)+= 9 ]
    • 9%4 equals to 1 since there is a remainder  of 1 when we divided 9 by 4. [ (4×2)+= 9 ]
    • 9%5 equals to 4 since there is a remainder  of 4 when we divided 9 by 5. [ (5×1)+= 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).

OperatorDescriptionExamples
==Equalprint (1 == 3)
The result will be
False
!=Not Equalprint (1 != 3)
The result will be
True
>Greater thanprint (1 > 3)
The result will be
False
<Less Thanprint (1 < 3)
The result will be
True
>=Greater Than or Equalprint (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 Equalprint (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

OperatorDescriptionExamples
andLogical ANDprint ((1 > 2) and (4 > 2))
The answer will be
False
orLogical ORprint ((1 > 2) or (4 > 2))
The answer will be
True
notLogical NOTprint (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

OperatorExamples
=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=10
y=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.

OperatorDescriptionExamples
~Negation~ a
The result will be
-71
&Bitwise ANDa & b
The result will be
2
|Bitwise ORa | b
The result will be
103
^Bitwise XORa ^ b
The result will be
101
<<Left Shifta << 3
The result will be
560
>>Right Shift a >> 3
The 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.

Leave a Reply

Your email address will not be published. Required fields are marked *