Learnitweb

Python Arithmetic Operators Tutorial

In this tutorial, we will discuss Arithmetic Operators in Python.
Although these operators look similar to those in C, C++, Java, and other languages, Python includes a few additional operators and some important behavioral differences that every developer must understand—especially regarding division and floor division.

This tutorial covers all seven arithmetic operators in Python, their rules, examples, edge cases, and their behavior with numbers, strings, and even boolean values.


1. Introduction to Arithmetic Operators

Python provides the following arithmetic operators:

• Addition (+)
• Subtraction (−)
• Multiplication (*)
• Division (/)
• Modulo (%)
• Floor Division (//)
• Exponentiation (**)

The first five operators exist in almost all programming languages.
Python adds two additional operators: floor division (//) and exponentiation ()**.


2. Basic Arithmetic Operators

Let us begin with very simple numeric examples.

a = 10
b = 2

print(a + b)    # 12
print(a - b)    # 8
print(a * b)    # 20
print(a % b)    # 0

When b = 3:

a = 10
b = 3

print(a + b)    # 13
print(a - b)    # 7
print(a * b)    # 30
print(a % b)    # 1 (because remainder of 10/3 is 1)

These operators behave exactly as expected from basic arithmetic.


3. Division Operator (/)

This is where Python differs from many other languages.

In Python 3, the division operator always returns a float value.

Example:

print(10 / 2)

Output:

5.0

Even though 10 and 2 are integers, the result is a float.

Another example:

print(10 / 3)

Output:

3.3333333333333335

Key rule:

/ always performs floating-point division
• The result is always a float, regardless of operand types


4. Floor Division Operator (//)

This operator is unique to Python.

Floor division returns:

• The largest integer less than or equal to the division result
• But the final type (int or float) depends on the operands

4.1 When both operands are integers

print(10 // 2)   # 5
print(10 // 3)   # 3

Explanation:

• Normal division: 10 / 3 = 3.3333
• Floor value: 3.0
• Since both arguments are int → result is int

4.2 When one operand is float

print(10.0 // 3)   # 3.0

Explanation:

• Floor value still 3
• But because one argument is float → result is float

Summary of // rules

• Works for integers and floats
• If both operands are int → result is int
• If at least one operand is float → result is float

Examples:

print(20 // 2)      # 10
print(20.5 // 2)    # 10.0
print(30 // 2)      # 15
print(30.0 // 2)    # 15.0

5. Exponentiation Operator ()**

This operator performs power calculation.

print(10 ** 2)   # 100
print(3 ** 3)    # 27

Meaning:

10 ** 2 = 10²
3 ** 3 = 3³

Python supports large integers, so exponentiation can compute extremely high powers.


**6. Operator Behavior With Strings: + and * **

Python allows certain arithmetic operators to work with strings.


6.1 Plus Operator on Strings → Concatenation

print("Durga" + "Soft")

Output:

DurgaSoft

Important Rule

If you apply + on strings:

Both operands must be strings only

Example of error:

print("Durga" + 10)

Python error:

TypeError: must be str, not int

Valid fix:

Convert the number to string:

print("Durga" + str(10))    # Durga10

6.2 Star Operator on Strings → Repetition

print("Durga" * 3)

Output:

DurgaDurgaDurga

You can also write:

print(3 * "Durga")

Same output.

Important Rule

When applying *:

• One operand must be a string
• The other must be an integer

Invalid:

print("Durga" * "3")

Error:

TypeError: can't multiply sequence by non-int type 'str'

Fix:

print("Durga" * int("3"))   # DurgaDurgaDurga

7. Multiplying Strings With Booleans (True/False)

This is an interesting Python behavior.

Boolean values in Python internally behave like integers:

True → 1
False → 0

Example:

print("Durga" * True)     # equivalent to "Durga" * 1

Output:

Durga
print("Durga" * False)    # equivalent to "Durga" * 0

Output:

''

(Boolean repetition is allowed because booleans behave as integers.)


8. Division, Floor Division, Modulo With Zero

Any arithmetic operation with zero as the divisor results in an error.

Examples:

print(10 / 0)
print(10 // 0)
print(10 % 0)

All result in:

ZeroDivisionError: division by zero

This applies to:

• Normal division
• Floor division
• Modulo
• Float or int operands


9. Summary of All Arithmetic Operators

OperatorMeaningPython Behavior
+AdditionSupports numbers, string concatenation
-SubtractionOnly for numbers
*MultiplicationWorks for numbers and string repetition
/True DivisionAlways returns float
//Floor DivisionReturns int or float depending on operands
%ModuloReturns remainder
**ExponentiationPower operator

10. Key Takeaways

• Python supports 7 arithmetic operators
/ always returns float
// returns floor value → int or float based on operands
** performs power calculation
+ on strings concatenates
* on strings repeats
• One operand must be string, other integer for repetition
• Boolean values act as integers (True=1, False=0)
• Any division/modulo by zero raises ZeroDivisionError