In programming, operators are special symbols or keywords that perform operations on one or more values (operands). These operations can range from mathematical calculations and comparisons to logical evaluations and assignments. Arithmetic operators are a subset of operators specifically designed to perform common mathematical calculations.
Here's a detailed look at common arithmetic operators:
1. Addition (`+`): Adds two operands together.
- Example: `5 + 3` results in `8`.
2. Subtraction (`-`): Subtracts the second operand from the first.
- Example: `10 - 4` results in `6`.
3. Multiplication (`-`): Multiplies two operands.
- Example: `6 - 7` results in `42`.
4. Division (`/`): Divides the first operand by the second. In many languages, this will result in a floating-point number, even if the division is exact.
- Example: `15 / 3` results in `5.0` (in Python and many other languages).
- Example: `10 / 4` results in `2.5`.
5. Modulus (`%`): Returns the remainder of the division of the first operand by the second. This is particularly useful for determining if a number is even or odd, or for cyclic operations.
- Example: `10 % 3` results in `1` (because 10 divided by 3 is 3 with a remainder of 1).
- Example: `12 % 2` results in `0`.
6. Exponentiation (``): Raises the first operand to the power of the second operand.
- Example: `2 3` results in `8` (2 to the power of 3, i.e., 2 - 2 - 2).
- Example: `5 2` results in `25`.
7. Floor Division (`//`): Divides the first operand by the second and returns the integer part of the quotient, effectively discarding any fractional part. The result is 'floored' towards negative infinity.
- Example: `10 // 3` results in `3`.
- Example: `10 // 4` results in `2`.
- Example: `-10 // 3` results in `-4` (flooring -3.33 to -4).
Operator Precedence and Associativity: Just like in mathematics, operators have an order of evaluation (precedence). For example, multiplication and division are performed before addition and subtraction. Parentheses `()` can be used to override this default precedence and explicitly define the order of operations.
Example Code
python
--- Arithmetic Operations Example ---
Define some variables
a = 20
b = 5
c = 3
print(f"Original values: a = {a}, b = {b}, c = {c}\n")
1. Addition (+)
result_add = a + b
print(f"Addition (a + b): {result_add}") Expected: 25
2. Subtraction (-)
result_sub = a - b
print(f"Subtraction (a - b): {result_sub}") Expected: 15
3. Multiplication (-)
result_mul = b - c
print(f"Multiplication (b - c): {result_mul}") Expected: 15
4. Division (/)
result_div = a / b
print(f"Division (a / b): {result_div}") Expected: 4.0 (float division)
5. Modulus (%)
result_mod = a % c
print(f"Modulus (a % c): {result_mod}") Expected: 2 (20 divided by 3 is 6 with a remainder of 2)
6. Exponentiation ()
result_exp = b c
print(f"Exponentiation (b c): {result_exp}") Expected: 125 (5 to the power of 3)
7. Floor Division (//)
result_floor_div = a // c
print(f"Floor Division (a // c): {result_floor_div}") Expected: 6 (integer part of 20 / 3)
--- Operator Precedence Example ---
Multiplication/Division before Addition/Subtraction
(5 + 3) - 2 vs 5 + 3 - 2
prec_example1 = 5 + 3 - 2
print(f"\nPrecedence Example (5 + 3 - 2): {prec_example1}") Expected: 11 (3 - 2 = 6, then 5 + 6 = 11)
prec_example2 = (5 + 3) - 2
print(f"Precedence Example ((5 + 3) - 2): {prec_example2}") Expected: 16 (5 + 3 = 8, then 8 - 2 = 16)








Operators and Arithmetic Operations