Python Operators

Python Operators
••• Python Operators

In this tutorial, you will learn about Python Operators and their types. Also, we will discuss their operational functionalities with examples.

Python Operators

A Python Operator is a symbol that tells the interpreter to perform certain mathematical or logical manipulation. In simple terms, we can say Python operators are used to manipulating data and variables. In programming universe, operators represent computations and conditional resemblance.

Python operators can be classified as listed below.

Python Arithmetic Operators

As the name suggests, Arithmetic Operators are used in Arithmetic (Mathematics) operations.

Let’s assume following two variables:

  • x = 5
  • y = 2
OperatorName & MeaningExample & Result
+Addition: Add two operands or unary plusx + y = 7
Subtraction: Subtract right operand from the leftx – y = 3
*Multiplication: Multiplies two operandsx * y = 10
/Division: Divides left operand by the right onex / y = 2.5
%Modulus – Takes the remainderx % y = 1
//Floor division: Division that results into the whole number truncating digits after decimal pointx // y = 2
**Exponent – left operand raised to the power of rightx**y (5^2) = 25

Example demonstrating use of Python Arithmetic operator

Python Operators

Python Relational Operator or Comparison Operator

Relational or Comparison Operators are used to compare the operands on the either side of them.

>Greater than: Eg. x > y will return true if x is greater than y
<Less than: Eg. x < y will return true if x is less than y
==Equal to: Eg. x == y will return true if x is equal to y
!=Not equal to: x != y will return true if x is not equal to y
>=Greater Than or Equal to: Eg. x >= y will return true if x is greater than or equal to y
<=Less Than or Equal to: Eg. x <= y will return true if x is less than or equal to y

Example demonstrating use of Python Logical operator


Python Assignment Operators

As simple as it sounds assignment operators are used for assigning values to variables.

=x = 1Assigns 1 to x
+=x += 1x = x + 1
-=x -= 1x = x – 1
/=x /= 1x = x / 1
%=x %= 1x = x % 1
*=x *= 1x = x * 1
**=x **= 1x= x ** 1
//=x //= 1x = x // 1

Example demonstrating use of Python Assignment operator

Python Bitwise Operators

Bitwise operators take binary digits as operands and perform bit by bit operations.

Following is the list of bitwise operators supported in Python.

let’s assume:

  • a = 5 = 0101 (in binary)
  • b = 7 = 0111 (in binary)

Now if we were to use bitwise operator AND (&), it would generate following output.

a&b = 0101 & 0111 = 0101

Explanation: Here bitwise operator performs bit by bit operation. So, if we observe from left most bit, following operation happens.

(1st bit) 0&0 = 0, (2nd bit) 1&1 = 1, (3rd bit) 0&1 = 0, (4th bit) 1&1 = 1

Hence the output: 0101

Here is the list of bitwise operators in Python.

& (Binary AND)a & b0101 & 0111 = 0101
| (Binary OR)a | b0101 | 0111 = 0111
^ (Binary XOR)a ^ b0101 ^ 0111 = 0010
~ (Binary NOT)~ a~5 = -6  (11111010)
>> (Binary right shift)a >> 10101>>1 = 0010
<< (Binary left shift)b << 10111<<1 = 1110

Python Membership Operators

Python membership operator is used to check whether a variable is a member of a sequence such as String, List, Tuple and Dictionary.

Note: In the Python dictionary, we can check the membership of key only, not the value.

Here are the membership operators in Python.

inx in yTrue if x is the member of y
not inx not in yTrue if x is not the member of y

Example demonstrating use of Python Membership operator

Python Identity Operators

Python identity operators are used to check if the operands have identical memory location. In simple language, it compares the memory location of two objects and returns True if both objects have identical or same memory location.

Here are the identity operators in Python.

         is     x is y                  True if both variables point to same memory location
     is not    x is not y                  True if both variable point separate memory location

Example demonstrating use of Python Identity operator

So, these are the Python operators explained above with examples which are used in manipulating data either mathematically or logically.

Now that we have discussed Python operators, let’s learn about how is the precedence of operators in Python.

Precedence of Python Operators

Here is the list of Python Operators in descending order, listing from higher precedence to lower precedence.

( )     Parentheses
**     Exponent (raise to the power)
 +, -, ~     Unary plus, Unary minus and Bitwise NOT
*, /, %, //     Multiplication, Division, Modulus and Floor Division
+, –     Addition and Subtraction
>>, <<     Bitwise Right Shift and Bitwise Left Shift
&     Bitwise AND
^, |     Bitwise XOR and OR
<=, <, >, >=     Comparison Operators
==, !=     Equality Operators
=, %=, /=, //=, -=, +=, *=, **=     Assignment Operators
is, is not    Identity Operators
in, not in    Membership Operators
not, or, and    Logical Operators