Python Introduction

Python Operators: Arithmetic, Comparison, Logical

In computer programming, an operator is basically a symbol that represents a process or an action. These symbols are accommodated from logic and mathematics. An operator has the capability of manipulating certain values or operands. They are very useful and are the backbone of any program. Operators in python are in general used to perform mathematical or logical operations on values and variables. In this article, different types of python operator are discussed in detail along with examples.

What are Operators in Python?

Operators are standard symbols in python defined to perform arithmetic and mathematical operations. Below are the list of operators supported in python:

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Special operators

Each operator is explained below in detail with examples.

 Arithmetic Operators

Arithmetic operators are used to perform various mathematical operations like subtraction(-), addition(+), multiplication(*) and division(/).

There are seven arithmetic operators in python, here is a detailed explanation of each arithmetic operator:

  • Addition operator: In python, the symbol ‘+’ is used as an additional operator. Two values can be added by using the addition operator.

Example:

                    

value1 = 4

value2 = 3

res = value1 + value2

print (res)

Output

7

  • Subtraction Operator: In python, the symbol ‘-’ is used as a subtraction operator. The second value is subtracted from the first value using the subtraction operator.

Example:

                    

value1 = 4

value2 = 3

 

res = value1 – value2

print (res)

Output

1

  • Multiplication Operator: In python, the symbol ‘*’ is used as a multiplication operator. The product of two values can be calculated using the multiplication operator.

Example:

                    

value1 = 4

value2 = 3

 

res = value1*value2

print (res)

Output

12

  • Division Operator: In python, symbol ‘/’ is used as a division operator. It calculates the quotient when the first operand is divided by the second operand. It returns the answer as floating point value.

Example:

                    

value1 = 3

value2 = 2

 

res = value1/value2

print (res)

Output

1.5

 

  • Modulus Operator: In python, symbol ‘%’ is used as a modulus operator. It calculates the remainder left when the first value is divided by the second value.

Example:

                    

value1 = 4

value2 = 3

 

res = value1%value2

print (res)

 

Output

1

 

  • Exponential Operator: In python, symbol ‘**’ is used as an exponential operator. It is used to calculate the result when the first operand is raised to the power of the second operand.

Example:

                    

value1 = 4

value2 = 3

 

res = value1**value2

print (res)

Output

64

 

 

  • Floor Division Operator: In python, symbol ‘//’ is used as a floor division operator. It calculates the floor of the quotient when the first value is divided by the second value.

Example:

                    

value1 = 3

value2 = 2

 

res = value1//value2

print (res)

 

Output

1

Comparison operators

Comparison operators are used to comparing two values and find the relation among the values on either side. According to the condition, it returns True or False. As they are used to find relations between the values, they are also known as relational operators.

There are six comparison operators in python, here is a detailed explanation of each comparison operator:

  • Greater than operator: In python, symbol ‘>’ is used as greater than the operator. It returns True if the value on its left side is greater than the value on its right side, False otherwise.

Syntax: a > b

Example:

                    

value1 = 3

value2 = 2

 

print (value1 > value2)

 

Output

True

  • Less than operator: In python, symbol ‘<’ is used as less than operator. It returns True if the value on its right side is greater than the value on its left side, False otherwise.

 Syntax: a < b

 Example:

                    

value1 = 3

value2 = 2

 

print (value1 < value2)

Output

False

 

  • Greater than equal to: In python, symbol ‘>=’ is used as greater than equal to operator. It returns True if the value on its left side is greater than or equal to the value on its right side, False otherwise.

Syntax: a >= b

Example:

                    

value1 = 3

value2 = 2

 

print (value1 >= value2)

Output

True

 

  • Less than equal to: In python, symbol ‘<=’ is used as less than equal to operator. It returns True if the value on its left side is less than or equal to the value on its right side, False otherwise.

Syntax: a <= b

 Example:

                    

value1 = 3

value2 = 2

 

print (value1 <= value2)

Output

False

 

  • Equal to: In python, symbol ‘==’ is used as equal to operator. It returns True if the values on its left and right side are equal, False otherwise.

 Syntax: a == b

 Example:

                    

value1 = 3

value2 = 2

 

print (value1 == value2)

Output

False

  • Not equal to: In python, symbol ‘!=’ is used as not equal to operator. It returns True if values on both sides are not equal.

 

Syntax: a != b

 Example:

                    

value1 = 3

value2 = 2

 

print (value1 != value2)

Output

True

Logical operators

Logical operators are used on conditional statements that are either true or false.

There are three logical operators in Python, here is a detailed explanation of each logical operator:

  • Logical AND operator: In python, a keyword and is used to perform logical AND operation. It returns True if both the values are true, false otherwise.

If the first expression evaluated is False while performing and operator, then the further expressions are not evaluated.

Example:

                    

value1 = 3

value2 = 2

 

if (value1>0 and value2>0):

print (“Both the values are positive”)

else:

print (“Atleast one number is negative”)

Output

Both the values are positive

  • Logical OR operator: In python, keyword or is used to perform logical OR operation. It returns True if any of the two values is true, false otherwise.

If the first expression evaluated is True while performing or operator, then the further expressions are not evaluated.

Example:

                    

value1 = 3

value2 = -2

 

if (value1>0 or value2>0):

print (“Either of the value is positive”)

else:

print (“None of the value is greater than zero”)

Output

Either of the value is positive

  • Logical NOT operator: In python, keyword not is used to perform logical NOT operation. It works with a single boolean value. It returns True if the value is False and vice versa.

Example:

                    

value1 = 3

 

if  not (value1 > 0) :

print (“value1 is less than equal to zero”)

else:

print (“value1 is positive”)

Output

value1 is positive

Note that keywords and, or and not are case sensitive.

Bitwise operators

Bitwise operators are used to performing calculations on integers. These operations are performed bit by bit and therefore they are called bitwise operators. The final result is returned in decimal format.

There are six bitwise operators in python, here is a detailed explanation of each bitwise operator:

  • Bitwise AND operator: In python, the symbol ‘&’ is used as Bitwise AND operator. It returns 1 if both the bits on left and right are 1 else returns 0.

 Syntax: a & b

 Example:

                    

value1 = 10

value2 = 4

print (value1 & value2)

Output

0

Explanation:

1010 is the binary representation of 10

0100 is the binary representation of 4

So 1010 & 0100 == 0000

0 is the decimal representation of 10&4(=0)

 

  • Bitwise OR operator: In python, the symbol ‘|’ is used as bitwise or operator. It returns 1 if any of the bits on left or right is 1 else returns 0.

Syntax: a | b

 

Example:

                    

value1 = 10

value2 = 4

 

print (value1 | value2)

Output

14

Explanation:

1010 is the binary representation of 10

0100 is the binary representation of 4

So 1010 | 0100 == 1110

14 is the decimal representation of 10|4(=14)

  • Bitwise NOT operator: In python, the symbol ‘~’ is used as bitwise not operator. It returns one’s complement of the given value

Syntax: a ~ b

 Example:

                    

value1 = 10

 

print (~value1)

Output

11

Explanation:

~1010 = ~ (1010 + 1)

= ~ (1011)
Decimal representation of 1011 is 11

  • Bitwise XOR operator: In python, symbol ‘^’ is used as xor operator. It returns 1 if both bits are opposite, that is one is 1 other is 0 or vice versa.

Syntax: a ^ b

Example:

                    

value1 = 10

value2 = 4

 

print (value1 ^ value2)

 

Output

14

 

Explanation:

1010 is binary representation of 10

0100 is binary representation of 4

So 1010 ^ 0100 == 1110

14 is the decimal representation of 10^4(=14)

  • Bitwise right shift operator: In python, symbol ‘>>’ is used as bitwise right shift operator. It shifts the binary representation of the left operand shifted right by the number of positions equal to the right operand and inserts zero bit on the left.

Syntax: a >> b

Example:

                    

value1 = 10

value2 = 1

 

print (value1 >> value2)

Output

5

Explanation:

1010 is binary representation of 10

So 10 >> 1 = 0000 0101 = 5

  • Bitwise left shift operator: In python, symbol ‘<<’ is used as bitwise left shift operator. It returns the binary representation of the left operand shifted left by the number of positions equal to the right operand and inserts zero bit on the right.

 Syntax: a << b

 Example:

                    

value1 = 5

value2 = 2

 

print (value1 << value2)

 

Output

20

Explanation:

0000 0101 is binary representation of 5

5 << 2 = 0001 0100 = 20

 Assignment operators

Assignment operators are used to assigning values to the variables. There are thirteen assignment operators, let’s look into each one of these in detail.

  • Assign operator: It assigns the right-side value of expressions to the left-side value.

Syntax: a = b + c

Example: a = 2

  • Add and assign: It adds the value of right operand and left operand and assigns results to the left operand.

Syntax: a += b

Example: a+=2 i.e. a=a+2

  • Subtract and assign:It subtracts the value of the right operand from left operand and assigns result to left operand.

Syntax: a -= b

Example: a-=2 i.e. a=a-2

  • Multiply and assign: It multiplies the value of right operand and left operand and assigns result to left operand.

Syntax: a *= b

Example: a*=2 i.e. a=a*2

  • Divide and assign: It divides the value of left operand with right operand and assigns result to left operand.

Syntax: a /= b

Example: a/=2 i.e. a=a/2

  • Modulus and assign: It takes modulus of the value of left operand with right operand and assigns result to left operand.

Syntax: a %= b

Example: a%=2 i.e. a=a%2

  • Divide(floor) and assign: It divides the value of left operand with right operand and assigns the floor value to left operand.

Syntax: a //= b

Example: a//=2 i.e. a=a//2

  • Exponent and assign: It calculates exponent by raising the value of left operand to the power of right operand and assigns the result to left operand.

Syntax: a **= b

Example: a**=2 i.e. a=a**2

  • Bitwise AND and assign: It performs bitwise AND operation to left and right operands and assigns the result to left operand.

Syntax: a &= b

Example: a&=2 i.e. a=a&2

  • Bitwise OR and assign: It performs bitwise OR operation to left and right operands and assigns the result to left operand.

Syntax: a |= b

Example: a|=2 i.e. a=a|2

  • Bitwise XOR and assign: It performs bitwise XOR operation to left and right operands and assigns the result to left operand.

Syntax: a ^= b

Example: a^=2 i.e. a=a^2

 

  • Bitwise Left Shift and assign: It performs bitwise left shift operation to left and right operands and assigns the result to left operand.

Syntax: a <<= b

Example: a<<=2 i.e. a=a<<2

 

  • Bitwise Right Shift and assign: It performs bitwise right shift operation to left and right operands and assigns the result to left operand.

Syntax: a >>= b

Example: a>>=2 i.e. a=a>>2

 

Special operators

Some special operators are offered by python like identity operator and the membership operator, here is the detailed explanation of each special operator:

  • Identity operators: is’ and ‘is not’ are two types of identity operators. They are used to check if two operands are located on the same part of the memory.

is: It returns True if both operands are identical

is not: It returns True if both operands are not identical

 

Example:

                    

value1 = 5

value2 = 5

print(value1 is value2)

print(value1 is not value2)

Output

True

False

  • Membership operators: in’ and ‘in not’ are two types of identity operators. They are used to check if value is in a sequence.

in: It returns True if value is present in the sequence

not in: It returns True if value is not present in the sequence

Frequently Asked Questions:

Q.1. What is a Python operator?

Answer: A python operator has the capability of manipulating certain values or operands. They are very useful and are the backbone of any program.

Q.2. What is the operator in Python used for?

Answer: Operators in python are in general used to perform mathematical or logical operations on values and variables.  Below is the list of operators supported in python:

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Special operators

Q.3. What are the 3 python logical operators?

Answer:

  • Logical AND operator: In python, keyword and is used to perform logical AND It returns True if both the values are true, false otherwise.

If the first expression evaluated is False while performing and operator, then the further expressions are not evaluated.

Example:

                    

value1 = 3

value2 = 2

print(value1>0 and value2>0)

Output

True

  • Logical OR operator: In python, keyword or is used to perform logical OR operation. It returns True if any of the two values is true, false otherwise.

If the first expression evaluated is True while performing or operator, then the further expressions are not evaluated.

Example:

                    

value1 = 3

value2 = -2

print(value1>0 or value2>0)

Output

True

  • Logical NOT operator: In python, keyword not is used to perform logical NOT operation. It works with a single boolean value. It returns True if the value is False and vice versa.

Example:

                    

value1 = 3

 

if  not (value1 > 0) :

print (“value1 is not positive”)

else:

print (“value1 is positive”)

Output

value1 is positive

Q.4. What type of operator is == in Python?

Answer:  In python, symbol ‘==’ is used as equal to operator. It returns True if the values on its left and right side are equal, False otherwise.

Example:

                    

value1 = 3

value2 = 2

print(value1 == value2)

Output:

False

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.