Arithmetic Operators in Python perform mathematical calculations between two numerical values or operands. Whether you are adding totals, applying formulas, or handling scientific computations, it is important to learn how arithmetic calculations work in Python and what operators are used to carry them out. In this article, you will understand Python arithmetic operators, their syntax, and how operator precedence works in detail.
Table of Contents:
What are Arithmetic Operators in Python?
Arithmetic Operators are the symbols used in Python that allow users to perform basic mathematical calculations between two numerical values. Python provides operators for addition (+), subtraction (-), multiplication(*), division (/), modulus (%), Exponentiation (**), and floor division (//). Having reserved operators for such methods eliminates the need to define a function to carry out the calculations.
Operands and Operators in Python
Operators are the symbols that instruct the interpreter to perform the calculations (operations) on the operands. Operands are the values or variables on which the calculations are performed by the interpreter.
For Example, in the expression “3 + 5,” (+) is an operator, and 3 and 5 are the operands.
Types of Arithmetic Operators in Python
In this section, we will be looking at all the types of operators in Python that were mentioned above with example code.
1. Addition (+)
This operator takes two operands and calculates their sum.
Example:
Output:
Explanation: Here, we have added 4 and 5.
2. Subtraction (-)
This operator takes in two operands and calculates their difference.
Example:
Output:
Explanation: Here, we have subtracted 5 from 4, resulting in -1.
3. Multiplication (*)
This operator takes in two operands and outputs the multiplication result of both.
Example:
Output:
Explanation: Here, we have multiplied 4 and 5, resulting in 20.
4. Division (/)
This operator divides the first operand by the second operand. Remember, the division operator always returns a float value, even if the first operand is completely divisible by the second operand or both operands are integers.
Example:
Output:
Explanation: Here, we have divided 4 and 5, which resulted in 0.8.
5. Modulus (%)
This operator returns the remainder after dividing the first operand by the second operand.
Example:
Output:
Explanation: Here, we have implemented a modulus operation on 6 and 5, which resulted in 1.
6. Exponentiation (**)
This operator raises the first operand to the power of the second operand and returns the result.
Example:
Output:
Explanation: Here, we have raised 4 to the power of 5, which resulted in 1024.
7. Floor Division (//)
This operator divides the first operand by the second operand and returns the largest integer less than or equal to the result.
Example:
Output:
Explanation: Here, 4 divided by 5 is 0.8. Floor division returns 0, which is the nearest integer smaller than the result.
Summary Table of Arithmetic Operators in Python
Operator | Symbol | Description | Example | Result |
Addition | + | Adds two numbers | 5 + 3 | 8 |
Subtraction | – | Subtracts the second number from the first | 10 – 4 | 6 |
Multiplication | * | Multiplies two numbers | 6 * 2 | 12 |
Division | / | Divides the first number by the second, returns float | 7 / 2 | 3.5 |
Modulus | % | Returns the remainder | 10 % 3 | 1 |
Exponentiation | ** | Raises the first number to the power of the second | 2 ** 3 | 8 |
Floor Division | // | Returns the integer part of division | 9 // 2 | 4 |
Precedence and Associativity of Operators in Python
Operator precedence determines the order in which the operations are evaluated based on the ranking or priority of the operators used in a single expression. Python follows the same mathematical rules of precedence, also known as BODMAS, to determine which will be evaluated first.
- B – Stands for Brackets
- O – Stands for orders (powers and roots)
- D – Stands for Division
- M – Stands for Multiplication
- A – Stands for Addition
- S – Stands for Subtraction
Example:
Output:
Explanation: Here, we gave a single expression with many operators, and Python evaluated the expression following the BODMAS rule.
Associativity determines the direction in which the operators will be evaluated in the case of operators with the same precedence. Left-associative means they are evaluated from left to right, and Right-associative means they are evaluated from right to left.
Example:
Output:
Explanation: Here, in both cases, the operators have the same precedence. Therefore, we solved the expressions based on the associativity rule. The calculations happen as 2 ** (3 ** 2)
Arithmetic operations with different Data Types
Python performs arithmetic operations across various numerical data types in Python, like integers and floats. Python can also handle complex numbers without importing any external module. Let us explore them one by one.
1. Integers (int)
The integer (int) data type in Python represents whole numbers, both positive and negative, just like in mathematics. They can hold positive numbers, negative numbers and zeros. This data type is commonly used in operations like counting, looping, indexing, and more. Operations with integers are straightforward and follow mathematical principles.
Example:
Output:
Explanation: Here, we performed some arithmetic operations on integers, which resulted in integer outputs.
2. Float (float)
Floating-point numbers in Python represent real numbers in mathematics. These data types are used when you need greater precision in situations like scientific calculations, financial log sheets, and many more. All arithmetic operators work with floats and often return as floats to retain precision.
Example:
Output:
Explanation: Here, we divided, multiplied and subtracted float operands. All the results were floats.
3. Complex Numbers
Python is unique in that it has built-in support for complex numbers. This means that you do not have to import any library to declare a complex number. You can simply use j for the imaginary part. In Python, the letter j is used to represent the imaginary part. Complex numbers always return complex numbers when arithmetic operations are performed on them.
Example:
Output:
Explanation: Here, we performed some arithmetic operations on complex numbers.
Arithmetic Operations with Type Conversion in Python
Python can perform arithmetic operations between two different data types, like between int and float, between int and complex numbers, or between float and complex numbers. Python applies implicit and explicit type conversion to convert the operands to a compatible type. This ensures that the operation does not result in unexpected behaviors and results.
In Implicit Type Conversion, Python automatically converts one data type to another during arithmetic operations when necessary. This is known as Type Coercion.
In Explicit Type Conversion, the programmer manually converts one data type into another using built-in functions like int(), float(), and complex(). This is known as Type Casting.
1. Int and Float
When an int and a float are used as operands, Python implicitly converts the int value into a float value before calculating the result of the operation. The result is always of float type.
Example:
Output:
Explanation: Here, we performed arithmetic operations using integers and floats. The result was of float data type.
2. Float and Complex
When an operation with a float operand and a complex number operand is performed, Python implicitly converts the float into a complex number with an imaginary part of 0j. The result is always of complex data type.
Example:
Output:
Explanation: Here, we added complex and float data type operand. The result was a complex data type.
3. Int and Complex
When an int and complex number are used as operands, Python converts the int data type to complex data type by assigning an imaginary part to the int. The result of such operation is of complex data type.
Example:
Output:
Explanation: Here, we used an integer and a complex number to perform multiplication. The result was a complex data type.
Conclusion
In this article, we covered Python arithmetic operators in detail. From basic arithmetic operations such as addition and division to more complex concepts such as precedence, associativity, and type conversion, all were explored here. We discussed how Python performs arithmetic calculations with various data types such as int, float, and complex numbers and how implicit and explicit type conversions take care of compatibility during Pythonic calculations. Understanding these mathematical ideas delivers a solid ground for coding more precise and effective code when working on real-world application projects.
To take your skills to the next level, check out this Python training course and gain hands-on experience. Also, prepare for job interviews with Python interview questions prepared by industry experts.
Python Arithmetic Operators – FAQs
Q1. What are Python arithmetic operators?
These are symbols that indicate the interpreter to perform mathematical calculations in Python
Q2. What are the seven types of operators in Python?
The seven Python operators are addition (+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//).
Q3. What does // do in Python?
// in Python is used to perform floor division. It returns the nearest whole number that is less than or equal to the result.
Q4. What happens if I divide by zero in Python?
Python will throw a ZeroDivisionError.
Q5. How do / and // operators differ in Python?
/ is a division operator, while // is a floor division operator.