January 25, 2021
Operators in Python

Python – Operators

Just as you know, operators are symbols used to perform various operations and get results. In programming terms we say operator work on operands. We are going to consider a number of operators used in Python.


 

Types of Operators in Python

There are 7 types of operators supported by the Python programming language as listed below:

  • Arithmetic Operator
  • Comparison Operator
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

So we begin with the first one – Arithmetic Operators


 

Python Arithmetic Operator

Arithmetic operators just as the name indicates are operators used to perform arithmetic operations in Python. The table below outlines the arithmetic operators and their description. It also includes an example when x = 10 and y = 20

OperatorDescriptionExample
+ AdditionAdds values on either side of the operator.a + b = 30
– SubtractionSubtracts right hand operand from left hand operand.a – b = -10
* MultiplicationIt multiplies values on either side of the operatora * b = 200
/ DivisionIt divides left hand operand by right hand operandb / a = 2
% ModulusIt divides left hand operand by right hand operand and returns remainderx % y = 0
** ExponentIt performs exponential (power) calculation on operatorsx**y =10 to the power 20
//Known as Floor Division – The division of operands where the result is the quotient in which the digits after the decimal point are truncated. If however one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) −9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0

 

Python Comparison/Relational Operators

Comparison Operators are used to compare two values on either side of the operator to see is they are equal or one is greater or less than the other. Comparison Operators are also known as Relational Operators.

The table below gives a list and description of Python Comparison operators and an example for when x =10 and y = 20

OperatorDescriptionExample
==If the values of two operands are equal, then the condition evaluates to true.(x == y) is not true.
!=If values of two operands are not equal, then condition evaluates to true.(x != y) is true.
<>If values of two operands are not equal, then condition evaluates to true.(x <> y) is true. This is similar to != operator.
>If the value of left operand is greater than the value of right operand, then condition evaluates to true.(x > y) is not true.
<If the value of left operand is less than the value of right operand, then condition evaluates to true.(x < y) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition evaluates to true.(x >= y) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition evaluates to true.(x <= y) is true.

 

 

Python Assignment Operators

Assignment operators are used to either assign a new value to a variable or to change the value of a variable to another value. The table below summarizes the result for x = 10 and y = 20

OperatorDescriptionExample
=Assigns values from right side operands to left side operandz = x + y assigns value of x + y into z
+= Add ANDAdds the right operand to the left operand and assigns the result to left operandz += x is equivalent to z = z + x
-= Subtract ANDSubtracts the right operand from the left operand and then assigns the result to left operandz -= x is equivalent to z = z – x
*= Multiply ANDMultiplies right operand with the left operand and then assign the result to left operandz *= x is equivalent to
z = z * x
/= Divide ANDDivides left operand with the right operand and assigns the result to left operandz /= x is equivalent to z = z / xz /= x is equivalent to z = z / x
%= Modulus ANDTakes modulus using two operands and assign the result to left operandz %= x is equivalent to
z = z % x
**= Exponent ANDIt performs exponential calculation on operators and assign value to the left operandz **= x is equivalent to z = z ** x
//= Floor DivisionPerforms floor division on operators and assign value to the left operandz //= x is equivalent to
z = z // x

 

 

Python Bitwise Operators

Bitwise operators are special operators that are applied on binary number. The operation is carried out bit-by-bit. So given two number in decimal x = 60 and y = 13. We convert to the binary equivalent and apply the bitwise operation

x = 00111100

y = 00001101

x & y = 00001100

x | y = 00111101

x ^ y = 00110001

~ x = 11000011

The table below gives a summary of Python Bitwise operators and their description

OperatorDescriptionExample
& (Binary AND)Produces a 1 if both bits are 1(x & y) (means 0000 1100)
| (Binary OR)Produces a 1 is there is 1 in either of the operands(x | y) = 61 (means 0011 1101)
^ (Binary XOR)Copies the bit if it is set in one operand but not in both.(x ^ y) = 49 (means 0011 0001)
~ (Binary Ones Complement)Unary which has the effect of ‘flipping’ bits. Chanes 1 to 0 and 0 to 1(~x ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number.
<< (Binary Left Shift)Move the left operand to the left by the number of bits specified by the right operand.x << 2 = 240 (means 1111 0000)
>> (Binary Right Shift)Moves the left operand to the right by the number of bits specified by the right operand.x >> 2 = 15 (means 0000 1111)

 

 

Python Logical Operators

Logical operators are used to perform logical comparison on operands. The table below summarizes the three Python logical operators for x = 10 and y = 20

OperatorDescriptionExample
and (Logical AND)If both the operands are true then condition becomes true.(x and y) is true.
or (Logical OR)If any of the two operands are non-zero then condition becomes true.(x or y) is true.
not (Logical NOT)Reverses the state of the given operand.Not(x and y) is false.

 

 

Python Membership Operators

Membership operators are used to check is a given variable is a member of some collection such as list, string, tuple or dictionary. The two membership operators in Python are summarized below.

OperatorDescriptionExample
inBecomes true if it finds a variable in the given sequence and false if not.a in b, here in results in 1 if a is a member of sequence b.
not inBecomes true if it does not finds a variable in the given sequence and false if otherwise.a not in b, here not in results in a 1 if a is not a member of sequence b.

 

 

Python Identity Operators

Identity operators are applied on Python objects. It compares the memory location of two objects. The two identity operators in Python are summarized below

OperatorDescriptionExample
isResolves to true if the variables on both sides of the operator point to the same object in memory and false if not.a is b, here is results in 1 if id(a) equals id(b).
is notResolves to false if variables on either side of the operator point to the same object and true if not.a is not b, here is not results in 1 if id(a) is not equal to id(b).

 

Watch the Video

0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Lesson Outline 1. Introduction to Python Programming 2. Downloading and Installing Python in Your Computer 3. Write Your First Python Program 4. Variables and Data Types in Python 5. Operators in Python […]