Introduction to Python Programming Cycle of Python Varibles and Datatypes in Python Input and Output in Python Operators in Python Precedence of Operators in Python Typecasting in Python If Else in Python Loops in Python Break, Continue and Pass in Python Functions in Python Default Arguments and Keyword Arguments in Python Strings in python Lists in Python Tuple in Python Dictionary in Python Sets in Python List Comprehension in Python Unpacking Sequences in Python Higher Order Functions in Python Lambda Functions in Python Sieve of Eratosthenes Algorithm in Python Linear Search in Python Binary Search in Python Selection Sort in Python Bubble Sort in Python Insertion Sort in Python Merge Sort in Python

Operators in Python

Operators are used to perform computation on values. We have 7 types of Operators in Python. These are:
  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Identity operators
  6. Membership operators
  7. Bitwise operators

Arithmetic operators

These are general mathematical operations, which work on more than one operand.

Arithmetic operators
Operators Meaning Example Result
+ Addition 10+2 12
- Subtraction 10-2 8
* Multiplication 10*2 20
/ Division 10/2 5
// Floor division 11/2 5
% Modulus 11%2 1
** Exponentiation 2**3 8

Assignment operators

As the name suggests, they are used to assign value to variables.

Assignment operators
Operators Example Equivalent
= x = 10 x = 10
+= x += 10 x = x + 10
-= x -= 10 x = x - 10
*= x *= 10 x = x * 10
/= x /= 10 x = x / 10
%= x %= 10 x = x % 10
//= x //= 10 x = x // 10
**= x **= 10 x = x ** 10

Comparison operators

Comparison operators are used to compare two values. The result of the comparison is true or false.

Comparison operators
Operators Meaning Example Result
> Greater than 10>2 True
< Less than 10<2 False
== Equal 10==10 True
!= Not equal 10!=2 True
>= Greater than or equal to 11>=2 True
<= Less than or equal to 11<=2 False

Logical operators

Logical operators are used to combine 2 or more conditional statements:

Logical operators
Operators Explanation Example Result
and Return true only when all conditional statements are true 10>2 and 15>5 True
or Return false only when all conditional statements are false 10>2 or 15>25 True
not It reverses the result not(5>15) True

Identity operators

Identity operators are used to compare objects. They compare the memory locations of two objects. We have two identity operators:
  1. is :- Return true if both points to the same memory location or we can say both are same objects. Consider the below Example for better understanding.
    
        a = ["rapid","coders"]
        b = ["rapid","coders"]
        c = b
        print(a is b)   # output-> False
        # is and == operators have different functions.
        print(a == b)   # output-> True
        print(c is b)   # output-> True
    
  2. is not :- It is opposite to 'is' operator. Return true if both point to different memory locations or we can say both are not same objects. Consider the below Example for better understanding.
  3. 
        a = ["rapid","coders"]
        b = ["rapid","coders"]
        c = b
        print(a is not b)   # output-> True
        print(a == b)   # output-> True
        print(c is not b)   # output-> False
    

Membership operators

Membership operators are used to check membership of a value. It checks whether a value is a member of a sequence, such as list, tuple. We have two Membership operators:
  1. in :- Return true if the given value is present in the sequence. Consider the below Example for better understanding.
    
        a = ["rapid","coders"]
        b = "rapid"
        print(b in a)   # output-> True
        print("akshay" in a)    # output-> False
    
  2. not in :- Return true if the given value is not present in the sequence. Consider the below Example for better understanding.
  3. 
        a = ["rapid","coders"]
        b = "rapid"
        print(b not in a)   # output-> True
        print("akshay" in a)    # output-> False
    

Bitwise Operators

Bitwise Operators are used to perform operations on bits. We will see Bitwise operators in detail in further articles.
Bitwise operators
Operators Name Example Result
& Bitwise AND 1 & 0 0
| Bitwise OR 1 | 0 1
~ Bitwise NOT ~1 -2
^ XOR 1^2 3
<< Left shift 11<<2 44
>> Right shift 11>>2 2