Skip to content

Operators

Operator is a symbol that is denoting an operation to be performed.

expression is a combination of operands and operators that must be evaluated to a result.

In an expression like 2+3
+ is called as operator
2,3 are operands

 

operands can be numbers, variables, or expressions

Eg:
x+10
a+b
a+(b-c)

 

Types of Operators based on the number of operands:
1. Unary : works with one operand Eg: -, not, ~
2. Binary : works with two operands Eg: +, -, *, /, =
3. Ternary : works with three operands. we will learn about this later.

 

Types of Operators in Python based on their purpose:
1. Arithmetic Operators
2. Assignment Operator
3. Relational or Comparison
4. Logical
5. Bitwise Operators
6. Membership
7. Identity

 

Arithmetic Operators:
Used to perform arithmetic operation

+ addition
– for subtraction
* for multiplication (asterisk)
/ division (forward slash)

% (Modulus) returns remainder after division
// – Floor division
** – returns the power

🐍
filename.py
#Example of arithmetic operators
res=13/2
print("res=",res)
res=13//2
print("res=",res)
res=13%2
print("res=",res)
res=2**3
print("res=",res)

Assignment Operator:
We already learnt about Assignment operator before.

 

Type Casting
The assumption of datatype of variable.
When system is determining the type of variable based on the type of value that we are assigning from right hand side, it is ‘implicit type casting’.
Eg: num=23

When we explicity mention that, it is ‘explicit type casting’
Eg: num=int()

🐍
filename.py
#Example
num=12 #implicit type casting
print(type(num))
num=int(12)  #explicit type casting
num=int("5")
print(type(num))
num=float()
print(type(num))
num=str()
print(type(num))
num=list()
print(type(num))
🐍
filename.py
#Example for Assignment statement
a=12
b=24
print("a=",a)
print("b=",b)
a=b #check output by changing b=a
print("a=",a)
print("b=",b)
🐍
filename.py
#Example for Assignment statement
#exchange values of two variables
a=12
b=24
print("a=",a) #a=12
print("b=",b) #b=24
temp=a
a=b
b=temp
print("a=",a) #a=24
print("b=",b) #b=12

Short Hand Notation or Compound Assignment
Combination of assignment operator with other operators. It can be used only when we have same variable at both sides of assignment statement.

🐍
filename.py
#Example of short hand notation of assignment Operator
a=10
a+=10 #a=a+10
print("a=",a)

Relational or Comparison Operators:
These are used to compare values of their operands and returns True or False value. The expression consists of relational operator is called as condition.

Relational Operators
> greater than
>= greater than or equal
< less than
<= less than or equal
== equal to
!= not equal to

🐍
filename.py
#Example
res=5>7
print(type(res))
print("res=",res)
res=5>=7
print("res=",res)
res=5<7
print("res=",res)
res=5<=7
print("res=",res)
res=5==7
print("res=",res)
res=5!=7
print("res=",res)

Logical or Boolean Operators:
These are work on the logical or boolean value of their operands and finally return True or False

 

Logical Operators:
and
or
not

🐍
filename.py
#Example
res=(5>7) and (12<20)
print("res=",res)

res=(5>7) or (12<20)
print("res=",res)

res=not(5>7)
print("res=",res)

Bitwise Operators:
Bitwise operators perform operations at the bit level.

 

Operator         Name                       Description
&                     AND                          Sets bit to 1 if both bits are 1
` `                    OR                             Sets bit to 1 if any of the bit is 1
^                      XOR                          Sets bit to 1 if only one of the bits is 1
~                     NOT                         (Complement) Inverts the bits (1 becomes 0, 0 becomes 1)
<<                   Left Shift                 Shifts bits to the left (multiplies by 2^n)
>>                   Right Shift               Shifts bits to the right (divides by 2^n)

🐍
filename.py
Example:
# Bitwise Operators Example
a = 5  # 0b0101
b = 3  # 0b0011

print(a & b)  # AND -> 0b0001 (1)
print(a | b)  # OR  -> 0b0111 (7)
print(a ^ b)  # XOR -> 0b0110 (6)
print(~a)     # NOT -> -(5+1) (-6)
print(a << 1) # Left Shift -> 0b1010 (10)
print(a >> 1) # Right Shift -> 0b0010 (2)

Identity Operators:
Identity operators check whether two variables reference the same object in memory.

 

Operator                   Description
is                                 Returns True if both variables point to the same object
is not                          Returns True if both variables point to different objects

🐍
filename.py
#Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)    # True (same object reference)
print(a is c)    # False (different objects with same values)
print(a is not c) # True

Membership Operators:
Membership operators check if a value exists in a sequence (string, list, tuple, etc.).

 

Operator                       Description
in                                    Returns True if the value is found in the sequence
not in                             Returns True if the value is not found in the sequence

🐍
filename.py
#Example:
string = "Hello, Python!"
print("Python" in string)    # True
print("Java" not in string)  # True

numbers = [1, 2, 3, 4, 5]
print(3 in numbers)         # True
print(10 not in numbers)    # True

High Precedence to Low Precedence:


Precedence                                                     Operators                 

1 (highest) ()                                                    Parentheses –>used to override precedence
2 **                                                                    Exponentiation
3 +x, -x, ~x                                                        Unary plus, minus, bitwise NOT
4 *, /, //, %                                                        Multiplication, division, floor division, modulus
5 +, –                                                                 Addition and subtraction
6 <<, >>                                                            Bitwise shift operators
7 &                                                                   Bitwise AND
8 ^                                                                    Bitwise XOR
9 ==, !=, >, <, >=, <=, is, is not, in, not in      Comparisons and membership
10 not                                                             Logical NOT
11 and                                                            Logical AND
12 or                                                               Logical OR
13 (lowest) =, +=, -=, etc.                             Assignment operators