Python Operators


Python operators are one of the most used character or character set in Python Coding. These operators are used mainly in math operations in python. You can add two values, divide one to another or you can compare two variables with these python operators.


As an operator, you can sue one character or one more character. Each of these character sets has a different meaning as a python operator.


There are different python operators. We can divide them into seven categories like below:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators


Now, let’s learn what are these python operators with different examples for each of them.


You can also check Python If Else


Arithmetic Operators


Python Arithmetic Operators are used with math operations like addition, multiplication, subtraction, division etc. You can find the list of these operations that are done by Python Arithmetic Operators.


  • Addition                +
  • Subtraction                –
  • Multiplication                *
  • Division                /
  • Modulus                %
  • Exponentiation                **
  • Floor division                //




Now, let’s give some examples to these operators.



You can also Download Python Cheat Sheet!



For example, to add one value to another, you can use plus (+) operator like below:


a = 10
b = 5
print (a + b)


The output will be:





We can also use divide operator of python for division like below:


a = 10
b = 5
print (a / b)


The result will be:




In another example, we can calculate exponentiation. Let ‘s do it for 2 and 6.


a = 2
b = 6
print (a ** b)


The output will be the result of 2 x 2 x 2 x 2 x 2 x 2. And this is:




The are basic math operations that we can udo by using these basic python operators.



Assignment Operators


Python assignment operators are used to assign value to variables. The basic operator that I used for this purpose is equal (=) operator. Beside this operator, there are other assignment operators that has one more character. You can find all of these Python assignment operators below:


  • =                Asign                                   x = 1                     x = 1
  • +=                Add AND                            x += 2                   x = x + 2
  • -=                Subtract AND                    x -= 2                    x = x – 2
  • *=                Multiply AND                    x *= 2                   x = x * 2
  • /=                Divide AND                        x /= 2                   x = x / 2
  • %=                Modulus AND                   x %= 3                  x = x % 3
  • //=                Divide Floor AND             x //= 3                  x = x // 3
  • **=                Exponent AND                  x **= 3                 x = x ** 3
  • &=                Bitwise AND                      x &= 4                  x = x & 4
  • |=                Bitwise OR                         x |= 4                   x = x | 4
  • ^=                Bitwise  xOR                      x ^= 4                   x = x ^ 4
  • >>=                Bitwise Right Shift           x >>= 5                 x = x >> 5
  • <<=                Bitwise Left Shift             x <<= 5                 x = x << 5



Now, let’s do some examples with these assignment operators of python programming.


Below, we will use *= operator to calculate


a = 3
a *= 2
print (a)




Or in another example, let’s use Bitwise AND. This Bitwise AND operator (&=) will compare the bits of each variable and will return 1 if each bits are 1.


x = 7
x &= 3


7 -> 0000 0111 (binary)

3 -> 0000 0011 (binary)



So, the bitwise and will return with the value of the equal ones and this is:






Lastly, let’s do an example with Bitwise left shift. Here, we will sue Bitwise Left Shift (<<=) operator like below:


a = 5


Here the binary value of 5 is

a = 5 = 0000 0101 (Binary)


So, when we use Bitwise Left Shift (<<=) operator, for the new value, the binary numbers will be shifted one left like below:

a << 1 = 0000 1010 = 10


And the output of this python code will be:







Comparison Operators


Another operator type in python programming is python comparison operators. With these operators, we can compare any variable with another variable. Below, you can find these python comparison operators.


  • == Equal                                              x == y
  • != Not equal                                        x != y
  • > Greater than                                    x > y
  • < Less than                                          x < y
  • >= Greater than or equal to             x >= y
  • <= Less than or equal to                   x <= y




These operators are used widely in python programming.


For example, we can use equal operator (==) to compare two variable and return a result according to it.


a = 5
b = 5
if (a==b):
     print("Not Equal")


The output will be:






Now, let’s use also for loop with range to print a series of numbers. Here, we will control our variable with >= operator. If it is equal or greater than the mentioned value, it will break printing.


for x in range(6):
 if x >= 4: 


It will start to print from 0 to 5. But it will control the value of x every time if it is equal or greater than 4. So, in the output, there will be only the values lower than 4.





Logical Operators


To combine conditional statements, we use python logical operators. There are three logical operators in python. These are:


  • and
  • or
  • not


and operator returns True, if both statements are true.

or operator returns True, if one of the statements is true.

not operator reverse the result, returns False if the result is true.


For example, below, we will use and operator of python and check the two statements. If two of them are true, then the program will return True. If one or two of them are false, it will return false.


a = 25
b = 50
print(a > 5 and a < 40)
print(b > 5 and b < 40)


The output of this python code will be:






Because, in the first line, both statements are True. In the second line one statement is True and the other is False. For True result, all the statements of and operator must be true.


Now, let’s do another example with or operator. Only one true statement is enough for or operator to return True.


b = 50
c = 3
print(b > 5 or b < 40)
print(c > 5 or c < 2)


The result of the first line will be True, because one statement is true about b.

The result of the second line will be False, because both of the statements are false about c.





Identity Operators


Python Identity Operators are the operators that are used to compare the objects. This is not only a comparison about their value, but also their location in the memory. These operators check that if they are the same object or not. Below, you can find these Python Identity Operators.


  • is
  • is not


is operator checks that if both objects are the same object. If yes, it returns True.

is not operator checks that if both objects are different objects. If yes, it returns True.


For example, in the below python code, we will compare two string list.


a = ["aa", "bb", "cc"]
b = ["aa", "bb", "cc"]
c= a
print(a is b)
print(a is c)


The result of first print will be False. Because, a is not same as b. But the result of the second print will be True. Because, c is the same object as a.







Membership Operators


Python Membership Operators are used to check the members of a sequence. To do this, it uses two operators:


  • in
  • not in


in operator returns True, if it finds the value as a member in the sequence.

not in operator return True, if it do not find the value as a members in the sequence.


In the below example, we will check “cc” string in the string list a.


a = ["aa", "bb", "cc"]
print("cc" in a)
print("dd" in a)


The first result will be True, because “cc” is the member of a.

The second result will be False, because there is no “dd” in a.






The same example with not in operator is below. This time the results will be reverse.


a = ["aa", "bb", "cc"]
print("cc" not in a)
print("dd" not in a)








Bitwise Operators


Lastly, we will talk about Bitwise operators of python. These operators are used to compare binary numbers in python programming.


  • & AND                                      Sets each bit to 1 if both bits are 1
  • | OR                                          Sets each bit to 1 if one of two bits is 1
  • ^ XOR                                      Sets each bit to 1 if only one of two bits is 1
  • ~ NOT                                      Inverts all the bits
  • << Zero fill left shift              Shift left by pushing zeros in from the right and let the leftmost bits fall off
  • >> Signed right shift             Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off



In this lesson, we have learned different python operators. You will use these important python operators too much in your codes. So, if you practice more with different examples, you can improve your knowledge about this important lesson.


Back to: Python Programming Course > Python Math

Leave a Reply

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

Python Programming Course