In this tutorial, we will cover basic arithmetic operations using python. Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. No doubt, we all are so familiar with the arithmetic operation as we are using it from primary school. And we can do these calculations without using pen and paper. But typing code from doing these calculations will help us to take one step ahead to learn python with ease and use it efficiently in our daily work and as a real-world example.
So Let’s begin and type some lines of codes to perform these operations-
Before we start to write an exact line of code in python we should know what and all operations can be performed. Here you can find operations with its python operator and syntax.
Operation | Operator | Summary | Syntax |
Addition | + | to add two number | a+b |
Subtraction | – | to subtracts two number | a-b |
Multiplication | * | to multiplies two number | a*b |
Division (float) | / | to divide the first number by the second (float) | a/b |
Division (floor) | // | to divide the first number by the second | a//b |
Modulus | % | to return the remainder of a division operation | a%b |
Power | ** | to return the first number raised to power second | a**b |
1. Addition
The addition is the process of calculating the total of two or more numbers. We can use the ‘+’ (plus) symbol to do addition. Python language uses a very simple syntax a+b
to perform this operation.
#simple addition(2+3) print(2+3) #Output : 5 #using two different variable a = 4 b = 5 c = a+b print (c) #Output : 9
2. Subtraction
Subtraction is the arithmetic operation for finding the difference between two numbers. We can use the ‘-‘ (minus) symbol to do subtraction. Here also in Python, we have an easy syntax a-b
to perform subtraction operation.
#simple subtraction(5-2) print(5-2) #Output : 3 #using two different variable a = 8 b = 3 c = a-b print (c) #Output : 5
3. Multiplication
Multiplication is also a simple arithmetic operation. The multiplication of two numbers is equivalent to adding as many copies of one of them, the multiplicand, as the value of the other one, the multiplier. We can use the ‘*’ (asterisk) symbol to do subtraction. Multiplication also have an effortless syntax a*b
to perform multiplication operation.
#simple multiplication(6*3) print(6*3) #Output : 18 #using two different variable a = 4 b = 3 c = a*b print (c) #Output : 12
4. Division (float)
Division is splitting into equal parts or groups. We can use the ‘/’ (single backslash) symbol to do float division. Float division uses syntax a/b
to perform the operation. In the float division, we will get the floating-point (decimal) as the final result.
#simple float divison(7/2) print(7/2) #Output : 3.5 #using two different variable a = 9 b = 4 c = a/b print (c) #Output : 2.25
5. Division (floor)
Division is splitting into equal parts or groups. We can use the ‘//’ (double backslash) symbol to do floor division. Float division uses syntax a//b
to perform the operation. In the float division, it returns the quotient in which the digits after the decimal point are removed.
#simple floor divisoin(7//2) print(7//2) #Output : 3 #using two different variable a = 9 b = 4 c = a//b print (c) #Output : 2
6. Modulus
The modulus (or modulo or mod) is the remainder after dividing one number by another. We can use the ‘%’ (percentage) symbol to perform modulus. It has an easy syntax a%b
to perform this operation.
#simple modulus(8%3) print(8%3) #Output : 2 #using two different variable a = 6 b = 5 c = a%b print (c) #Output : 1
7. Power
The power (or exponent) of a number says how many times to use the number in a multiplication. We can use the ‘**’ (double asterisk) symbol to perform modulus. It has an easy syntax a**b
to perform power operation.
#simple power operation(2**3) print(2**3) #Output : 8 #using two different variable a = 3 b = 2 c = a**b print (c) #Output : 9
While writing code for the arithmetic operation we can write a code to solve the long equations with the mixture of all operators. It will follow the “DMAS” rule to solve that equation.
#solving an equation of mixed operators exp = 4+4-7*2/8//7%2 print (exp) #Output : 8.0
If we want to perform any specific operation at first and not to follow the “DMAS” rule we can use brackets to specify the priority to any specific operation. This rule also we know for so long, it called as “BODMAS” rule.
#solving an equation of mixed operators exp = 4+4-[(7*2)/8//7%2] print (exp) #Output : 5.0
So now definitely you got that how easy it is to perform basic arithmetic operations using Python code. Therefore now we will write a beautiful code of a few lines that can replace our basic calculator. This calculator may not have a GUI but it can show you the proper and exact result if you use appropriate operators according to the table given above.
# This is s program perform the operation of basic calculator # Addition def add(x, y): return x + y # Subtraction def subtract(x, y): return x - y # Multiplication def multiply(x, y): return x * y # Float division def float_divide(x, y): return x / y # Floor division def floor_divide(x, y): return x // y # Modulus def modulus(x, y): return x %y # Power def power(x, y): return x **y print("Select operation you want to perform") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Float Division") print("5. Floor Division") print("6. Modulus") print("7. Power") while True: # Take input from the user choice = input("Enter choice(1/2/3/4/5/6/7): ") # Check if choice is one of the four options if choice in ('1', '2', '3', '4','5','6','7'): num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1': print("Addition") print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print("Subtraction") print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print("Multiplication") print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print("Float Division") print(num1, "/", num2, "=", float_divide(num1, num2)) elif choice == '5': print("Floor Division") print(num1, "/", num2, "=", floor_divide(num1, num2)) elif choice == '6': print("Modulus") print(num1, "/", num2, "=", modulus(num1, num2)) elif choice == '7': print("Power") print(num1, "/", num2, "=", power(num1, num2)) break else: print("Invalid Input")
This code can perform all 7 basic arithmetic operations possible with Python code. You can just run it in your IDLE or any environment.
I hope you got some good points out from this post. Keep the support and share it with your friends.