Last Updated on March 10, 2023 by Abhishek Sharma
While in a mall purchasing any groceries, and you see different offers on the same grocery in the sale then how will you compare which offer is best and provides you the maximum benefit, You will do some calculations on the calculator and then know the correct answer but what are these calculations?
These are basic arithmetic operations performed by a calculator. In this article, we will learn all about python arithmetic operators in detail with examples of each operator with proper explanation.
What are Python Arithmetic Operators?
Arithmetic operators are nothing but mathematical symbols used in programming or math to perform basic arithmetic operations on numerical values. Python supports all the arithmetic operators, some of the most commonly used python arithmetic operators are:
- Addition(+)
- Subtraction(-)
- Multiplication(*)
- Division(/)
- Modulus(%)
- Exponentiation(**)
- Floor Division(//)
Addition Operator (+)
When we want to add two or more numbers we use the addition operator. It is one of the most commonly used python arithmetic operators.
Syntax of Addition Operator
The syntax for addition in Python is as follows:
result = num1 + num2
Example of Addition Operator
Let’s understand the following example to understand the concept better.
Code Implementation
num1=10 num2=5 result = num1 + num2 print(result)
Output
15
Explanation of the above code
In the above example num1 and num2 are the numbers being added, and the result is then stored and printed. This will output 15, which is the sum of the two numbers.
Subtraction Operator (-)
To subtract one number from another the subtraction operator is used.
Syntax of Subtraction Operator
The syntax for subtraction in Python is as follows:
result = num1 - num2
Example of Subtraction Operator
Look at the example given below to understand the subtraction operator.
Code Implementation
num1=10 num2=5 result = num1 - num2 print(result)
Output
5
Explanation of the above example
In the above example, num2 is the number being subtracted from, and num1 is the number being subtracted. This will output 5, which is the difference between the two numbers.
Multiplication Operator (*)
To multiply two or more numbers we use the multiplication operator.
Syntax of Multiplication Operator
The syntax for multiplication in Python is as follows:
result = num1 * num2
Example of Multiplication Operator
Below is the example of a multiplication operator.
Code Implementation
num1=10 num2=5 result = num1 * num2 print(result)
Output
50
Explanation of the above example
In the above example, num1 and num2 are the numbers being multiplied. This will output 50, which is the product of the two numbers.
Division Operator (/)
To divide one number by another the division operator is used.
Syntax of Division Operator
The syntax for division in Python is as follows:
result = num1 / num2
Example of Division Operator
Below is the example of a division operator.
Code Implementation
num1=10 num2=5 result = num1 / num2 print(result)
Output
2.0
Explanation of the above code
In the above syntax, num1 is the numerator, and num2 is the denominator. This will output 2.0, which is the quotient of the two numbers.
Modulus Operator (%)
To find the remainder of a division operation the modulus operator is used.
Syntax of Modulus Operator
The syntax for modulus in Python is as follows:
result = num1 % num2
Example of Modulus Operator
Below is the example of a Modulus operator.
Code Implementation
num1=10 num2=3 result = num1 % num2 print(result)
Output
1
Explanation of the above example
In the above syntax, num1 is the dividend and num2 is the divisor. The result is the remainder when num1 is divided by num2. This will output 1, which is the remainder when 10 is divided by 3.
Exponentiation Operator (**)
To raise a number to a power the exponentiation operator is used.
Syntax of Exponentiation Operator
The syntax for exponentiation in Python is as follows:
result = num1 ** num2
Example of Exponentiation Operator
Below is the example of an exponentiation operator.
Code Implementation
num1=2 num2=3 result = num1 ** num2 print(result)
Output
8
Explanation of the above example
In the above example, let’s say we are raising the number 2 to the power of 3. This will output 8.
Floor Division Operator (//)
The floor division operator is used to divide one number by another and get the floor of the result. The floor of a number is the largest integer less than or equal to the number. This is one of the least used python arithmetic operators.
Syntax of Floor Division Operator
The syntax for floor division in Python is as follows:
result = num1 // num2
Code Implementation
num1=10 num2=3 result = num1 // num2 print(result)
Output
3
Explanation of the above example
In the above example, num1 is the numerator and num2 is the denominator. The result is the floor of the quotient when num1 is divided by num2, and it is stored in the variable result. This will output 3, which is the largest integer less than or equal to the quotient when 10 is divided by 3.
Operator Precedence
When multiple arithmetic operators are used in an expression, the order in which they are evaluated is determined by operator precedence. Python arithmetic operators follow the standard order of operations, which is as follows:
- Parentheses ()
- Exponentiation **
- Multiplication and division *, /, %
- Addition and subtraction +, –
Applications of Python Arithmetic Operators
Python arithmetic operators are used in many different applications, such as
- Calculations: Arithmetic operators are used to performing simple or complex calculations in Python. For example, if you need to calculate the area of a rectangle, you can use the multiplication operator to multiply its length by its width. Similarly, if you need to calculate the interest on a loan, you can use the multiplication and addition operators to perform the necessary calculations.
- Data analysis: Arithmetic operators are used in data analysis applications to perform various mathematical operations on data sets. For example, you can use arithmetic operators to calculate the average, median, and mode of a set of numbers. You can also use them to perform more complex calculations, such as standard deviation, correlation, and regression analysis.
- Financial applications: Arithmetic operators are used in many financial applications to perform calculations related to investments, loans, and other financial transactions. For example, you can use them to calculate compound interest, amortization, and the present value of money.
- Graphics and gaming: Arithmetic operators are used in graphics and gaming applications to perform calculations related to object movement, collision detection, and other game mechanics. For example, you can use the addition operator to move an object a certain distance in a particular direction, or the multiplication operator to scale an object’s size.
- Science and engineering: Arithmetic operators are used in scientific and engineering applications to perform calculations related to physical and chemical phenomena. For example, you can use them to calculate the velocity of an object, the force required to move an object, or the amount of energy released in a chemical reaction.
Conclusion
Arithmetic operators are an essential concept in programming, and they are used to perform basic mathematical operations on numbers. Python arithmetic operators include addition, subtraction, multiplication, division, modulus, exponentiation, and floor division. By understanding the syntax and usage of these operators, programmers can write code that performs complex mathematical operations with ease. It is also important to keep in mind operator precedence, as it determines the order in which arithmetic operations are evaluated.
Frequently Asked Questions
1. How do I change the order of evaluation in Python?
You can change the order of evaluation in Python by using parentheses to group the operations that you want to perform first.
2. How do I perform complex arithmetic operations in Python?
You can perform complex arithmetic operations in Python by using a combination of arithmetic operators and parentheses.
3. How do I convert a string to a number in Python?
You can convert a string to a number in Python using the int() or float() functions.
4. How do I format numbers in Python?
You can format numbers in Python using the format() function or f-strings.
5. How do I perform arithmetic operations on large numbers in Python?
Python supports arbitrary-precision arithmetic, which means that it can handle integers and floats of any size. To perform arithmetic operations on large numbers, you can use the same arithmetic operators used for smaller numbers. However, you should be aware that operations on large numbers may take longer to execute.