Python Operators – Python3 Tutorials

Like other programming languages Python also uses various Python Operators. Here, we will discuss each descriptively. What is Operator? Operators are special symbols(like +,*,-,, etc.), which are used to performing operations on the variables and values.

Table of Content:

What’s inside:
Types of Python Operators
Arithmetic Operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
FAQs

Types of Python Operators

Python language divides the operators into the following types:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Identity operators
  6. Membership operators
  7. Bitwise operators

Let us have a look at all the operators one by one.

Arithmetic Operators

Arithmetic Operators in Python perform various arithmetic operations like addition, subtraction, multiplication, division, %modulus, and so on.

Suppose : a=10 and b=5

OperatorDescriptionExample
(+)AdditionAdds values.a + b = 15
(-)SubtractionSubtracts right-hand value from left-hand value.a – b = 10
(*)MultiplicationMultiplies the valuesa * b = 50
(/) DivisionDivides left-hand Value by right-hand value.b / a = 0.2
(%) ModulusDivides left-hand operand or value by right-hand operand and return the remainderb % a =2
(**) ExponentExponent exponential ( or power) calculation on operatorsa**b =10 to the power 5
//Floor Division – The division of values where the result is the quotient or remainder. But if one of the operands is (-) negative, the result will be floored, that is rounded away from zero (towards the negative infinity) −9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0

Assignment Operators

Like other programming languages, Python also has various assignment operators.

Let a=5, b=6, and c=7.

OperatorDescriptionExample
=this sign assigns values from right side to left side operandc = a + b assigns value of a + b into c
+= Add ANDIt adds right to the left operand and assigns the result to the left side operandc += a is equivalent to c = c + a
-= Subtract ANDthis subtracts right from the left side operand and assigns the result to the left side operandc -= a is equivalent to c = c – a
*= Multiply ANDIt multiplies the right with the left operand and assigns the result to the left side operandc *= a is equivalent to c = c * a
/= Divide ANDIt divides left with the right operand and assign the result to the left side operandc /= a is equivalent to c = c / a
%= Modulus ANDIt takes modulus using two operands and assigns the result to the left sidec %= a is equivalent to c = c % a
**= Exponent ANDPerforms the exponential ( or power) calculation on operators and assign value to the left side operand.c **= a is equivalent to c = c ** a
//= Floor DivisionIt performs floor division on both side operators and assigns the value to the left operandc //= a is equivalent to c = c // a

Python Comparison Operators

OperatorMeaningExample
>Greater than sign – True if the left operand is greater than the right.x > y
<Less than sign – True if left operand is less than the right.x < y
==Equal to sign – True if both operands are equal.x == y
!=Not equal to sign – True if operands are not equal.x != y
>=Greater than or equal to sign – True if the left operand is greater than or equal to the right.x >= y
<=Less than or equal to sign – True if the left operand is less than or equal to the right.x <= y

Logical operators in Python

The Logical operators in PythonPython uses for conditional statements are true or false. Logical operators are AND, OR, and NOT.

For Python logical operators below, mentioned following condition are applied:

  1. AND operator – This operator is returned TRUE if both the operands (right side and left side) are true.
  2. OR operator– OR returns TRUE if either of the operand (right side or left side) is true.
  3. NOT operator– this operator returns TRUE if the operand is false.

For Example: Here, we get a true or false value based on the value of X and Y.

X = True
Y = False
print(('X and Y is',X and Y))
print(('X or Y is',X or Y))
print(('not a is',not Y))

Identity operators

Identity python operators use to compare the objects, not only if they are equal, but if they are the same object, with the same memory location, we can say that Identity Operators use to compare the memory location of the two objects. 

There are two types of identity operators used in PythonPython.

  • Is Operator: It returns true if two variables point to the same object.
  • Is not Operator: This returns false if two variables or operands the same object, or it returns false if two variables are not the same.
a1 = 5
b1 = 5
a2 = 'Hello'
b2 = 'Hello'
a3 = [1,2,3]
b3 = [1,2,3]

# Output will be: False
.
print(a1 is not b1)

# Output will be: True
.
print(a2 is b2)

# Output will be: False
.
print(a3 is b3)
Output:
python operators

Membership operators

Membership Python operators are types: in and not in.

In membership operators: True if the variable is found in the sequence. For instance:

not in membership operators: True if the variable is not found in the sequence.

x = 'Hello Guys'
y = {1:'a',2:'b'}

# Output will be: True
print('H' in x)

# Output will be: True
print('hello' not in x)

# Output will be: True
print(1 in y)

# Output will be: False
print('a' in y)
Output:
python operators

Bitwise operators

Bitwise Python operators act on operands or values as if they were strings of binary digits. It operates on the bits and performs bit by bit operation.

For example: 2 = 10 in binary and 7 = 111.
OperatorNameDescription
ANDAND is used to sets each bit to 1 if it exists in both operands
|ORIt copies 1 bit if it exists in either operand.
 ^XORThe resulting bit will be 0 if both the bits are the same, or it will be 1 if both bits are different; 
NOTIt calculates the negation of each bit of the operand or values; that is, if the bit is 1, the resulting bit will be 0 and vice versa.
<<Zero fill left shiftThe left operand value is moved left side by the number of bits present in the right side operand.
>>Signed right shiftThe left operand is moved right side by the number of bits present in the right side operand.

FAQs in Python Operators:

While using Python IDLE, by how many spaces are the code suites indented?

Ans: 4 Spaces

What is the output of code: print(2%6)

Ans: 2

What is the output of code: print(2 ** 3 ** 2)

Ans: 512

What is the output of the below-mentioned code of assignment operator?
y = 10
x = y += 2
print(x)

Ans: Syntax error

Must Read:


In the above, we have completed the discussion about Python operators, their type, uses, and descriptions with suitable examples. In the coming one, we will discuss the conditions statements and loops in PythonPython.

For now Thank you for being here 🙂

4 thoughts on “Python Operators – Python3 Tutorials

Leave a Reply

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