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:
Operator | Name | Description |
---|---|---|
+ | Addition | Adds two operands |
- | Subtraction | Subtracts the second operand from the first |
* | Multiplication | Multiplies two operands |
/ | Division | Divides the first operand by the second, returns a float |
% | Modulus | Returns the remainder of the division |
** | Exponentiation | Raises the first operand to the power of the second |
// | Floor Division | Divides and returns the integer part (floors the result) |
Examples of Arithmetic Operators
1. Addition (+
)
The +
operator adds two numbers together.
2. Subtraction (-
)
The -
operator subtracts the second number from the first.
3. Multiplication (*
)
The *
operator multiplies two numbers.
4. Division (/
)
The /
operator divides the first number by the second and returns a floating-point result.
5. Modulus (%
)
The %
operator returns the remainder when the first number is divided by the second.
6. Exponentiation (**
)
The **
operator raises the first number to the power of the second.
7. Floor Division (//
)
The //
operator divides the first number by the second and returns the integer part of the result (it floors the result).
More Examples
Combining Multiple Arithmetic Operators
You can combine multiple arithmetic operators in a single expression, and Python will follow operator precedence.
Operator Precedence Example
Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) order when evaluating expressions:
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.