Hello Pythonistas, here’s a quick reference to arithmetic and assignment operators in Python.
Contents
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.
Arithmetic Operators
These operators perform mathematical tasks on the two values(operand), like +,-,/,*, etc. Letās see each one of them in detail.
OPERATOR | NAME | EXAMPLE | EXPLANATION |
---|---|---|---|
+ | Addition | 1+1 = 2 | adds two numbers |
ā | Subtraction | 1-1 = 0 | performs subtraction |
* | Multiplication | 1*3 = 3 | multiplies two numbers |
/ | Division | 4/2 = 2 | divides one number with the other |
// | Floor Division | 5//2 = 2 | gives the answer in integer after dividing |
% | Modulus | 5%2 = 1 | gives the remainder after dividing |
** | Exponentiation | 2**2 = 4 | raises 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.
Operator | Name | Example | Twin to | Explanation |
---|---|---|---|---|
= | Equals to | a= 10 | a=10 | This is used to give a value to any variable |
+= | Plus equals to | a= 10 | a=a+10 | This is used to add numbers in a pythonic way. |
-= | Minus equals to | a= 10 | a=a-10 | This is used to subtract in a pythonic way. |
*= | Multiply equals to | a= 10 | a=a*10 | This is used to multiply in a pythonic way |
/= | Divide equals to | a= 15 | a=a/10 | This is used to divide in a pythonic way |
//= | Floor division equals to | a= 15 | a=//10 | This is used to floor divide in a pythonic way |
**= | Exponentiation equals to | a= 2 | a=a**2 | This is used to use exponentiation in a pythonic way |
%= | Modulus equals to | a= 15 | a=a%10 | This is used to get the remainder in a pythonic way |