BlogsDope image BlogsDope

Python Operators

April 23, 2019 PYTHON OPERATOR 232

This article is an extension of the Boolean chapter of the Python course.

We already know that operators are used to perform some operations on variables and values and those variable or values are called operands. In this article we will look at different operators available in Python and their examples.

There are different types or groups of operators in Python and those are:

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

So, let’s discuss each of these one by one.

Arithmetic Operators


Arithmetic operators are used to perform various arithmetic operations like addition, multiplication, division, etc. Here is the list of arithmetic operators:

Operator Name Description
+ Addition Performs addition on two operands
- Subtraction Performs subtraction on two operands
* Multiplication Performs multiplication on two operands
/ Division Performs division on two operands
// Floor Division Gives floor of the result of division of two operands
% Modulus Gives remainder after division
** Exponentiation a**b calculate ab

Let’s look at an example to see working of each of these.

print (5+2)
print (5-2)
print (5*2)
print (5/2.0)
print (5//2)
print (5**2)
print (5%2)

7
3
10
2.5
2
25
1

Assignment Operators


Assignment operators are used to assign values to variables. For example, = is an assignment operator which assigns the value at the right side of it to the variable at the left side of it i.e., x = y will make the value of x equal to the value of y and not vice versa. 

+= is an another assignment operator which adds the value at the right side to the left side and assigns to the variable at the left side i.e., x += y is same as x = x+y.

In the expression x = x+y, we have two operators, = and +. The + operator has higher precedence than = operator (precedence chart is given at the last of this post), so x+y will be calculated first and then = will make the value of x equal to that summation. For example,

x = 5
y = 2
x = x+y

Here, x+y is calculated first (5+2 = 7) and then = assigned this value to x i.e., x = 7. So, x += y will also make the value of x equal to 7 because it is same as x = x+y.

Take a note that this is little different from regular mathematics.

Similarly, we have -= (x -= y is same as x = x-y), *= (x*=y is same as x = x*y), etc. operators. Some of the important assignment operators are:

Operator Same as
+= x+=y is same as x = x+y
-= x-=y is same as x = x-y
*= x*=y is same as x = x*y
/= x/=y is same as x = x/y
//= x//=y is same as x = x//y
**= x**=y is same as x = x**y
%= x%=y is same as x = x%y
x = 5
y = 2
x += y
print(x)
x -= y
print(x)
x *= y
print(x)
x /= y
print(x)
x //= y
print(x)
x **= y
print(x)
x %= y
print(x)

7
5
10
5.0
2.0
4.0
0.0

Comparison Operators


Comparison operators are used to compare the values of the operands. Let’s have a look at them.

Operator Name Description
== Equal a==b is true is a and b have same value
> Greater than a>b is true is a if greater than b
< Less than a<b is true if a is less than b
>= Greater than or equal to a>=b is true if a is either greater than or equal to b
<= Less than or equal to a<= is true if a is either smaller than or equal to b
!= Not equal a!=b is true if a and b have different values
print(5==2)
print(5>2)
print(5<2)
print(5>=2)
print(5<=2)
print(5!=2)

False
True
False
True
False
True

Logical Operators


The and, or and not operators discussed in the Boolean chapter are logical operators.

Operator Description
and a and b is True if both a and b are True
or a or b is True if either of a and b is True
not not(a) reverses the value of a i.e., True becomes False and vice versa
print(True and False)
print(True or False)
print(not(True))

False
True
False

Identity Operators


We know that each variable is assigned a memory in our computer. Identity operators check if the memory location of two objects is same or not i.e., they are same or not (not just values).

Operator Description
is True if both the operands are same
is not True if both the operands are different
x = [5, 2]
y = [5, 2]
z = y

print(z is y)
print(x is z)
print(x == y)
print(x is not y)

True
False
True
True

x and y are lists with same value but different memory locations. z and y are the same i.e., with the same value and same memory location also. So, z is y is true.

If you are reading this post after the boolean chapter, you will read about lists in later chapters.

Membership Operators


Membership operators are used to check if a value is present in a sequence like tuple, list, etc. or not.

Operator Description
in True if the value is present in the sequence
not in True if the value is not present in the sequence
x = (5, 2)
print(5 in x)
print(10 not in x)

True
True

We have only discussed some important operators in this course, you can check https://python-reference.readthedocs.io/en/latest/docs/operators/ for more.

Precedence Order


In the case of an expression with multiple operations like, x = 5+3/2-1, the order of execution of operators is decided by precedence order given below (highest priority at top and least at bottom).

**
*, /, //, %
+, -
<=, <, >, >=
=, %=, /=, //=, -=, +=, *=, **=
is, is not
in, not in
and, or, and

In the case of have same precedence, the expression is evaluated from left to right.

For example, in the expression, x = a+b-c, + and - have higher precedence than = but also equal, so a+b-c will be evaluated first from left to right i.e., first b-c will be evaluated and then its result will be added to a and at last, = operator set the value of x to the final result.


Liked the post?
Developer and founder of CodesDope.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).