Introduction
Arithmetic operators help us do math with numbers in programs. They let us add, subtract, multiply, and divide easily.
Arithmetic operators help us do math with numbers in programs. They let us add, subtract, multiply, and divide easily.
result = a + b # addition result = a - b # subtraction result = a * b # multiplication result = a / b # division result = a % b # remainder (modulus) result = a ** b # power (exponent) result = a // b # floor division (whole number division)
Use + to add numbers.
Use - to subtract one number from another.
x = 5 + 3 print(x)
y = 10 - 4 print(y)
z = 7 * 2 print(z)
a = 15 / 4 print(a)
This program shows all main arithmetic operations between 12 and 5 with clear labels.
a = 12 b = 5 print(f"Addition: {a} + {b} = {a + b}") print(f"Subtraction: {a} - {b} = {a - b}") print(f"Multiplication: {a} * {b} = {a * b}") print(f"Division: {a} / {b} = {a / b}") print(f"Modulus: {a} % {b} = {a % b}") print(f"Power: {a} ** {b} = {a ** b}") print(f"Floor Division: {a} // {b} = {a // b}")
Division / always gives a float result, even if numbers divide evenly.
Floor division // gives the whole number part of division, dropping decimals.
Modulus % gives the remainder after division.
Arithmetic operators let you do basic math in code.
Use +, -, *, / for add, subtract, multiply, divide.
Other useful operators: % (remainder), ** (power), // (floor division).
% gives the remainder after division, // gives the floor division result, ** is for power, and + is for addition.%, which returns the leftover part after dividing two numbers.** to calculate powers, so 5 ** 3 means 5 to the power of 3.5 ^ 3 is bitwise XOR, 5 ^^ 3 is invalid syntax, and pow(5, 3) is a function call but not an operator syntax.result = 17 // 4 print(result)
// divides and returns the largest whole number less than or equal to the result.num = 10 result = num % 0 print(result)
% cannot divide by zero; it causes an error.ZeroDivisionError at runtime.side. Which expression correctly uses arithmetic operators to do this?side ** 2 calculates side to the power of 2, which is correct for area.Pass the test and this pattern moves unknown