Arithmetic operators are used for performing mathematical operations like addition, subtraction, multiplication, etc.
Arithmetic operators in Python
Operator | Meaning | Example |
+ | Add two operands or unary plus | x + y
+2 |
- | Subtract right operand from the left or unary minus | x - y
-2 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus - the remainder of the division of left operand by the right | x % y (remainder of x/y) |
// | Floor division - division that results into the whole number adjusted to the left in the number line | x // y |
** | Exponent - left operand raised to the power of right | x**y (x to the power y) |
Example #1: Arithmetic operators in Python
x = 15
y = 4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
When you run the program, the output will be:
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
Comments
Post a Comment