Python

In Python, Arithmetic Operators are used with numeric values to perform common mathematical operations. These are the building blocks of any algorithm involving calculations, from simple addition to complex data processing.

1. Theoretical Overview

Python supports several arithmetic operators, including standard ones found in basic calculators and specific ones like "Floor Division" and "Modulus" which are vital in programming logic.

Types of Arithmetic Operators

OperatorNameDescription
+AdditionAdds values on either side of the operator.
-SubtractionSubtracts the right-hand operand from the left-hand operand.
*MultiplicationMultiplies values on either side of the operator.
/DivisionDivides the left operand by the right operand. Always returns a float.
//Floor DivisionDivides and returns the largest possible integer (rounds down).
%ModulusReturns the remainder of the division.
**ExponentiationPerforms "power" calculations (e.g., $2^3$).

2. Key Concepts & Behaviors

A. Division vs. Floor Division

In Python 3, the / operator performs floating-point division. Even if you divide two integers perfectly (like 4 / 2), the result is 2.0.

The // operator, however, performs integer division, discarding the decimal remainder.

B. The Modulus Operator (%)

This is extremely useful for logic tasks, such as:

  • Checking Parity: if x % 2 == 0: (checks if a number is even).

  • Cycling: Keeping a value within a specific range (like clock math).

C. Operator Precedence (PEMDAS/BODMAS)

Python follows standard mathematical order:

  1. Parentheses ()

  2. Exponentiation **

  3. Multiplication and Division (*, /, //, %)

  4. Addition and Subtraction (+, -)

3. Code Implementation

Python
# Initialize variables
a = 15
b = 4

# Basic Operations
print(f"Addition: {a} + {b} = {a + b}")         # 19
print(f"Subtraction: {a} - {b} = {a - b}")      # 11
print(f"Multiplication: {a} * {b} = {a * b}")   # 60

# Division Varieties
print(f"Float Division: {a} / {b} = {a / b}")   # 3.75
print(f"Floor Division: {a} // {b} = {a // b}") # 3 (removes .75)

# Remainder and Power
print(f"Modulus (Remainder): {a} % {b} = {a % b}") # 3
print(f"Exponent (Power): {a} ** {b} = {a ** b}")   # 50625

# Complex Expression (Order of Operations)
result = (10 + 2) * 5 / 2 ** 2
# Calculation: 12 * 5 / 4 -> 60 / 4 -> 15.0
print(f"Complex Result: {result}")

4. Special Use: String Concatenation

While technically an "Arithmetic Operator," the + sign can also be used with strings to join them together, and the * sign can repeat strings.

Python
print("Python" + "Is" + "Fun") # Concatenation
print("Echo! " * 3)            # Repetition
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now