Table of contents

Operators

  • An operator is a symbol used to perform specific operations on given data. When two or more variables or objects are connected with an operator is called Expression.    
  • The main purpose of an operator is to perform operations on variables or data to achieve a specific result.

In Python 7 different types of operators are there. They are

  1.  Arithmetic Operator
  2. Assignment Operator
  3. Relational Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

Arithmetic operators are symbols that denote mathematical operations such as addition, subtraction, multiplication, and division. When two or more variables or objects are combined using arithmetic operators, the resulting combination is called an arithmetic expression.

Arithmetic Operators
Arithmetic Operators
# Arithmetic Operators a=int(input("Enter Value of a:")) b=int(input("Enter Value of b:")) print("Arithmetic Operation Results") print("{}+{}={}".format(a,b,a+b)) print("{}-{}={}".format(a,b,a-b)) print("{}*{}={}".format(a,b,a*b)) print("{}/{}={}".format(a,b,a/b)) print("{}//{}={}".format(a,b,a//b)) print("{}%{}={}".format(a,b,a%b)) print("{}**{}={}".format(a,b,a**b))
  • The symbol for the assignment operator is =
  • The main purpose of the Assignment operator is To transfer the RHS value to the LHS Variable.
  • We can use the Assignment operators in two ways. There are
  1. Single Line Assignment Operator.
  2. Multi-Line Assignment Operator

I. Single Line Assignment Operator

This Operator transfers single values from RSH to a single Variable of LHS.

Syntax :
             varname = value

# single line assignment operators num = 100 num1 = 2003 name = "Python"

 II. Multi-Line Assignment Operator.

This operator transfers multiple values from RHS to Multiple Variables of LHS.

Syntax: 
           var1,var2,var3...varn = val1,val2,val3..... valn

# Multiline assignment operators a,b,c = 10,20,30 print(a,b,c)

Here are the values and variables(LHS and RHS) should be the same.

# Multiline assignment operators a,b,c = 10,20 # error
  • Relational operators are used to compare two values, helping determine their relationship (e.g., equal, greater, or less).
  • When two or more values or variables are connected using relational operators, it forms a relational expression.
  • The result of a relational expression is always either True or False, which aids in decision-making processes.  
Relational Operators
Relational Operators
# Relational Operators a=int(input("Enter Value of a:")) b=int(input("Enter Value of b:")) print("Relational Operation Results") print("{}>{}={}".format(a,b,a>b)) print("{}<{}={}".format(a,b,a<b)) print("{}=={}={}".format(a,b,a==b)) print("{}!={}={}".format(a,b,a!=b)) print("{}>={}={}".format(a,b,a>=b)) print("{}<+{}={}".format(a,b,a<+b))
  • Logical operators are used to combine two or more relational expressions to evaluate a collective condition.
  • Logical expressions, formed by connecting relational expressions with logical operators, result in either True or False. They are also known as compound conditions.  
  • There are three types of logical operators, used to structure complex conditions in decision-making. 
  1. and
  2. or
  3. not

1. and Operator

  • The Logical AND Operator evaluates to True only if all the conditions in the expression are True; otherwise, it evaluates to False.  
  • In the case of the 'and' operator, if it is connected with multiple relational expressions and the first relational expression evaluates to False, the remaining relational expressions will not be evaluated, and the result will be considered False."   
and Operator
and Operator
# Example 1: Both conditions are True a = 5 b = 10 print(a > 0 and b > 5) # Output: True # Example 2: One condition is False print(a > 0 and b < 5) # Output: False # Example 3: Short-circuiting print(a < 0 and b > 5) # Output: False (Second condition is not checked)

  2. or Operator    

  • The Logical or Operator evaluates to True only if any of the conditions in the expression are True; otherwise, it evaluates to False.   
  • "In the case of the 'or' operator, if it is connected with multiple relational expressions and the first relational expression evaluates to True, the remaining relational expressions will not be evaluated, and the result will be considered True."     
or Operator
or Operator
# Example 1: At least one condition is True a = 5 b = 10 print(a > 0 or b < 5) # Output: True # Example 2: Both conditions are False print(a < 0 or b < 5) # Output: False # Example 3: Short-circuiting print(a > 0 or b < 5) # Output: True (Second condition is not checked)

3. not Operator

  • This operator gives the opposite result of a Boolean value.  
not Operators
not Operators
# Example 1: Negating True a = True print(not a) # Output: False # Example 2: Negating False b = False print(not b) # Output: True # Example 3: With expressions x = 5 print(not (x > 3)) # Output: False (x > 3 is True, negated to False)

Bitwise Operators apply only to integer data and not to floating-point values because floating-point numbers have varying precision.

The purpose of Bitwise Operators is as follows:

  • They convert integer data internally into binary format and perform binary operations bit by bit.
  • The final result is always displayed in decimal number format.

Since the operations are performed bit by bit, these operators are named Bitwise Operators.

In Python programming, the following bitwise operators are available.

  • Bitwise Left Shift Operator (<<)
  • Bitwise Right Shift Operator (>>)
  • Bitwise AND Operator (&)
  • Bitwise OR Operator (|)
  • Bitwise Complement Operator (~)
  • Bitwise XOR Operator (^)

1. Bitwise Left shift Operator(<<)

  • The concept of the bitwise left shift operator is to shift the specified number of bits to the left by adding zeros to the right side. The number of zeros added is equal to the specified number of bits.
  • Syntax:- varname = GivenData << No.of Bits  
# Example: x = 5 # Binary: 00000101 result = x << 2 # Shift left by 2 positions print(result) # Output: 20 (Binary: 00010100)

2. Bitwise Right shift Operator(>>)

  • The bitwise right shift operator shifts the specified number of bits to the right by adding zeros to the left side. The number of zeros added is equal to the specified number of bits.
  • Syntax:- varname = GivenData >> No.of Bits  
# Example: x = 20 # Binary: 00010100 result = x >> 2 # Shift right by 2 positions print(result) # Output: 5 (Binary: 00000101)

3. Bitwise and Operator( & )

The Bitwise AND operator compares each bit of the two inputs and returns

  • 1 if both bits are 1.
  • 0 otherwise.
  • Syntax:- varname = value1 & value2
  Bitwise and Operator(&)
  Bitwise and Operator(&)
# Example: x = 20 # Binary: 00010100 result = x >> 2 # Shift right by 2 positions print(result) # Output: 5 (Binary: 00000101)

4. Bitwise or Operator( | )

The Bitwise or operator compares each bit of the two inputs and returns

  • 1 if anyone bit is 1.
  • 0 otherwise.
  • Syntax:- varname = value1 | value2
Bitwise or Operator
Bitwise or Operator
# Example: x = 5 # Binary: 0101 y = 3 # Binary: 0011 result = x | y print(result) # Output: 7 (Binary: 0111)

5. Bitwise complement Operator( ~ )

  • bitwise complement operator (~) inverts the bits of a number. It flips all 1s to 0s and all 0s to 1s.    
# Example: x = 5 # Binary: 00000101 result = ~x print(result) # Output: -6 (Binary: 11111010 in 2's complement representation)
  • In the above code the binary representation of 5 is 00000101.   
  • The complement is 11111010 (in binary) -6 in decimal (2's complement representation).  
  • The most significant bit (MSB) acts as the sign bit. 
  •  Represents a positive number.  
  • 1: Represents a negative number.  
  • Positive numbers are stored directly in binary format.
  • Negative numbers are stored in 2's complement form, which includes flipping all bits and adding 1.
  • Syntax:- varname = ~value

6. Bitwise xor Operator(^)

  • The Bitwise XOR (Exclusive OR) Operator performs a comparison of bits from two numbers. It returns 1 if the corresponding bits of the two numbers are different and 0 if they are the same.   
Bitwise xor Operator(^)
Bitwise xor Operator(^)
# Example: x = 5 # Binary: 0101 y = 3 # Binary: 0011 result = x ^ y print(result) # Output: 6 (Binary: 0110)
  • The purpose of Membership Operators is to check if a specified value is present in an iterable object. 
  • An iterable object is an object that contains multiple elements, such as sequence types (strings, lists, tuples), sets, and dictionaries.
  • In Python, there are two types of Membership Operators.
  1. in
  2. not in

1. in Operator

  •   If the value is present in the Iterable_Object, the expression returns True otherwise, it returns False.  
  • Here, the variable varname will contain either True or False, and its type is bool.
  • Syntax :-  varname = value in itrable_obect
# Example: Using 'in' and 'not in' with a string text = "Hello, Python!" # Check if a substring is present print("Python" in text) # Output: True print("python" in text) # Output: False
# Example: Using 'in' and 'not in' with a list numbers = [1, 2, 3, 4, 5] # Check if an element is present print(3 in numbers) # Output: True print(6 in numbers) # Output: False
# Example: Using 'in' and 'not in' with a tuple colors = ("red", "green", "blue") # Check if an element is present print("green" in colors) # Output: True print("Yellow" in colors) # Output: False
# Example: Using 'in' and 'not in' with a set fruits = {"apple", "banana", "cherry"} # Check if an element is present print("apple" in fruits) # Output: True print("apple1" in fruits) # Output: False
# Example: Using 'in' and 'not in' with a dictionary student = {"name": "Alice", "age": 22, "grade": "A"} # Check if a key is present print("name" in student) # Output: True print("Id" in student) # Output: False

2. not in Operator

  • If the value is present not in the Iterable_Object, the expression returns True otherwise, it returns False.  
  • Here, the variable varname will contain either True or False, and its type is bool.
  • Syntax :-  varname = value not in itrable_obect
# Check if a substring is not in the string text = "Hello, Python!" print("Java" not in text) # Output: True (because "Java" is not in the string) print("Python" not in text) # Output: False (because "Python" is in the string)
# Check if an element is not in the list numbers = [1, 2, 3, 4, 5] print(10 not in numbers) # Output: True (because 10 is not in the list) print(3 not in numbers) # Output: False (because 3 is in the list)
# Check if an element is not in the list numbers = [1, 2, 3, 4, 5] print(10 not in numbers) # Output: True (because 10 is not in the list) print(3 not in numbers) # Output: False (because 3 is in the list)
# Check if an element is not in the tuple colors = ("red", "green", "blue") print("yellow" not in colors) # Output: True (because "yellow" is not in the tuple) print("green" not in colors) # Output: False (because "green" is in the tuple)
# Check if an element is not in the set fruits = {"apple", "banana", "cherry"} print("mango" not in fruits) # Output: True (because "mango" is not in the set) print("apple" not in fruits) # Output: False (because "apple" is in the set)
# Check if a key is not in the dictionary student = {"name": "Alice", "age": 22, "grade": "A"} print("address" not in student) # Output: True (because "address" is not a key) print("name" not in student) # Output: False (because "name" is a key)
  • The purpose of Identity Operators is to compare the memory address of objects.
  • To get the memory address of an object/variable, we use the id() function.  
  • In Python, there are two Identity Operators.
  1. is
  2. is not

1. is Operator:

  • The is operator checks if two variables reference the same object in memory. It returns True if they do, and False otherwise.     
  • Syntax:- var1 is var2
a = 10 b = 10 # Both a and b reference the same object print(a is b) # Output: True # Check memory address using id() print(id(a), id(b)) # Output: Same ID c = 500 d = 500 print(c is d) # Output: True or False (depends on Python's optimization)
x = [1, 2, 3] y = x # Both x and y refer to the same object in memory print(x is y) # Output: True # Check memory address using id() print(id(x), id(y)) # Output: Both IDs will be the same
x = [1, 2, 3] y = [1, 2, 3] # x and y have the same content but are different objects print(x is y) # Output: False # Check memory address using id() print(id(x), id(y)) # Output: Different IDs
x = [1, 2, 3] y = [1, 2, 3] print(x == y) # Output: True (values are equal) print(x is y) # Output: False (different objects)
var = None if var is None: print("Variable is None") # Output: Variable is None

2. is not Operator

  • The is not operator checks if two variables reference the different objects in memory. It returns True if they do, and False otherwise.
  • Syntax:- var1 is not var2
x = [1, 2, 3] y = x # Both x and y reference the same object print(x is not y) # Output: False (since they refer to the same object) # Check memory address using id() print(id(x), id(y)) # Output: Both IDs will be the same
a = 10 b = 10 # Both a and b reference the same object print(a is not b) # Output: False (since they refer to the same object) c = 500 d = 500 print(c is not d) # Output: False or True (depends on Python's optimization)
x = [1, 2, 3] y = [1, 2, 3] # x and y have the same content but are different objects print(x is not y) # Output: True (since they refer to different objects) # Check memory address using id() print(id(x), id(y)) # Output: Different IDs
var = 10 if var is not None: print("Variable is not None") # Output: Variable is not None
str1 = "Hello" str2 = "Hello" print(str1 is not str2) # Output: False (they refer to the same object in memory) str3 = "Hello, Python!" str4 = "Hello, Python!" print(str3 is not str4) # Output: True or False (depends on optimization)