Arithmetic And Assignment Operators In Python

Hello Pythonistas, here’s a quick reference to arithmetic and assignment operators in Python.

Operators In Python

Operators are simple symbols or keywords(reserved by python) that perform some task between two given values.

These values can be string, integer, float, or any data type and even variable.

See simplešŸ˜‡, isnā€™t it? Letā€™s in detail find out about all of its 6 types.

Types of operators arithmetic and assignment

Arithmetic Operators

These operators perform mathematical tasks on the two values(operand), like +,-,/,*, etc. Letā€™s see each one of them in detail.

OPERATORNAMEEXAMPLEEXPLANATION
+Addition1+1 = 2adds two numbers
ā€“Subtraction1-1 = 0performs subtraction
*Multiplication1*3 = 3multiplies two numbers
/Division4/2 = 2divides one number with the other
//Floor Division5//2 = 2gives the answer in integer after dividing
%Modulus5%2 = 1gives the remainder after dividing
**Exponentiation2**2 = 4raises first to the power of second

Assignment Operators

Has your teacheršŸ‘©ā€šŸ« ever assigned you homework?

She might have assigned it to you using blackboard and chalk.

Assignment operators assign values to variables, they work similar to chalk and blackboard, that is, act as a tool for assignment.

For example, a = 10 here, '=' is an assignment operator it gives the variable 'a' which is on the left the value '10' which is on right.

Let’s look at these operators in detail.

OperatorNameExampleTwin toExplanation
=Equals toa= 10a=10This is used to give a value to any variable
+=Plus equals toa= 10
a+= 10
print(a) #20
a=a+10This is used to add numbers in a pythonic way.
-=Minus equals toa= 10
a-= 10
print(a) #0
a=a-10This is used to subtract in a pythonic way.
*=Multiply equals toa= 10
a*= 10
print(a) #100
a=a*10This is used to multiply in a pythonic way
/=Divide equals toa= 15
a/= 10
print(a) #1.5
a=a/10This is used to divide in a pythonic way
//=Floor division equals toa= 15
a//= 10
print(a) #1
a=//10This is used to floor divide in a pythonic way
**=Exponentiation equals toa= 2
a**= 2
print(a) #4
a=a**2This is used to use exponentiation in a pythonic way
%=Modulus equals toa= 15
a%= 10
print(a) #5
a=a%10This is used to get the remainder in a pythonic way

Leave a Reply