Python arithmetic operators perform mathematical operations like addition, subtraction, multiplication, etc. Let’s understand the following:-
Operator | Meaning | Example |
---|---|---|
+ | Add two operands or unary plus | x + y+ 2 |
– | Subtract the right operand from the left or unary minus | x – y- 2 |
* | Multiply two operands | x * y |
/ | Divide the left operand by the right one (always results in float) | x / y |
% | Modulus – the remainder of the division of the left operand by the right | x % y (remainder of x/y) |
// | Floor division – division that results in 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) |
Arithmetic operators in Python
A simple example code shows the basic arithmetic operations i.e. Arithmetic operators are used with numeric values to perform common mathematical operations:
x = 15
y = 5
print('x + y =', x + y)
print('x - y =', x - y)
print('x * y =', x * y)
print('x / y =', x / y)
print('x // y =', x // y)
print('x ** y =', x ** y)
Output:
These operators can be combined and used in expressions to perform more complex calculations. Additionally, parentheses can be used to specify the order of operations (precedence) when needed.
Do comment if you have any doubts or suggestions on this Python operators topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.