Python Operators: Arithmetic operators, Comparison operators, and Assignment Operators

In python you can use an operator to accept one or more inputs in the form of variables or expressions to perform a task such as comparison or addition and then provide an output consistent with that task. Python operators are the very important concept in python programming because at high level you have to deal the things will these operators.

Data Operators or Python Operators

Types of data operators for numeric type supported by python are Arithmetic operators, comparison (relational) operators, assignment operators, and bit-wise operators.

Arithmetic Operators

As the name suggest arithmetic operators are used for arithmetic operations. The arithmetic operators are as follows:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus which is a remainder after division
  • Exponent

Addition

Addition is used to find some of two numbers.

num1 = 25
num2 = 15
num1 + num2 = 40

In this example num1 is 25 which is an integer and num2 is 15 which is also an integer. Here num1 is added to num2 and we get the answer of 40.

Subtraction

If you are a math student then you have an idea of what subtraction is? In programming subtraction is same like the real world math.

num1 = 10
num2 = 20
num1 – num2 = 10

We have just subtracted the num1 from num1 and get the answer of 10.

Multiplication

The multiplication use the same principle as it work in the real maths. It takes two numbers and then multiple them and provide you the result of that multiplication. Python operators are very useful in python programming language because it big programs you have to use them a lot.

num1 = 5
num2 = 10
num1 * num2 = 50

Num1 equals to 5 and num2 equals to 10. So, the product of these two numbers is 50.

Division

Division is used to find the quotient of two numbers.

num1 = 8
num2 = 2
num1 / num2 = 4

Modulus

Modulus is used to find the remainder of two numbers.

num1 = 25
num2 = 15
num1 % num2 = 10

Exponent

Exponent is used to find the exponent of two numbers. We just need to raise to the power of.

num1 = 25
num2 = 10
num1 ** num2 = 9.313225

So, num1 raised to the power of num2 equals 9.3

Comparison Operators

Comparison operators are also called as relation operators and it is used to compare one value to another and tell you whether the relationship you provided is true. The comparison operators are described below.

  • Double Equal to operator (==)
  • Not equal to operator (!=)
  • Greater then operator (>)
  • Less than operator (<)
  • Greater than equal to operator (>=)
  • Less than equal to operator (<=)

Double equal to operator (==)

If the values to two operands are equal, then the condition becomes true.

num1 = 25 
num2 = 10
(num1 == num2) is not true.

Not Equal to operator (!=)

If the values of two operands are not equal, then the condition becomes true.

num1 = 25 
num2 = 10
(num1 != num2) is true.

Greater than operator

If the values of left operand is greater than the value of the right operand, then the condition becomes true. Otherwise it will be false.

num1 = 25
num2 = 10
(num1 > num2) is true.

Less than operator

This operator works by checking the value of both website and if left side value is less than right side then it will give you result in true.

num1 = 25
num2 = 10
(num1 < num2) is not true.

Greater than equal to operator

Greater than operator in python works by checking the values at both sides and if the left side value is greater or equal to right side value, then you will get the result in true.

num1 = 25
num2 = 10
(num1 >= num2) is true.

Less than equal to operator

This working of this operation is same as the above operator but you can replace the greater than with the less than and you will get the value for less than equal to operator.

Assignment Operators

Imagine you are writing a math solving a math problem and you have solved it and got the answer. You will assign you answer to a word of English to remember that this is the answer. In programming the principle is same you assign the value to variables with the assignment operators to remember them.

  • Equal to operator (=)
  • Add AND Operator (+=)
  • Subtract AND Operator (-=)
  • Multiply AND Operator (*=)
  • Divide AND Operator (/=)
  • Modulus AND Operator (%=)
  • Exponent AND Operator (**=)

Equal to operator (=)

It is used to assign values from right side operands to left side operand.

num3 = num1 + num2 assigns
Value of num1 + num2 into num3

Add AND Operator (+=)

It is used to add the value of the right operand to the left operand and assign the result to left operand.

num1 += num2 is equivalent to
num1 = num1 + num2

Subtract AND Operator (-=)

It is used to subtract the value of the right operand from the left operand and assign the result to the left operand.

num1 -= num2 is equivalent to 
num1 = num1 – num2

Multiply AND Operator (*=)

This operator python perform multiplication between the two values. This means it multiply the right number with the left number and assign the value to the left number or side.

num1 *= num2 is equivalent to 
num1 = num1 * num2

Divide AND Operator

It is used to divide the value of the left operand with the right operand and assign the result to the left operand.

num1 /= num2 is equivalent to 
num1 = num1 / num2

Modulus AND Operator

This operator takes two values and take modulus of then and assign the result of this modulus to the left side.

num1 %= num2 is equivalent to 
num1 = num1 % num2

Exponent AND Operator

It is used to perform the exponential (power) calculation on operators and assign value to the left operator.  

If you like this article about Python Operators: Arithmetic operators, Comparison operators, and Assignment operators then share it with your friends on Facebook, Twitter, etc.

Muhammad Nauman

Muhammad Nauman a Linguistics Scholar, Content Writer, and Teacher of English as a Second Language is a passionate and dedicated person to his interests in language. He has been teaching for three years in various institutes and has a keen interest in language learning and teaching and is a researcher as well.

Leave a Reply

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