Python Operator – Types of Operators in Python

Python course with 57 real-time projects - Learn Python

In this Python Operator tutorial, we will discuss what is an operator in Python Programming Language.

We will learn different types of Python Operators:  Arithmetic,  Relational,  Assignment, Logical, Membership, Identity, and Bitwise Operators with their syntax and examples.

So, let’s start the Python Operator Tutorial.

Python Operator - Types of Operators in Python

Python Operator – Types of Operators in Python

What is Python Operator?

Python operator is a symbol that performs an operation on one or more operands. An operand is a variable or a value on which we perform the operation.

Python Operator falls into 7 categories:

  • Python Arithmetic Operator
  • Python Relational Operator
  • Python Assignment Operator
  • Python Logical Operator
  • Python Membership Operator
  • Python Identity Operator
  • Python Bitwise Operator

1. Arithmetic Operators in Python

These Python arithmetic operators include Python operators for basic mathematical operations.

Python Operator

Arithmetic Operators in Python

a. Addition(+)

Adds the values on either side of the operator.

>>> 3+4

Output

7

CHECK YOUR KNOWLEDGE – How to use + Operator for concatenation?

Comment, if you know the answer, else check the article – Frequently asked Python Interview Questions

b. Subtraction(-)

Subtracts the value on the right from the one on the left.

>>> 3-4

Output

-1

 

c. Multiplication(*)

Multiplies the values on either side of the operator.

>>> 3*4

Output

12

d. Division(/)

Divides the value on the left by the one on the right. Notice that division results in a floating-point value.

>>> 3/4

Output

0.75

e. Exponentiation(**)

Raises the first number to the power of the second.

>>> 3**4

Output

81

f. Floor Division(//)

Divides and returns the integer value of the quotient. It dumps the digits after the decimal.

>>> 3//4
>>> 4//3

Output

1
>>> 10//3

Output

3

g. Modulus(%)

Divides and returns the value of the remainder.

>>> 3%4

Output

3
>>> 4%3

Output

1
>>> 10%3

Output

1
>>> 10.5%3

Output

1.5

If you face any query in Python Operator with examples, ask us in the comment.

2. Python Relational Operator

Python Operator

Relational Operators in Python

Relational Python Operator carries out the comparison between operands.

They tell us whether an operand is greater than the other, lesser, equal, or a combination of those.

a. Less than(<)

This operator checks if the value on the left of the operator is lesser than the one on the right.

>>> 3<4

Output

True

b. Greater than(>)

It checks if the value on the left of the operator is greater than the one on the right.

>>> 3>4

Output

False

c. Less than or equal to(<=)

It checks if the value on the left of the operator is lesser than or equal to the one on the right.

>>> 7<=7

Output

True

d. Greater than or equal to(>=)

It checks if the value on the left of the operator is greater than or equal to the one on the right.

>>> 0>=0

Output

True

e. Equal to(= =)

This operator checks if the value on the left of the operator is equal to the one on the right.

1 is equal to the Boolean value True, but 2 isn’t. Also, 0 is equal to False.

>>> 3==3.0

Output

True
>>> 1==True

Output

True
>>> 7==True

Output

False
>>> 0==False

Output

True
>>> 0.5==True

Output

False

f. Not equal to(!=)

It checks if the value on the left of the operator is not equal to the one on the right.

The Python operator <> does the same job, but has been abandoned in Python 3.

When the condition for a relative operator is fulfilled, it returns True. Otherwise, it returns False. You can use this return value in a further statement or expression.

>>> 1!=1.0

Output

False
>>> -1<>-1.0

#This causes a syntax error

3. Python Assignment Operator

Python Operator

Assignment Operators in Python

Python assignment operator assigns a value to a variable. It may manipulate the value by a factor before assigning it.

We have 8 assignment operators- one plain, and seven for the 7 arithmetic python operators.

a. Assign(=)

Assigns a value to the expression on the left. Notice that = = is used for comparing, but = is used for assigning.

>>> a=7
>>> print(a)

Output

7

b. Add and Assign(+=)

Adds the values on either side and assigns it to the expression on the left. a+=10 is the same as a=a+10.

The same goes for all the next assignment operators.

>>> a+=2
>>> print(a)

Output

9

c. Subtract and Assign(-=)

Subtracts the value on the right from the value on the left. Then it assigns it to the expression on the left.

>>> a-=2
>>> print(a)

Output

7

d. Divide and Assign(/=)

Divides the value on the left by the one on the right. Then it assigns it to the expression on the left.

>>> a/=7
>>> print(a)

Output

1.0

e. Multiply and Assign(*=)

Multiplies the values on either sides. Then it assigns it to the expression on the left.

>>> a*=8
>>> print(a)

Output

8.0

DON’T MISS!! Top Python Projects with Source Code

f. Modulus and Assign(%=)

Performs modulus on the values on either side. Then it assigns it to the expression on the left.

>>> a%=3
>>> print(a)

Output

2.0

g. Exponent and Assign(**=)

Performs exponentiation on the values on either side. Then assigns it to the expression on the left.

>>> a**=5
>>> print(a)

Output

32.0

h. Floor-Divide and Assign(//=)

Performs floor-division on the values on either side. Then assigns it to the expression on the left.

>>> a//=3
>>> print(a)

Output

10.0

This is one of the important Python Operator.

4. Python Logical Operator

These are conjunctions that you can use to combine more than one condition.

We have three Python logical operator – and, or, and not that come under python operators.

Python Operator

Logical Operators in Python

a. and Operator in Python

If the conditions on both sides of the operator are true, then the expression as a whole is true.

>>> a=7>7 and 2>-1
>>> print(a)

Output

False

b. or Operator in Python

The expression is false only if both the statements around the operator are false. Otherwise, it is true.

>>> a=7>7 or 2>-1
>>> print(a)

Output

True

‘and’ returns the first False value or the last value; ‘or’ returns the first True value or the last value

>>> 7 and 0 or 5

Output

5

c. not Operator in Python

This inverts the Boolean value of an expression. It converts True to False, and False to True.

As you can see below, the Boolean value for 0 is False. So, not inverts it to True.

>>> a=not(0)
>>> print(a)

Output

True

5. Membership Python Operator

These operators test whether a value is a member of a sequence. The sequence may be a list, a string, or a tuple.

We have two membership python operators- ‘in’ and ‘not in’.

a. in Operator in Python

This checks if a value is a member of a sequence.

In our example, we see that the string ‘fox’ does not belong to the list pets. But the string ‘cat’ belongs to it, so it returns True.

Also, the string ‘me’ is a substring to the string ‘disappointment’. Therefore, it returns true.

>>> pets=[‘dog’,’cat’,’ferret’]
>>> ‘fox’ in pets

Output

False
>>> ‘cat’ in pets

Output

True
>>> ‘me’ in ‘disappointment’

Output

True

b. not in Operator in Python

Unlike ‘in’, ‘not in’ checks if a value is not a member of a sequence.

>>> ‘pot’ not in ‘disappointment’

Output

True

In doubt yet in any Python operator with examples? Please comment.

6. Python Identity Operator

Let us proceed towards identity Python Operator.

These operators test if the two operands share an identity. We have two identity operators- ‘is’ and ‘is not’.

a. is Operator in Python

If two operands have the same identity, it returns True. Otherwise, it returns False. Here, 2 is not the same as 20, so it returns False.

Also, ‘2’ and “2” are the same. The difference in quotes does not make them different. So, it returns True.

>>> 2 is 20

Output

False
>>> ‘2’ is “2”

Output

True

b. is not Operator in Python

2 is a number, and ‘2’ is a string. So, it returns a True to that.

>>> 2 is not ‘2’

Output

True

7. Python Bitwise Operator

Let us now look at Bitwise Python Operator.

Python Operator

Bitwise Operators in Python

On the operands, these operate bit by bit.

a. Binary AND(&) Operator in Python

It performs bit by bit AND operation on the two values. Here, binary for 2 is 10, and that for 3 is 11. &-ing them results in 10, which is binary for 2.

Similarly, &-ing 011(3) and 100(4) results in 000(0).

>>> 2&3

Output

2
>>> 3&4

Output

0

b. Binary OR(|) Operator in Python

It performs bit by bit OR on the two values. Here, OR-ing 10(2) and 11(3) results in 11(3).

>>> 2|3

Output

3

c. Binary XOR(^) Operator in Python

It performs bit by bit XOR(exclusive-OR) on the two values. Here, XOR-ing 10(2) and 11(3) results in 01(1).

>>> 2^3

Output

1

d. Binary One’s Complement(~) in Python

It returns the one’s complement of a number’s binary. It flips the bits. Binary for 2 is 00000010. Its one’s complement is 11111101.

This is binary for -3. So, this results in -3. Similarly, ~1 results in -2.

>>>~-3

Output

2

Again, one’s complement of -3 is 2.

e. Binary Left-Shift(<<) Operator in Python

It shifts the value of the left operand the number of places to the left that the right operand specifies.

Here, binary of 2 is 10. 2<<2 shifts it two places to the left. This results in 1000, which is binary for 8.

>>> 2<<2

Output

8

f. Binary Right-Shift(>>) in Python

It shifts the value of the left operand the number of places to the right that the right operand specifies.

Here, binary of 3 is 11. 3>>2 shifts it two places to the right. This results in 00, which is binary for 0.

Similarly, 3>>1 shifts it one place to the right. This results in 01, which is binary for 1.

>>> 3>>2
>>> 3>>1

Output

1

This was all about the Python Operator Tutorial.

Python Interview Questions on Python Operators

  1. What is not operator in Python?
  2. Explain relational operators in Python?
  3. What does != Mean in Python?
  4. Explain types of Bitwise Operators in Python
  5.  Explain Floor-Divide and Assign Operator in Python

Conclusion

Finally, in this lesson, we looked at seven different classes of Python operator.

We executed them in the Python Shell(IDLE) to find out how they work. We can further use this operator in conditions, and to combine them.

Go ahead and practice some combinations.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

54 Responses

  1. Dr D J Nagendra Kumar says:

    You forgot to mention modulus % operator in arithmetic operators section.
    Thank you
    Dr Nagendra
    [email protected]

    • Data Flair says:

      Thank You
      Dr. Nagendra for taking the time to share the important information with us about “Python Operators”.
      Soon we will update our content.

  2. Nick P says:

    How do you reconcile
    >>> test_dictionary = {1:’a’, 2:’b’, 3:’c’, 4:’d’}
    >>> ‘d’ in test_dictionary
    False
    >>>
    with “This checks if a value is a member of a sequence.”.
    ‘d’ seems to be a value in the sequence!

    • Data Flair says:

      Hi Nick,
      We are happy to help you!
      For a Python dictionary, this will only test if a certain key is a member.
      In your example, ‘d’ is a value in the dictionary, but isn’t a key. The following code would return True:
      >>> 4 in test_dictionary
      True
      For what you want, you can try this-
      >>> ‘d’ in test_dictionary.values()
      True”
      We Can Also Refer Our Blog on Python Dictionaries

    • Tejas Shetty says:

      Maybe it would work if you use test_dictionary.values

      • Data Flair says:

        Yes Tejas, it does.
        As we mentioned in our response above, searching in test_dictionary.values() gives us whether ‘d’ exists in it as a value.
        Hope it helps.
        Thank you for visiting Data Flair

  3. Mukesh Thakur says:

    Hello Team,
    Your both logical and, or statements are wrong please check them

    • Data Flair says:

      Hi Mukesh,
      Thank you for pointing out the ambiguity in the text of Python Operator. We have made the corrections.
      Hope it makes more sense now!

  4. Peter says:

    The != operator example looks incorrect to me?
    >>> 1!=-1.0
    False
    one is not equal to minus one, so must be a true statement?
    Peter

    • DataFlair Team says:

      Hello, Peter.
      We appreciate your feedback, and we would glad to inform you that we have rectified our mistake. The example provided, 1!=1.0 or 1!=-1.0, is indeed false. The output for 1!=1.0 is False, while the output for 1!=-1.0 is True. We kindly request you to reexamine the code and run it again.

  5. Shrey Tyagi says:

    “1 and 1” gives 1 makes sense. But 20 and 30 gives 30. What’s the logic behind it?

    • Shashank Naresh says:

      With non zero numbers, number at right will be output. If any one of the numbers is zero then zero will be output

      • DataFlair Team says:

        Thank you, Shashank for giving this superb suggestion. Hope you have read our Python Operators blog. It seems that you have a good interest in Python. Check our latest blogs on Python. Your feedback means a lot to us.
        Also, tell us if you need any new blog on Python. We will definitely get it to you.

    • DataFlair Team says:

      Hello Shrey,

      Shashank is correct! Between two numbers joined by an ‘and’, if the one on the left is 0, it doesn’t evaluate the one on the right. However, if non-zero, it does evaluate the one on the right, and returns the same. This explains the following pieces of code:

      >>> 0 and 0
      0
      >>> 0 and 1
      0
      >>> 1 and 0
      0
      >>> 1 and 2
      2
      >>> 2 and 1
      1
      >>> 1 and 1
      1
      >>> 20 and 30
      30

      Hope this clears it for you!
      Keep connected with us.

  6. Abdul MALIK says:

    Sir, you are providing extraordinary content.
    My humble request is can you please provide this entire data in a pdf format.
    I sent my email id to you.
    If pdf is available you can send it to my mail.
    Thank You

  7. Damodar says:

    256 is 254+2, 257 is 255+2, 256 is 256, 257 is 257 # (True, False, True, True)
    ‘abc’ is ‘ab’+’c’, ‘abc’ is ‘ab’+’c’, ‘ab-c’ is ‘ab’+’-c’, ‘ab-c’ is ‘ab’+’-c’ # (True, True, False, False)
    # why o/p is different for 256 is 254+2, 257 is 255+2 and same for string
    # if so, why should we use is operator with numbers and strings

    • DataFlair Team says:

      Hi Damodar,
      There seems to be a problem with the outputs you mentioned. 257 is 255+2 certainly returns True.

      >>> 257 is 255+2
      True
      Thanks for reading

      • Rakesh says:

        [256 is 254+2, 257 is 255+2, 256 is 256, 257 is 257] actually gives [True, False, True, True]
        Can you please try this and then come out with the rationale behind it?

        • Rakesh Raushan says:

          Got the answer!
          From documentation,
          The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.
          So basically they check if both are pointing to same memory location or not. Python keeps some int (-5 to 256) mostly already in memory and whenever these numbers are used another instance is not created but the same object is used. Hence, is operated to True for these while false for others.

          To get a hold, try this:
          a = 256
          b= 254+2
          print(id(a),id(b))

          and then

          a= 257
          b = 255+2
          print(id(a),id(b))

          and see how different instances are created for numbers beyond 256.

          Moral of the story for int comparisions use == or != instead of is or is not operator!!

      • Rakesh Raushan says:

        Got the answer!
        From documentation,
        The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.
        So basically they check if both are pointing to same memory location or not. Python keeps some int (-5 to 256) mostly already in memory and whenever these numbers are used another instance is not created but the same object is used. Hence, is operated to True for these while false for others.

        To get a hold, try this:
        a = 256
        b= 254+2
        print(id(a),id(b))

        and then

        a= 257
        b = 255+2
        print(id(a),id(b))

        and see how different instances are created for numbers beyond 256.

        Moral of the story for int comparisions use == or != instead of is or is not operator!!

    • Rakesh says:

      Got the answer!
      From documentation,
      The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.
      So basically they check if both are pointing to same memory location or not. Python keeps some int (-5 to 256) mostly already in memory and whenever these numbers are used another instance is not created but the same object is used. Hence, is operated to True for these while false for others.

      To get a hold, try this:
      a = 256
      b= 254+2
      print(id(a),id(b))

      and then

      a= 257
      b = 255+2
      print(id(a),id(b))

      and see how different instances are created for numbers beyond 256. Same is true for common alphabets

      Moral of the story for int comparisions use == or != instead of is or is not operator!!

    • Jigyasa says:

      Hello
      In the arithmetic Operations section under modulus
      I think the output is wrong because the remainder is 0. So the output should also be 0.

  8. Ravi Teja says:

    And operator :

    Return the first false value ; if not found return last
    3 and 5 and 6 and 7: 7
    0 and 3 and 9 and 10: 0

    Or operator :

    Returns the first true value; if not found returns last

    3 or 5 or 0 or 10 : 3
    0 or 5 or 6 or 7 : 5

    • DataFlair Team says:

      Hello, Ravi
      Absolutely. We have implemented this concept in our tutorial on ternary operators in Python, you check it in our sidebar.

  9. Vaishnavi singh says:

    -21%2what is the output and why

    • DataFlair Team says:

      Hello, Vaishnavi
      Thanks for connecting with DataFlair, the output of your query will be 1. Because even though it’s negative, 2 divides -21 by 11 and leaves a positive remainder of 1.

      divmod(-21,2) is (-11, 1)
      If you still don’t get it then revise Modulus Operator in Python.
      Hope, it helps!

  10. Anna says:

    what does the ‘ mean in python? as in:
    y = int(3 * ‘4’)

    • DataFlair Team says:

      Hey Anna,
      When we put 4 in quotes, it means it is a string and not an integer. Multiplying it by 3 gives us the string ‘444’. And then, calling int() on it gives us the integer 444.
      Hope, it helps!

  11. Semih says:

    Hello,
    I couldnt get this “7 and 0 or 5″ its 5, could you explain a bit?
    in the above examples, i found a little mistake ” 1!=-1.0″ its written “false”, but its “true”
    Thank you.

    • DataFlair Team says:

      Hello Semih, ‘
      Let’s break it down.
      7 and 0.
      7 is True, but this is ‘and’, so it returns the second value, which is 0.
      Now, 0 or 5.
      0 is False, but since we have ‘or’, it returns the second value, which is 5.
      Hope, it helps!

  12. Naveen Gupta says:

    9%4 is 1
    -9%4 is 3

    why is the difference?

    • DataFlair Team says:

      Hi Naveen
      9%4 is 1- this is simple; 4 divides 9 by 2 and leaves a remainder of 1.
      When we talk of -9%4, -12 perfectly divides 4, and -12 is at a distance of 3 from -9.
      Hope, it helps!

  13. Fekih says:

    1!=-1.0
    Is True not False

    • DataFlair Team says:

      Hey Fekih
      Thank you for the comment, but it is false as the example given is 1!=1.0 nor 1!=-1.0,
      so 1!=1.0 output is False and 1!=-1.0 output is true.Check and run the code again.

  14. Ankit Garg says:

    There is no problem with output. I have check above and found
    257 is 255+2 is False.
    But 3 is 2 + 1 is True.
    Because ‘is’ operator check if both objects are the same. In the first case, both are not the same.
    But is the second case both are the same. Python use cache concept for a smaller object to load data faster so it is possible 257 is 255+2 would be true

    • DataFlair says:

      Actually we get True for both the operations. This is becase 257 is same as 255+2 and 3 is same as 2+1. But we might get a warning in some IDEs asking to use ‘==’ as we are trying to compare the addresses of constants rather than comparing variables.

  15. JuanP says:

    3//4 is equal to 0, not to 1

    • DataFlair says:

      Yes, you are correct 3//4 is 0. But in the example, the output is given to the 2nd operation, i.e. 4//3. We will add the output of 3//4 as well in the example. Thank you for noticing and letting us know. Hope you enjoyed reading this article.

  16. Janmejaya says:

    h. Floor divide and Assign (//=), the answer should be 10 and not 10.0.
    Please correct it.

    • DataFlair says:

      You are correct, the output should be 10 and not 10.0. Thank you for correcting us. This helps us and also other readers. Happy learning!

  17. Summer says:

    what does the # mean in python?

  18. Vitaly Chait says:

    Please correct the output in the example for ” // “(floor division)

    Example shown as:
    3//4
    4//3

    output:1

    Should either give an output for 3//4, which is 0
    or should remove tat example

  19. Lucky says:

    Can u explain detail in bitwise operators

    • DataFlair says:

      Python Bitwise Operators take one to two operands, and operates on it/them bit by bit, instead of whole. Here are most common bitwise operators: AND, OR, XOR, 1’s COMPLEMENT, LEFT-SHIFT, RIGHT-SHIFT

  20. aria says:

    what does 2++2 equals in python
    2–2
    3-+3
    3+-3

  21. Aswini says:

    Consider a list1 [3,4,5,6,7,8]. How to create a new list2 such that Add 10 to the even number and multiply with 5 if it is odd number in the list1?

    • DataFlair says:

      We can create an empty list and use for loops and conditional to do the operations. I am also adding the code for this: list1=[3,4,5,6,7,8]
      list2=[] #creating an empty list

      for i in list1: #running for loop for every element in list1
      if(i%2): #checking is element is odd
      list2.append(i*5) #appending 5*element to list2
      else: #if element is even
      list2.append(10+i) #adding 10 to the element and appending it to list2
      print(list2) Hope this code is clear to you

  22. Muskan Kushwah says:

    What is the exact difference between bitwise and logical and , or operators /

    >> And if bitwise operators operate bit by bit , then does it operate only on numbers or integers ?

  23. sahana T H says:

    I also have same doubt. Please explain me

  24. Biology by K. DAHIYA says:

    sir, I have checked the below-mentioned code in the online GDB Python compiler.
    a=10
    b=10
    c=10
    print(a==b==c) # here output is true
    so kindly confirm which platform is best for understanding the actual or accurate Python concept for the exam.

  25. JAY SORATHIYA says:

    sir please you say for neg bit last number is 1 and for positive last bit is 0.but in video you take example you do reverse.so there is lots of correction required.so lots of confusion create please do need full.

Leave a Reply

Your email address will not be published. Required fields are marked *