Arithmetic Operators in Python
Python Arithmetic Operators
Python provides several arithmetic operators to perform common mathematical operations on numeric values such as addition, subtraction, multiplication, division, and more. These operators can be applied to integers, floats, and even complex numbers.
Basic Arithmetic Operators
1. Addition (+)
The +
operator adds two values together.
Example:
python
1x = 10
2y = 5
3result = x + y
4print(result) # Output: 15
2. Subtraction (-)
The -
operator subtracts one value from another.
Example:
python
1x = 10
2y = 5
3result = x - y
4print(result) # Output: 5
3. Multiplication (*)
The *
operator multiplies two values.
Example:
python
1x = 10
2y = 5
3result = x * y
4print(result) # Output: 50
4. Division (/)
The /
operator divides one value by another. The result is always a floating-point number.
Example:
python
1x = 10
2y = 5
3result = x / y
4print(result) # Output: 2.0
Python Advanced Arithmetic Operators
5. Modulus (%)
The %
operator returns the remainder of a division.
Example:
python
1x = 10
2y = 3
3result = x % y
4print(result) # Output: 1
The modulus operator is useful when you need to determine whether one number is divisible by another.
6. Exponentiation (**)
The **
operator raises one value to the power of another.
Example:
python
1x = 2
2y = 3
3result = x ** y
4print(result) # Output: 8
In this case, 2 ** 3
is equivalent to 2 raised to the power of 3.
7. Floor Division (//)
The //
operator divides one value by another but returns only the whole number (integer) part of the division, discarding the remainder.
Example:
python
1x = 10
2y = 3
3result = x // y
4print(result) # Output: 3
Floor division is useful when you only need the integer result without the fractional part.