Python 3-Operators

Python Operators

Python Operators are used to perform operations on variables and values.

Python divides the operators and grouped as following:

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

Python Arithmetic operators

The following are the Arithmetic operators in Python:

>>> x = 10
>>> y = 5
>>>
>>> x+y
15
>>> x-y
5
>>> x*y
50
>>> x/y
2.0
>>> x%y
0
>>> x ** y
100000
>>>

From the above example,

  • The values 10 and 5 are stored in the variable x and y
  • Addition of x and y returns 15
  • Subtraction of x and y return 5
  • Multiplication of x and y return 50
  • Division of x and y return 2.0, which is a float variable
  • Modulo of x and y return 0, since the remainder for this is 0
  • Exponential operator ** of x and y return 100000

Python Assignment operators

The following are the Assignment operators in Python

>>> x=5
>>> x+=3
>>> print (x)
8
>>> x-=3
>>> print(x)
5
>>> x*=5
>>> print (x)
25
>>> x/=5
>>> print (x)
5.0
>>> x=5
>>> x//=3
>>> print (x)
1
>>> x**=3
>>> print (x)
1
>>> x&=3
>>> print (x)
1
>>> x|=3
>>> print (x)
3
>>> x^=3
>>> print (x)
0
>>> x>>=3
>>> print (x)
0
>>> x<<=3
>>> print (x)
0

From the above example,

  • The value 5 is assigned to the variable x and various arithmetic operators are used in a fashion of assignment operators such as +=, -=, *=, /=, **= and %=
  • The Bitwise operators are also used in the fashion of assignment operators.

Python Comparison operators

The following are the Comparison operators of Python

>>> x=10
>>> y=5
>>> x==y
False
>>> x!=y
True
>>> x>y
True
>>> x<y
False
>>> x>=y
True
>>> x<=y
False
>>>

From the above example,

  • The value 10 and 5 are assigned to the variable x and y.
  • As 10 is greater than 5, hence other than >(greater than) everything returns False

Python Logical operators

The following are the logical operators in Python

>>> x=5
>>> if x>3 and x<10:
...     print("value of x lies between 3 and 10")
...
value of x lies between 3 and 10
>>>
>>> x=3
>>> if x<2 or x<5:
...     print("value of x less than 2 or 5")
...
value of x less than 2 or 5
>>>
>>> x=15
>>> if not(x<3 and x<10):
...     print("value of x is not less than 10")
...
value of x is not less than 10
>>>

From the above example,

  • The logical operator and returns True if both the statements are True
  • The logical operator or returns True if anyone of the statement is True
  • The logical operator not returns False, if the result is true. Hence, it reverses the result.

Python Identity operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location. The following are the Identity operators in Python

>>> x=5
>>> y=10
>>> x is 5
True
>>> x is y
False
>>> x is not y
True
>>>

From the above example,

  • The is operator returns true if both variables are the same object
  • The is not operator returns true if both variables are not the same object

Python Membership operators

Membership operators are used to test if a sequence is presented in an object:

>>> x = ["apple", "banana"]
>>>
>>> print("banana" in x)
True
>>>
>>> print("pineapple" not in x)
True

From the above example,

  • The in operator returns True if a sequence with the specified values is present in the object.
  • The not in operator returns True if a sequence with the specified values is not present in the object.

Python Bitwise operators

Bitwise operators are used to compare (binary) numbers:

>>> a = 60            # 60 = 0011 1100
>>> b = 13            # 13 = 0000 1101
>>> c = 0
>>>
>>> c = a & b;        # 12 = 0000 1100
>>> print ("Line 1 - Value of c is ", c)
Line 1 - Value of c is  12
>>>
>>> c = a | b;        # 61 = 0011 1101
>>> print ("Line 2 - Value of c is ", c)
Line 2 - Value of c is  61
>>>
>>> c = a ^ b;        # 49 = 0011 0001
>>> print ("Line 3 - Value of c is ", c)
Line 3 - Value of c is  49
>>>
>>> c = ~a;           # -61 = 1100 0011
>>> print ("Line 4 - Value of c is ", c)
Line 4 - Value of c is  -61
>>>
>>> c = a << 2;       # 240 = 1111 0000
>>> print ("Line 5 - Value of c is ", c)
Line 5 - Value of c is  240
>>>
>>> c = a >> 2;       # 15 = 0000 1111
>>> print ("Line 6 - Value of c is ", c)
Line 6 - Value of c is  15
>>>

From the above example,

  • The AND operator, sets each bit to 1 if both bits are 1
  • The OR operator, sets each bit to 1 if one of  two bit is 1
  • The XOR operator, sets each bit to 1 if only one of two bit is 1
  • The NOT operator, inverts all the bits
  • The Zero fill left shift operator, shift left by pushing zeros in from the right and let the leftmost bits fall off
  • The Signed right shift operator, shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off