Home » Python Basics » Python operator

Python operator

python operators

Python operator is an essential part of python programming, which are required in almost every line of code. In another way, Python operator help in building up expressions in Python. In this comprehensive article, you will learn in detail about various operators in Python such as Arithmetic operator, Comparison operator, Assignment operator, Logical operators, Identity operators, Membership operators and Bitwise operators. To use these operators, you don’t have to import any python operator module or library, they are loaded by default with Python.


Table of Contents

  1. General Introduction to Python operator
  2. Python operators with example
    1. Arithmetic Operators
    2. Comparison Operators
    3. Assignment Operators
    4. Mixed (Bitwise and Assignment) Operator
    5. Logical Operators
    6. Identity Operators
    7. Membership Operators
    8. Bitwise Operators
  3. Conclusion

Introduction to Python Operator

After finishing our previous tutorial on Python Variable and Python Datatypes, now it’s time to understand operators in python. Operators are special symbols in Python that carry out a specific operation, depending on their type. The value that the operator operates on is called the operand. Mostly the operator finds itself between two operators. In this Python expression, operators are “+” (add) and “=” (equal), and operands are a, b, c. By the way, all these operands are also called variables. Operands could have either a fixed value (also known as constant) or a changeable value. The image below shows a typical Python operator.

python operators

In the above expression (the python statement with operators, operands and other things), “a” and “b” are added together (using “+” operator) and the result is assigned to “c” (using “=” operator).

Python operators with example

In the following section, you will learn the various type of Operators with example code that is used across Python programming. After reading through this section, you will get a deeper insight into numerous types of operators that are stacked together based on their application.

Arithmetic Operator

Python provides a group of operators under Arithmetic Operator section. Arithmetic operators are for performing mathematical operations in Python such as addition, subtraction, multiplication, modulus python etc. The table below shows the list of operators under this section with the symbol as well as the description.

Name		      Symbol	Description
Addition		+	To add two operands
Subtraction		-	To subtract two operands
Multiplication		*	To multiply to operands
Division		/	To divide two operands and result a float number
Division (floor)	//	To divide two operands and result in a whole adjusted towards the left side of the number line.
Modulus			%	Left operands get divided by the right and results in the remainder.
Exponent		**	Left operands raised to the power of right operands and provide the result.

Example with value:
Addition: x+y => 13
Subtraction: x-y => 7
Multiplication: x*y => 30
Division (float): x/y => 3.333
Division (floor): x//y => 3
Modulus: x%y => 1
Exponent: x**y => 1000

Comparison Operator or Relational Operator

Python objects may be compared using a variety of symbols that evaluate to Booleans. Result of comparison always results in True or False value.

Name          	   Symbol  	Description
Greater than	      >		 Returns True if the left operand is greater than the right, otherwise returns False
Less than	      <	 	Returns True if the left operand is less than the right, otherwise returns False
Equal to	      ==	Returns True if both operands are equal, otherwise False
Not equal to	      !=	Returns True if operands are not equal, otherwise returns False
Greater than/equal to >=	Returns True if the left operand is greater than or equal to the right, otherwise returns False
Less than/equal to    <=	Returns True if the left operand is less than or equal to the right, otherwise returns False

Note: The “=” and “==” symbols are often confused. The “=” symbol is an assignment operator (discussed in the next topic). So, x = 5 assigns the integer 5 to the x variable. The “==” symbol makes a comparison. Thus x == 5 checks to see whether x is equivalent to 5. The result of x == 5 will be True or False.

Let’s see the example for each one of these. Let’s say x is 10 and y is 3

Greater than: x > y =>True
Less than: x < y => False
Equal to: x == y => False
Not equal to: x != y => True 
Greater than or equal to: x>= y => True
Less than or equal to: x<=y => False

Assignment Operator

Assignment operators are mostly used to assign the value of one operand to another. Also, we can say assignment operators assign value to a variable. There are multiple variations of these operators, look at the table below to get more understanding.

Out of all assignment operators, we use ‘+=’ a lot, meaning += python. This symbol is equivalent to increment some value (specified) and assign back to the variable.

Name          Symbol 	Description
Equal		=	Assign value of right side of expression to left side operand
Add and		+=	Add the right side operand with left side operand and then assign to left operand
Subtract and   	-=	Subtract right operand from left operand and then assign to left operand
Multiply and    *=	Multiply right operand with left operand and then assign to left operand
Divide and	/=	Divide left operand with right operand and then assign to left operand
Modulus and   	%=	Takes modulus using left and right operands and assign the result to left operand
Divide (floor)  //=	Divide left operand with right operand and then assign the value(floor) to left operand
Exponent and	**=	Calculate exponent value using operands and assign value to left operand

Let’s see the example for each one of these.

Equal: x = 5 => 5 is assigned to x, Now x is carrying a value of 5
Add and: x += 5 => x = x+5
Subtract and: x -= 5 => x = x-5
Multiply and: x *=5 => x = x*5
Divide and: x/= 5 => x = x/5
Modulus and: x%= 5 => x = x%5
Divide (floor) and: x//=5 => x = x//5
Exponent and: x**=5 =>x= x**5



Mixed (Bitwise and Assignment) Operator

Python provides some of the mixed Operators that are built by combining operators from two different groups. Below are the example of Augmented or Mixed type operators.

Name                   Symbol  	Description
Bitwise and		&=	Performs Bitwise AND on operands and assign value to left operand
Bitwise OR		|=	Performs Bitwise OR on operands and assign value to left operand
Bitwise XOR		^=	Performs Bitwise xOR on operands and assign value to left operand
Bitwise shift Right	>>=	Performs Bitwise right shift on operands and assign value to left operand
Bitwise shift Left	<<=	Performs Bitwise left shift on operands and assign value to left operand

Let’s see the example for each one of these. Let say, x is 4 and y is 1

Bitwise and: x &= y => x = x&y
 Explaination: x = (0100 & 0001) => 0000 (0)
Bitwise OR: x |= y => x = x|y
 Explaination: x = (0100 | 0001) => 0101 (5)
Bitwise XOR: x ^= y => x = x^y
 Explaination: x = (0100 ^ 0001) => 0101 (5)
Bitwise shift Right: x >>= y => x = x>>y
 Explaination: x = (0100 >> 1) => 0010 (2)
Bitwise shift Left: x<<=y => x = x<<y
 Explaination: x = (0100 << 1) => 1000 (8)

Logical Operator

The logical operator works only with boolean operands (True or False). The result of a logical operation is also a boolean. There are only three logical operator – and or in python and not in python. The table below shows the all logical operators available at Python.

Name           Symbol   Description
Logical AND	and	Evaluates to True, if both the operands are True, otherwise False
Logical OR	or	Evaluates to True, even if one of the Operand is True, otherwise False
Logical NOT	not	Evaluates to flip the original boolean value, if True it becomes False and so on.

Let us see with the help of examples,

Logical AND: True and False => False
Logical OR: True or False => True
Logical NOT: not True => False

Identity Operator

Identity Operators help find out whether the value of operand (variable in general programming term) shares the same memory location. If the same value is tied to two variable then Identity operators will output True.

There are only two types of Identity operators, “is” and “is not”. Let us look at them.

Name                   	Symbol    	Description
Identical		is		Returns True if both operands share same memory location
Not Identical		is not		Returns True if both operands do not share same memory location

Let us see with the help of examples, Consider x= 10 and y = 20 and z = 10

1) x is z => True
2) x is y => False
3) x is not z => False
4) y is not z => True

Membership Operator

Membership operator is simple yet very useful, particularly in searching through a list of huge value. Membership operators help find if a particular value is present or not present in a large sequence of data such as List, Tuple, set and strings. There are only two operators in this section which are, “in” and “not in”.

Name           Symbol    Description
Value present	in	Returns True, if the value is present in a sequence, otherwise False.
Value absent	not in	Returns True, if the value is not present in a sequence, otherwise False.

Let us see with the help of examples, Consider, L = [1, 2, 3, 4, 5, 6 ]

Value present: 2 in L => True, 9 in L => False
Value absent: 2 not in L => False, 9 not in L => True

Bitwise Operator

As you have seen earlier in this section, the Bitwise combined with the Assignment to produce another group of operators. I believe you got some idea about the bitwise operator, even if you did not, don’t worry. This type of operator works bit by bit hence named as Bitwise Operator.

Note: When you write a number in decimal form and apply a bitwise operator. In the background, Python converts the decimal number into binary and perform the operation. Once the operation is finished, Python converts back the result is decimal again and display the result. Find below the table for the complete list of Bitwise operators.

Name                  Symbol    Description
Bitwise AND		&	Performs "and" operation bit by bit and returns the result.
Bitwise OR		|	Performs "or" operation bit by bit and returns the result.
Bitwise NOT		~	Invert the value bit by bit (changing 0 -> 1 and 1 -> 0)
Bitwise XOR		^	Performs "xor" operation bit by bit and returns the result.
Bitwise right shift	>>	Shift bits to specified place towards the right side.
Bitwise left shift 	<<	Shift bits to specified place towards the left side.

Let us see with the help of examples. Let say, x is 4 and y is 1

Bitwise AND: x&y (i.e 4 & 1)
    Explaination: (0100 & 0001) => 0000 (0)
Bitwise OR: x|y (i.e 4 | 1)
    Explaination: (0100 | 0001) => 0101 (5)
Bitwise NOT: ~x (i.e ~4)
    Explanation: ~(0100) => -5
Bitwise XOR: x^y (i.e 4 ^ 1)
    Explaination: (0100 ^ 0001) => 0101 (5)
Bitwise shift Right: x>>y (i.e 0100 >> 1)
    Explaination: (0100 >> 1) => 0010 (2)
Bitwise shift Left: x<<y (i.e 0100 << 1)
    Explaination: (0100 << 1) => 1000 (8)


Conclusion

After reading the complete article, you have learned what are operators in Python and why operators are used. You have also understood the concept of Operands. Further, you have also learned various types of operators in Python such as Arithmetic, Logical, Assignment, Bitwise, Membership, Identity and comparison operators.

I hope you have understood the concept in this tutorial and I also hope that you are going to practice it in your Python code. If you have any concern please put them in the comment section. We will help you out.

Keep Learning and Keep Growing !!

How did you like the content

Scroll to Top