Crus4

logo

Python Operators : Full Explained


What are Operators in Python

In Python, Operators are special symbols or you can say keywords, that perform operations on one or more values or variables and produce a result. Python provides a wide range of operators that can be used for arithmetic, assignment, comparison, logical operations, and more. Below, let’s discuss some of the key operators in Python:

Python Arithmetic Operator

In python, arithmetic operator are used with numeric values or with strings (only in case of addition) to perform basic mathematical operations. Like addition, subtraction, multiplication, division, modulus, floor division, Exponentiation. Here is example:

Example

x = 3
y = 2
#Addition 
print (x+y)  #outputs: 5
#Subtraction
print (x-y)  #outputs: 1
#Multiplication 
print (x*y)  #outputs: 6
#Division 
print (x / y)  #outputs: 1.5
#Floor Division
print (x // y) #outputs: 1
#Modulus
print (x % y) #outputs: 1
#Exponentiation 
print (x ** y)  #outputs: 9

Python Assignment Operator

In python, Assignment operator is used to assign any value to a variable. Assignment operator is of two types:

  • Assignment ( = ):- This operator assigns the value on the right to the variable on the left.
  • Compound Assignment:- Compound Assignment includes +=, -=, *=, /=, //=, %=, these signs performs the operation on the right operand with the left operand and assigns the result to the left operand. Take a look at the below table and clear you doubts about these signs.
Operator ExampleAlso written as
+=x+=2x = x+2
-=x -= 2x = x-2
*xx*=2x = x*2
/xx/=2x = x/2
//=x//=2x = x//2
%=x%=2x= x%2
**xx**=2x = x**2
Compound Assignment Operators

In an example below, let’s write a code and see how you can use these operators in Python.

Example

x = 3
#Addition 
x+=2  
print (x) #outputs: 5
#Subtraction
x -=2
print (x)  #outputs: 1
#Multiplication 
x*=2
print (x)  #outputs: 6
#Division 
x/=2
print (x)  #outputs: 1.5
#Floor Division
x//=2
print (x) #outputs: 1
#Modulus
x%=2
print (x) #outputs: 1
#Exponentiation 
x**=2
print (x)  #outputs: 9

Python Comparison Operator

Comparison Operators in python are used to compare two values.

Name OperatorExampleDescription
Equal to==x==yReturns True, if x is equal to y, otherwise False
Not Equal to!=x!=yReturns True, if x is not equal to y, otherwise False
Greater Than>x>yReturns True, if x is greater than y, otherwise False
Less Than<x<yReturns True, if x is less than y, otherwise False
Greater than or equal to>=x>=yReturns True, if x is greater or equal to y, otherwise False
Less than or equal to<=x<=yReturns True, if x is less or equal to y, otherwise False
Comparison Operators

Here is how you can use these operators in your code.

Example

x = 3
y = 5
print (x==y) #outputs: False
print (x!=y) #outputs: True
print (x>y) #outputs: False
print (x<y) #outputs: True
print (x>=y) #outputs: False
print (x<=y) #outputs: True

Python Logical Operator

Python logical operators are used to perform logical operations on expressions.

OperatorExampleDescription
andx> y and y<zReturns True, if both statements are true, otherwise False
orx>y or y<zReturns True, if any one or both of the statements are true, otherwise False
notnot(x > y)Returns True, if x is not greater than y, otherwise False
Python Logical Operator

Let’s write a code in an below example to clear your all doubts about Python Logical Operators.

Example

x = 3
print (x > 2 and x < 5) #outputs: True
print (x > 5 and x == 3) #outputs: False
print (x > 4 or x < 6 ) #outputs: True
print (not(x > 5)) #outputs: True
print (not (x == 3)) #outputs: False

Python Bitwise Operators

The Bitwise Operators are used to perform operations on integers.

Name Operator Example Description
And & x & y Performs a bitwise AND operation on each pair of corresponding bits.
or | x | y Performs a bitwise OR operation on each pair of corresponding bits.
XOR ^ x ^ y Performs a bitwise XOR operation on each pair of corresponding bits.
NOT ~ ~x Inverts the bits of the operand, changing 0 to 1 and 1 to 0.
Zero fill left shift << x << 3 Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
Signed right shift >> x >> 3 Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
Python Bitwise Operator

Here is an example of an Python Bitwise Operator.

Example

print (5 & 3) #outputs: 1
# Explanation 
""" The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0:
we know that 
5 = 0000000000000101
3 = 0000000000000011
so the result is 0000000000000001, which is equal to 1
1 = 0000000000000001


Decimal numbers and their binary values:
0 = 0000000000000000
1 = 0000000000000001
2 = 0000000000000010
3 = 0000000000000011
4 = 0000000000000100
5 = 0000000000000101
6 = 0000000000000110
7 = 0000000000000111
"""

Python Identity Operators

Python Identity Operators are used to compare the identity of two objects. They do not check whether the two objects are equal, but rather if they refer to the same memory location or not. Python Provides two Identity Operators. is and is not:

  • is:- The is operator returns True if the two operands have the same identity, means they refer to the same object in memory. Otherwise, it returns False.
  • is not:- The is not operator returns True if the two operands have different identities, means they refer to different objects in memory. Otherwise, it returns False.

Here is an example Python Identity Operators.

Example

x = ["Learn", "Python"]
y = x
z = ["Learn" "Java"]

# Using 'is' operator
print(x is y)   # Outputs: True because 'x' and 'y' refer to the same object
print(x is z)   # Outputs: False Because 'x' and 'z' refer to different objects

# Using 'is not' operator
print(x is not y)   # Outputs: False because 'x' and 'y' refer to the same object
print(x is not z)   # Outputs: True Because 'x' and 'z' refer to different objects

Python Membership Operators

Python Membership Operator are used to test whether a value or item is a member of a sequence (string, list, tuple) or not. There are two membership operators in Python. in and not in:

  • in:- Returns True if the value is present in a list, tuple or set, otherwise False.
  • not in:- Returns True if the value is not present in a string, tuple or set, otherwise False.

Here is an example of Python Membership Operator.

Example

fruits = ['apple', 'banana', 'cherry']

# Using 'in' operator
print('banana' in fruits)   # Outputs: True
print('grapes' in fruits)    # Outputs: False

# Using 'not in' operator
print('orange' not in fruits)   # Outputs: True
print('apple' not in fruits)    # Outputs: False

Share This Post!

Python Operators : Full Explained