In this post, we will be investigating about the Arithmetic Operators in Python programming language. In this program, we will be performing some basic Arithmetic operations like Addition, Subtraction, Division, Multiplication, etc with the use of arithmetic operators in python program.
Before we get to the code performing arithmetic operations in python. Let us quickly get an overview of what are different arithmetic operators available for the Python programs with their functions.
Arithmetic Operators
Arithmetic operators are utilized with numeric qualities to perform regular mathematical activities or operations:
Operators | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds values on either side of the operator. | a + b |
– | Subtraction | Subtracts right hand operand from left hand operand. | a – b |
* | Multiplication | Multiplies values on either side of the operator. | a * b |
/ | Division | Divides left hand operand by right hand operand. | a / b |
% | Modulus | Divides left hand operand by right hand operand and returns the remainder. | a % b |
** | Exponentiation | Performs exponential calculations on operands. | a ** b |
// | Floor Division | Division of operands where the result is quotient and the digits after the decimal point are removed; and in case of negative the result is rounded away from zero. | a // b |
Program with Arithmetic Operators in Python
#Assume two variables a and b with random initial values a = 5 b= 2 #Addition s = a + b print("Sum = ",s) #Subtraction d = a - b print("Difference = ",d) #Division di = a / b #a is dividend and b is divisor print("Division = ",di) #Multiplication m = a * b #a is multiplicand and b is multiplier print("Multiplication = ",m) #Exponentiation e = a ** b #a is base and b is exponential power (exponent) print("Exponentiation = ",e) #Floor Division f = a // b #a is dividend and b is divisor print("Floor Division = ",f)
Note: In above program the variables used a and b are the operands and different symbols used between them are operators. And in order to execute this program, save it with a suffix of .py, for example, HelloWorld.py – Ctrl+F5
(for Visual Studio Code IDE). According to different Integrated Development Environments (IDEs), the process to run the program may differ, no need to worry about it.
Output
Sum = 7
Difference = 3
Division = 2.5
Multiplication = 10
Exponentiation = 25
Floor Division = 2
New to python programming? Get to know the Hello World program in Python.
Explanation
In the first line, we can see the sentence followed by hash ‘#‘, are known as comments.
#Assume two variables a and b with random initial values
.......
#Addition
.......
#Subtraction
.......
#Division
....... #a is dividend and b is divisor
#Multiplication
........ #a is multiplicand and b is multiplier
#Exponentiation
........ #a is base and b is exponential power (exponent)
#Floor Division
........ #a is dividend and b is divisor
Comments can be taken as the important part of the programming as they allows the programmers to explain what each part of the code does.
After the comments, there are two alphabets a and b which are known are variables (whose value can be changed during execution of the program) but in this case we have made it static that means the value assigned to a (=5) and b (=2) does not change.
Further going downwards, we see the statement as below:
s = a + b
Above statement means that the variable a (known as left hand operand in the program) and b (known as right hand operand in the program) are being added with the arithmetic operator named as addition operation and the calculated value (or the result) is assigned to the variable s.
And then, the statement encountered as print() displays the specified message in the screen as output of the program as shown above.
print("Sum = ",s)
Similarly, it applies for the different operators used in above program (-, / , *, // , **, %). And the remainings, di, m, f, d, e are also known as variables.
Now, take this program to any IDE for Python and try to achieve the output.