Python Operators with examples

python operators

Python operators are considered a high-level programming language with less complexity when it comes to using the basic operators in the code. It is built to read and implement computer language with ease. Let us see the basic operators provided by Python.

What are Operators in Python?

Types of operator.

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

For example:

>>> 3+5
    8

Here, + is the operator that performs addition. 3 and 5 are the operands and 8 is the output of the operation.

Python Arithmetic Operators

Arithmetic operators help us to do several types of mathematical problems like addition, subtraction, multiplication, exponential values, floor divisions, etc.

Operator NameSymbolFunctionality
Addition+Adds the operands on either side
Subtraction-Subtracts the operands on either side
Multiplication*Multiplies the operands on either side
Division/Divides the operands on the left by the one on the right
Modulus%Divides the operand on the left by the one on the right and returns the remainder
Exponent**Takes exponent of the operand on the left to the power of right

Examples: Arithmetic operators

# working with operators
x=10
y=3

print("value x + y is", x + y)
print("value x - y is", x - y)
print("value x * y is", x * y)
print("value x / y is", x / y)
print("value x % y is", x % y)
print("value x // y is", x // y)
print("value x ** y is", x ** y)

Output

value x+y is 13
value x-y is 7
value x*y is 30
value x/y is 3.333333333
value x%y is 1
value x//y is 3
value x**y is 1000

Comparison Operators

comparison operators are operators that compare two operands’ values and returns true or false in case of whether the condition has matched or not. It is also called Python Relational Operator.

OperatorMeaning
==Checks if two operands are equal
!=Checks if two operands are not equal
>Checks if one operand is greater than another
<Checks if one operands is less than another
>=Checks if one operand is greater than or equal to another
<=Check if one operand is less than or equal to another

If a condition evaluates to true, then a value of 1 is returned. Else a value of 0 is returned.

example: comparison operators

# working with operators

a = 168; b = 90;
if (a==b):
     print"operator ==: a is equal to b"
else:
   print "operator ==: a is not equal to b"

if (a!=b):
    print "operator !=: a is not equal to b"
else:
    print "operator !=: a is equal to b"

if (a<>b):
    print "operator <>: a is not equal to b"
else:
   print "operator <>: a is equal to b"

if (a<b):
    print "operator <: a is less than b"
else:
   print "operator <: a is not less than b"

if (a>b):
    print "operator >: a is greater than b"
else:
   print "operator >: a not greater than b"

if (a<=b):
    print "operator <=: a is either less than or equal to b"
else:
   print "operator <=: a is neither less than nor equal to b"

if (b>=a):
    print "operator >=: b is either greater than or equal to a"
else:
   print "operator >=: b is neither greater than nor equal to a"


Output

operator ==: a is not equal to b
operator !=: a is not equal to b
operator <>: a is not equal to b
operator <: a is not equal to b
operator >: a is greater than b
operator <=: a is neither less then nor equal to b
operator >=: b is neither greater than nor equal to a

Python Logical Operators

Logical operators are used to performing logical AND, OR, and NOT operations in Python. These are operators used to determine the value of conditions based on the value of the operands, where the operands are Boolean values. Hence these operators work exclusively with True/False value.

Logical Operators

OperatorMeaning
&&This is the logical AND operator
||This is the logical OR operator
!This is the logical NOT operator

Below is a table for logical AND operator, based on the possible value combinations of the operands.

Logical Operators-AND

Operand AOperand BResult
TrueTrue1
TrueFalse0
FalseTrue0
FalseFalse0

Example Logical AND operator

# working with operators
i=True; j=True; k=False
print("i AND j:",i and j)
print("i AND k:", i and k)
print("k AND j:", k and j)
print("k AND k;", k and k)

Output

i AND j: True
i AND k:False
k AND j:False
k AND k:False

Next is a table for the logical OR operator, based on the possible value combination of the operands.

Logical Operators –OR

Operand AOperand BResult
TrueTrue1
TrueFalse1
FalseTrue1
FalseFalse0

Examples Logical OR operator

# working  with operators
i=True; j=True; k=False
print("i OR j:",i or j)
print("i OR k:", i or k)
print("k OR j:", k or j)
print("k OR k;", k or k)

Output

i OR j: True
i OR k:True
k OR j:True
k OR k:False

Lastly is a table for the logical NOT operator, based on the two possible values of the operands.

Relational Operators-NOT

Operand AResult
True0
False1

Example Logical NOT operator


# working with operators
i = True; j = False;
print("NOT i:", not i)
print("NOT j:", not j)

Output

NOT i: False
NOT J: True

Python Assignment Operators

These kinds of operators are used to assign several values to the variables. Let’s check the different types of assignment operators.

OperatorMeaning
+=This is used to carry out the addition and assignment operator is one go
-=This is used to carry out the subtraction and assignment operator is one go
*=This is used to carry out the multiplication and assignment operator is one go
/=This is used to carry out the division and assignment operator is one go
%=This is used to carry out the modulus and assignment operator is one go

Example: Assignment Operators

X = 20; Y = 10
R = X+Y
print(R)

X = 20;
Y =10
X += Y
print(X)

X = 20;
Y = 10
X -= Y
print(X)

X = 20;
Y = 10
X *= Y
print(X)

X = 20;
Y = 10
X /= Y
print(X)

X = 20;
Y = 10
X %= Y
print(X)

X = 20;
Y = 10
X **= Y
print(X)


Output

30
30
10
200
2.0
0
10240000000000

Python Membership Operators

Membership operators are operators that validate the membership of a value. It examines for membership in a sequence like strings, list, tuples, etc. Two types of membership operators are:

Membership in operator

The condition becomes true if it can find a variable in a specified sequence.

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
print('Sunday' in days)

Output

True

not in operator

The condition become true it it can find no variable in a specified sequence.

days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
print('Xunday' not in  days)

Output

True

Python Bitwise Operators

Bitwise operators are used to perform bit operations. All the decimal values will be converted in the binary format here. 

Let us suppose: 


 
 a = 0101 1010
 b = 0001 1000

 (a & b) = 0001 1000
 (a | b)  = 0101 1010 
 (a ˆ b ) = 0100 0010
 (~a) = 1010 0101 


Note: There is an in-built function [bin ()] in python that can obtain binary representation of an integer number.

OperatorNameMeaningSyntax
&ANDThis copies a bit to the result if it exists in both operandsx & y
|ORThis copies a bit to the result if it exists in either operandsX | y
^XORThis copies a bit to the result if it exists in one operand but not in bothX ^ y
<<Zero fill left shiftHere the left operand value is moved left by the number of bits specified by the right operandx<<
>>Zero fill left shiftHere the left operand value is moved right by the number of bits specified by the right operandx>>
~NOTInverts all the bits~x

Example: Bitwise Operators

# working with operators 
x =10; y=3

print("showcasing the & bit operator")
print(x & y)

print("showcasing the | bit operator")
print(x | y)

print("showcasing the ^ bit operator")
print(x ^ y)

print("showcasing the << bit operator")
print(x << 2)

print("showcasing the >> bit operator")
print(x >> 2)

Output

Showcasing the & bit operator
2
Showcasing the | bit operator
11
Showcasing the ^ bit operator
9
Showcasing the << bit operator
40
Showcasing the >> bit operator
2