Basic Operators in Python

Operators are used to manipulate variables and values by performing operations on them. They are an essential part of any programming language. Python has the following types of operators:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Let’s look at the details of these operator types one by one.

Arithmetic operators

These are used to perform simple mathematical operations on numerals.

Note: Let’s assume x = 20 and y = 40 for examples.

OperatorNameDescriptionExample
+AdditionAdds two numbersx + y = 60
SubtractionSubtracts two numbersx – y = -20
*MultiplicationMultiplies two numbersx * y = 800
/DivisionDivides two numbersy / x = 2
%ModulusDivides left side operand by right side operand and returns remaindery % x = 0
**ExponentiationReturns left hand operator raised to the power right hand operatorx ** y = 20 raised to the power 40
//Floor divisionIf the answer of division is a decimal number, it returns its floor (the lowest possible number after round-off) 7 // 3 = 2, -7 // 3 = -3

Assignment operators

These are used to assign values to variables.

OperatorDescriptionExample
=Assigns right side operand to left sidex = 5
+=Adds right side operand to left side and stores result in left side operandx += 3
-=Subtracts right side operand to left side and stores result in left side operandx -= 3
*=Multiplies right side operand by left side and stores result in left side operandx *= 3
/=Divides left side operand by right side and stores result in left side operandx /= 3
%=Performs modulus between both operands and stores result in left side operandx %= 3
//=Performs floor division between both operands and stores result in left side operandx //= 3
**=Performs exponentiation between both operands and stores result in left side operandx **= 3

Comparison operators

These are used to compare two variables and are commonly used in while loops or conditionals.

Note: Let’s assume x = 20 and y = 40 for examples.

OperatorDescriptionExample
==Returns true if the values of two operands are equal(x == y) is not true
!=Returns true if values of two operands are not equal(x != y) is true
<>Returns true if values of two operands are not equal(x <> y) is true
>Returns true if the value of left operand is greater than the value of right operand(x > y) is not true
<Returns true if the value of left operand is less than the value of right operand(x < y) is true
>=Returns true if the value of left operand is greater than or equal to the value of right operand(x >= y) is not true
<=Returns true if the value of left operand is less than or equal to the value of right operand(x <= y) is true

Logical operators

Following logical operators are supported by Python:

Note: Let’s assume a = True and b = False for examples.

OperatorDescriptionExample
and Returns True if both statements are true(a and b) is False
orReturns True if one of the statements is true(a or b) is True
notReverses the resultnot(a and b) is True

Identity operators

These are used to compare the objects returning whether they are actually the same object with the same memory location.

OperatorDescriptionExample
is Returns True if both variables are the same object(x is y) is False
is notReturns True if both variables are not the same object(x is not y) is True

Membership operators

These are used to test if an element is present in a sequence (list, array, tuple) or not.

Note: Let’s assume p = 2 and q = [0, 2, 4, 6, 8] for examples.

OperatorDescriptionExample
in Returns True if it finds a variable in the specified sequence(p in q) is True
not inReturns True if it doesn’t find a variable in the specified sequence(x not in y) is False

Bitwise operators

These operators work on bits and perform bit by bit operation on binary numbers.

Note: Let’s assume u = 0011 1100 and v = 0000 1101 for examples.

OperatorNameDescriptionExample
ANDSets each bit to 1 if both bits are 1u & v = 0000 1100
|ORSets each bit to 1 if one of two bits is 1u | v = 0011 1101
^XORSets each bit to 1 if only one of two bits is 1u ^ v = 0011 0001
NOTInverts all the bits~ u = 1100 0011
<<Left shiftShift left by pushing zeros in from the right and removes the leftmost bitsu << 2 = 1111 0000
>>Right shiftShift right by pushing copies of the leftmost bit in from the left, and removes the rightmost bits u >> 2 = 0000 1111

Operator Precedence

The following table lists operators in the order of their precedence in Python:

No.Operator
1Exponentiation
2Complement, unary plus and minus
3Multiply, divide, modulo and floor division
4Addition and subtraction
5Right and left bitwise shift
6Bitwise ‘AND’
7Bitwise exclusive ‘OR’ and regular ‘OR’
8Comparison operators
9Equality operators
10Assignment operators
11Identity operators
12Membership operators
13Logical operators

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest