Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Python Programming » Operators in Python Programming
Suryateja Pericherla Categories: Python Programming. No Comments on Operators in Python Programming
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

This article provides a comprehensive overview of operators in Python programming language along with relevant examples.

 

This article is a part of our Python programming tutorial for beginners.

 

Following are different types of operators in Python:

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

 

Arithmetic Operators

Following are various arithmetic operators available in Python:

Operator Description Example
+Add two operandsx+y = 26
Subtract one operand from anotherx-y = 14
*Multiply one operand with anotherx*y = 120
/Divide one operand with anotherx/y = 3.333
%Remainder of division between two operandsx%y = 2
**x to the power of yx**y = 64000000
//Floor division. Removes decimal part after division.x//y = 3

Note: In above examples, x = 20 and y = 6

 

Relational Operators

Following are various relational operators available in Python:

Operator Description Example
= =Returns true if values of both operands are equal . Otherwise false.x==y is false
!=Returns true if values of both operands are not equal. Otherwise false.x!=y is true
< >Returns true if values of both operands are not equal. Otherwise false.x!=y is true
>Returns true if left operand is greater than the right operand. Otherwise false.x>y is true
<Returns true if left operand is less than the right operand. Otherwise false.x<y is false
>=Returns true if left operand is greater than or equal to the right operand. Otherwise false.x>=y is true
<=Returns true if left operand is less than or equal to the right operand. Otherwise false.x<=y is false

Note: In above examples, x = 20 and y = 6

 

Assignment Operators

Following are various assignment operators available in Python:

Operator Description Example
=Assigns value of right side operand to left side operandz=x; z=20
+=Adds operand on left side with operand on right side and assigns the value to left side operand.z+=x; z=20
-=Subtracts operand on right side with operand on left side and assigns the value to left side operand.z-=x; z=-20
*=Multiplies operand on left side with operand on right side and assigns the value to left side operand.z*=x; z=0
/=Divide operand on left side with operand on right side and assigns the value to left side operand.z/=x; z=0.0
%=Assigns remainder of division to left side operand.z%=x; z=0
**=Assigns left operand power right operand to left side operand.z**=x; z=0
//=Assigns result of floor division to left side operand.z//=x; z=0

Note: In above examples, x = 20 , y = 6, and z = 0

 

Bitwise Operators

Following are various bitwise operators available in Python:

Operator Description Example
& (and)Performs bit-wise and of both operandsx&y = 1
| (or)Performs bit-wise or of both operandsx|y = 5
^ (ex-or)Performs exclusive-or of both operandsx^y = 4
~Performs 1’s complement of the operand~x = -6
<<Performs left shift of the left operand by n number of timesx<<2 = 20
>>Performs right shift of the left operand by n number of timesx>>1 = 2

Note: In above examples, x = 5 , y = 1

 

Logical Operators

Following are various logical operators available in Python:

Operator Description Example
andIf  both left side and right side expressions are true, it returns true. Otherwise, false.True and True is True
orIf either or both of left side and right side expressions are true, it returns true. Otherwise, false.True or False is True
notIf expression evaluates to true, it returns false or if the expression evaluates to false, it returns true.not True is False

 

Membership Operators

Following are the membership operators available in Python:

Operator Description Example
inEvaluates to true if it finds a variable or value in the given sequence. Otherwise, it returns true.y in x is True
not inEvaluates to true if it doesn’t find a variable or value in the given sequence. Otherwise, it returns false.y not in x is False

Note: In above examples, x = [1,2,3,4,5,6] , y = 2

 

Identity Operators

Following are the identity operators available in Python:

Operator Description Example
isEvaluates to true if both the variables point to the same object in memory.x is y returns True
is notEvaluates to true if both the variables does not point to the same object in memoryx is not y returns False

Note: In above examples, x = 10, y = x

 

Next let’s learn about expressions in Python.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory