Python Arithmetic Operators

python-arithmetic-1.webp

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. 

Understanding 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.

Advance Your Career with Python
Learn from industry pros and build real-world projects.
quiz-icon

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 for Addition in Python:

Python

Output:

Python operators, Arithmetic operators in Python for addition

Explanation: Here, we have added 4 and 5.

2. Subtraction (-)

This operator takes in two operands and calculates their difference.

Example for Subtraction in Python:

Python

Output:

Arithmetic operators in Python, Python math operators for subtraction

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 for Multiplication in Python:

Python

Output:

Multiplication Python math operators

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 for Division in Python:

Python

Output:

Division  python arithmetic operators

Explanation: Here, we have divided 4 and 5, which resulted in 0.8.

5. Modulus (%) 

Python modulus operator returns the remainder after dividing the first operand by the second operand.

Example for Modulus in Python:

Python

Output:

Python modulus operator, mathematical operations in Python

Explanation: Here, we have implemented a Python modulus operator 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 for Exponentiation in Python:

Python

Output:

Exponentiation, mathematical operations in Python

Explanation: Here, we have raised 4 to the power of 5, which resulted in 1024. 

7. Floor Division (//) 

Python floor division operator divides the first operand by the second operand and returns the largest integer less than or equal to the result.

Example for Floor Division in Python: 

Python

Output:

Floor Division, Python floor division

Explanation: Here, 4 divided by 5 is 0.8. Python floor division returns 0, which is the nearest integer smaller than the result. 

Get 100% Hike!

Master Most in Demand Skills Now!

Summary Table of Arithmetic Operators in Python

The difference between /, //, and % in Python are listed below:

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

Operator Precedence and Associativity 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 for Operator Precedence in Python:

Python

Output:

Precedence and Associativity of Operators in Python

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 for Associativity in Python:

Python

Output:

Associativity, mathematical operations in Python

Explanation: Here, operator precedence and associativity in Python, 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 in Python

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 Integers in Python:

Python

Output:

Integers, mathematical operations in Python

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 for Float in Python:

Python

Output:

Float, Python integer division vs float division

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 for Complex Numbers in Python:

Python

Output:

Complex Numbers,Python arithmetic operator tutorial

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 for Int and Float:

Python

Output:

Int and Float, Python arithmetic operator tutorial

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 a complex data type.

Example for Float and Complex in Python:

Python

Output:

Float and Complex, Python integer division vs float division

Explanation: Here, we added complex and float data type operands. The result was a complex data type. 

3. Int and Complex 

When an int and a complex number are used as operands, Python converts the int data type to the complex data type by assigning an imaginary part to the int. The result of such an operation is a complex data type.

Example for Int and Complex in Python:

Python

Output:

Int and Complex , Python integer division vs float division

Explanation: Here, we used an integer and a complex number to perform multiplication. The result was a complex data type.

Common Mistakes to Avoid with Python Arithmetic Operators

1. Confusing / and // in Python Division
One of the top mistakes beginners make with Python arithmetic operators is using / when they actually need //. The / operator performs float division, which 7 / 2 results in 3.5. In contrast, // performs Python floor division, which removes the decimal and returns an integer—7 // 2 equals 3. This is crucial when doing loop counters or index-based operations where integers are expected. Misusing them can break your logic completely.

2. Incorrect Use of the Python Modulus Operator %
The % symbol in Python math operators doesn’t mean percentage—it returns the remainder after division. For example, 10 % 3 returns 1. But here’s what throws many off: -10 % 3 returns 2 in Python due to how the language handles negative numbers. This behavior differs from many other languages, and not understanding this can lead to unexpected results in logic related to even/odd checks or cyclic operations.

3. Ignoring Operator Precedence in Python Calculations
Another critical error is not respecting operator precedence in Python. For example, in 2 + 3*5, Python evaluates the multiplication first, giving 17 and not 25. Similarly, exponentiation has higher precedence than multiplication and is right-associative, so 2**3**2 becomes 2**9, which is 512, not 64. Always use parentheses to ensure your mathematical operations in Python run in the correct order.

4. Mixing Integer and Float Without Understanding the Result
Using both integers and floats in the same operation can cause confusion. For instance, 4 + 5.0 returns 9.0, not 9. Python automatically promotes the result to a float. In contexts like Python integer division vs float division, this can silently introduce bugs or mismatched data types. It’s important to keep data types in mind and convert explicitly if needed.

5. Skipping Proper Tutorials and Practice with Python Operators
Many beginners try to learn through trial and error, skipping a structured Python arithmetic operator tutorial. This often results in a shallow understanding of core behaviors like operator associativity in Python, float precision issues, and common traps with // and %. Investing time in learning the basics of Python operators thoroughly helps avoid long-term problems in code quality and logic.

Real-Life Use Cases of Arithmetic Operators in Python

1. Financial Calculations and Budgeting
Arithmetic operators in Python are heavily used in budgeting apps and expense trackers. For example, + and - handle income and expenses, while / and * help calculate averages or project future savings. The Python math operators make it simple to build financial models that scale dynamically.

2. Gaming Logic and Score Systems
In game development, arithmetic operators in Python manage player scores, levels, and health bars. For instance, += increases score, % determines game loops or turns, and // helps manage experience point thresholds. These Python operators control core gameplay mechanics smoothly.

3. Data Analysis and Reporting
Python arithmetic operators are essential in data processing. Whether calculating averages with /, totals with +, or identifying outliers using -, these tools are key in generating reports or analytics dashboards. They’re widely used in pandas, NumPy, and other Python data science tools.

Start Your Python Journey – 100% Free
Learn the basics of Python through hands-on practice and projects.
quiz-icon

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 operations in Python delivers a solid foundation 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.

 

Learn how to manage files and use modules effectively with these Python Basics blogs

How to Parse a String to a Float or Int in Python – Follow step-by-step instructions to parse a string into a float or int using Python.
Python Comparison Operators – Use this guide to learn Python comparison operators through detailed step-by-step instructions.
Python Assignment Operator – Understand how to use the assignment operator in Python with easy, step-by-step directions.
Python Bitwise Operators – Learn how bitwise operators work in Python by following these step-by-step examples.
Python Membership and Identity Operators – Explore membership and identity operators in Python through a guided, step-by-step approach.
Python Docstrings – Gain a solid understanding of Python docstrings with these simple, step-by-step instructions.
Access Environment Variable Values in Python – Access environment variable values in Python by following this straightforward, step-by-step guide.
Python: How to Get the Last Element of List – Use this step-by-step tutorial to get the last element of a list in Python.
Access Index Using For Loop in Python – Learn how to access an index during a for loop in Python with clear, step-by-step examples.

Python Arithmetic Operators – FAQs

Q1. What are Python arithmetic operators?

These are symbols that indicate the interpreter to perform mathematical calculations in Python

Q2. How many arithmetic operators are there in Python?

Python has 7 arithmetic operators: +, -, *, /, //, %, and **.

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. What is the difference between / and // in Python?

/ is a division operator, while // is a Python floor division operator.

Q6. What are the seven types of operators in Python?

The seven Python operators are addition (+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//).

Q7. Can you use arithmetic operators with floats and complex numbers?

Yes, all arithmetic operators work with floats and complex numbers, including mixed types like int + float or float + complex

Q8. Is the exponentiation operator ** left or right-associative?

The exponentiation operator ** is right-associative, so 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2).

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner