Mathematical Operators
Mathematical operators in Python allow you to perform basic arithmetic operations on numbers. These operations are fundamental in any programming language and are used to manipulate data, perform calculations, and solve problems.
Operator | Description | Example |
---|---|---|
Mathematical Operators | ||
+ |
Addition | x + y |
- |
Subtraction | x - y |
* |
Multiplication | x * y |
/ |
Division | x / y |
** |
Exponentiation | x ** y |
% |
Modulus (x mod y) | x % y |
Addition (+)
Addition is used to add two numbers together.
x = 10
y = 5
result = x + y
print(result)
# Output: 15
Subtraction (-)
Subtraction is used to subtract one number from another.
x = 10
y = 5
result = x - y
print(result)
# Output: 5
Multiplication (*)
Multiplication is used to multiply two numbers.
x = 10
y = 5
result = x * y
print(result)
# Output: 50
Division (/)
Division is used to divide one number by another. Note that division in Python always results in a float, even if both operands are integers.
x = 10
y = 5
result = x / y
print(result)
# Output: 2.0
Exponentiation (**)
Exponentiation raises a number to the power of another number.
x = 2
y = 3
result = x ** y
print(result)
# Output: 8
Modulus (%)
The modulus operator returns the remainder of the division between two numbers. It’s useful for determining if a number is even, odd, or for cycling through a sequence.
x = 10
y = 3
result = x % y
print(result)
# Output: 1