Python Learning Part-19: Python Operators

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand, for example, 2+3 output will be 5. here + is operator, 2,3 are operands and 5 is output.

Since we have already used some of these operators by now in previous posts earlier, I will not go through examples in this one just to keep it to list of operators. I you can use these references and do your own practice and build your own examples.

Operators in python

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Special operators
  • Identity operators
  • Membership operators
Arithmetic Operators:
  • Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication

Comparison operators:
  • Comparison operators are used to compare values. 
  • It either returns True or False according to the condition.

Logical operators:
  • Logical operators are the and, or, not operators
  • They are widely used in if statements and loops.

Bitwise operators:

  • Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.
  • For example, 2 is 10 in binary and 7 is 111.
  • In the table below: x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Assignment operators:

  • Assignment operators are used in Python to assign values to variables.
  • a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
  • There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5.

Special operators:

Python language offers special type of operators like the identity operator or the membership operator.

  • Identity operators
    • is and is not are the identity operators in Python. 
    • They are used to check if two values (or variables) are located on the same part of the memory. 
    • Two variables that are equal does not imply that they are identical.
  • Membership operators
    • in and not in are the membership operators in Python. 
    • They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
    • In a dictionary we can only test for presence of key, not the value.

Leave a Reply