Python Operators With Examples

This article entitle Python Operators is a continuation of the previous article entitled Python Datatype.

Python Operators is a constructor that can use to manipulate the value of the operands. These operators are symbols which mainly used for a purpose of logical arithmetic and other various operations

What are Python Operators?

In Python, operators are very special symbols that can be useful for the designation of some sort of calculation or computation that could need to be performed.

Types of Operators in Python

The following list is the types of Python operators which have been supported.

  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Arithmetic Operators
  • Membership Operators
  • Identity Operators
  • Comparison (Relational) Operators

Logical Operators

Python Logical operators are mainly used to combine a conditional statement.

OperatorDescriptionExample
ANDOnce both operands are true then the condition becomes true.x < 15 and x < 20
OROnce any of the two operands is non-zero then the condition becomes true.x < 14 or x < 14
NOTIt is used to reverse the logical state of its operandnot(x < 15 and x < 20)

Bitwise Operators

The bitwise operators are mainly used for comparing (binary) numbers.

Operator NameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of the two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero-fill left shiftleft shift operation by pushing zeros in from the right and letting the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left operand, and let the rightmost bits fall off

Example

a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101

# Binary AND
c = a & b        # 12 = 0000 1100
print ("a & b : ", c)

# Binary OR
c = a | b        # 61 = 0011 1101
print ("a | b : ", c)

# Binary XOR
c = a ^ b        # 49 = 0011 0001
print ("a ^ b : ", c)

# Binary Ones Complement
c = ~a;           # -61 = 1100 0011
print ("~a : ", c)

# Binary Left Shift
c = a << 2;       # 240 = 1111 0000
print ("a << 2 : ", c)

# Binary Right Shift
c = a >> 2;       # 15 = 0000 1111
print ("a >> 2 : ", c)

Output

a & b :  12
a | b :  61
a ^ b :  49
~a :  -61
a << 2 :  240
a >> 2 :  15

Assignment Operators

Python assignment operator work is mainly used for assigning values to variables.

OperatorExampleSame As
=x = 10x = 10
+=x += 4x = x + 4
-=x -= 4x = x – 4
*=x *= 4x = x * 4
/=x /= 4x = x / 4
%=x %= 4x = x % 4
//=x //= 4x = x // 4
**=x **= 4x = x ** 4
&=x &= 4x = x & 4
|=x |= 4x = x | 4
^=x ^= 4x = x ^ 4
>>=x >>= 4x = x >> 4
<<=x <<= 4x = x << 4

Example

# Assignment Operator
a = 20

# Addition Assignment
a += 6
print ("a += 5 : ", a)

# Subtraction Assignment
a -= 6
print ("a -= 5 : ", a)

# Multiplication Assignment
a *= 6
print ("a *= 5 : ", a)

# Division Assignment
a /= 6
print ("a /= 5 : ",a)

# Remainder Assignment
a %= 6

print ("a %= 3 : ", a)

# Exponent Assignment
a **= 2
print ("a **= 2 : ", a)

# Floor Division Assignment
a //= 3
print ("a //= 3 : ", a)

Output

a += 5 :  26
a -= 5 :  20
a *= 5 :  120
a /= 5 :  20.0
a %= 3 :  2.0
a **= 2 :  4.0
a //= 3 :  1.0

Arithmetic Operators

The arithmetic operators are mainly used with numeric values in order to perform common mathematical operations.

OperatorNameExample
+Addition15 + 15 = 30
Subtraction30 – 20 = 10
*Multiplication20 * 10 = 200
/Division10 / 2 = 5
%Modulus22 % 10 = 2
**Exponent4**2 = 16
//Floor Division9//2 = 4

Example

x = 32
y = 15

# Addition
print ("x + y : ", x + y)

# Subtraction
print ("x - y : ", x - y)

# Multiplication
print ("x * y : ", x * y)

# Division
print ("x / y : ", x / y)

# Modulus
print ("x % y : ", x % y)

# Exponent
print ("x ** y : ", x ** y)

# Floor Division
print ("x // y : ", x // y)

Output

x + y :  47
x - y :  17
x * y :  480
x / y :  2.1333333333333333
x % y :  2
x ** y :  37778931862957161709568
x // y :  2

Membership Operators

The membership operators are mainly used to test if the sequence is already presented into an object.

OperatorDescriptionExample
inReturns True once the sequence has a specified value that is present in the objectx in y, here in results in a 1 if x is a member of sequence y.
not inReturns True once the sequence with a specified value is not present in the objectx not in y, here not in results in a 1 if x is not a member of sequence y.

Identity Operators

The identity operators are mainly used for comparing an object if there are not equal, but once they actually have the same object with the same memory location.

OperatorDescriptionExample
isIt simply returns True once both variables have the same objectx is y, here is results in 1 if id(x) equals id(y).
is notIt simply returns True once both variables have not the same objectx is not y, here is not results in 1 if id(x) is not equal to id(y).

Comparison Operators

The Python comparison operators are mainly used for comparing two values

OperatorNameExample
==Equal5 == 6 is not true.
!=Not Equal5 != 6 is true.
>Greater Than5 > 6 is not true.
<Less Than5 < 6 is true.
>=Greater than or Equal to5 >= 6 is not true.
<=Less than or Equal to5 <= 6 is true.

Summary

In summary, you have read about Python Operators. We also discussed in this article what are Python Operators, and the different types of operators.

I hope this article about Operator in Python could help you a lot to continue pursuing learning this powerful programming language.

If you want to learn more check out my previous and latest articles for more career-changing articles.

Leave a Comment