Python Arithmetic Operators


Arithmetic Operators in Python

Arithmetic operators in Python are used to perform mathematical operations such as addition, subtraction, multiplication, and division. These operators work on numerical values (integers, floats) and perform the standard mathematical operations.

Here are the basic arithmetic operators:

OperatorNameDescription
+AdditionAdds two operands
-SubtractionSubtracts the second operand from the first
*MultiplicationMultiplies two operands
/DivisionDivides the first operand by the second, returns a float
%ModulusReturns the remainder of the division
**ExponentiationRaises the first operand to the power of the second
//Floor DivisionDivides and returns the integer part (floors the result)

Examples of Arithmetic Operators

1. Addition (+)

The + operator adds two numbers together.

a = 10 b = 5 # Addition result = a + b print(result) # Output: 15

2. Subtraction (-)

The - operator subtracts the second number from the first.

a = 10 b = 5 # Subtraction result = a - b print(result) # Output: 5

3. Multiplication (*)

The * operator multiplies two numbers.

a = 10 b = 5 # Multiplication result = a * b print(result) # Output: 50

4. Division (/)

The / operator divides the first number by the second and returns a floating-point result.

a = 10 b = 4 # Division result = a / b print(result) # Output: 2.5

5. Modulus (%)

The % operator returns the remainder when the first number is divided by the second.

a = 10 b = 3 # Modulus (remainder) result = a % b print(result) # Output: 1

6. Exponentiation (**)

The ** operator raises the first number to the power of the second.

a = 2 b = 3 # Exponentiation (2 raised to the power of 3) result = a ** b print(result) # Output: 8

7. Floor Division (//)

The // operator divides the first number by the second and returns the integer part of the result (it floors the result).

a = 10 b = 3 # Floor Division result = a // b print(result) # Output: 3

More Examples

Combining Multiple Arithmetic Operators

You can combine multiple arithmetic operators in a single expression, and Python will follow operator precedence.

a = 10 b = 5 c = 2 # Combine operators: (10 + 5) * 2 result = (a + b) * c print(result) # Output: 30

Operator Precedence Example

Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) order when evaluating expressions:

# Without parentheses, Python evaluates in the order of precedence result = 10 + 2 * 3 # Multiplication first, then addition print(result) # Output: 16 # With parentheses, you can change the precedence result = (10 + 2) * 3 # Parentheses first print(result) # Output: 36

Summary of Arithmetic Operators:

  • Addition (+): Adds two numbers.
  • Subtraction (-): Subtracts one number from another.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides one number by another, resulting in a float.
  • Modulus (%): Gives the remainder of a division.
  • Exponentiation (**): Raises a number to the power of another number.
  • Floor Division (//): Performs division and returns the integer part of the quotient.