Python arithmetic operators perform mathematical operations like addition, subtraction, multiplication, etc. Let’s understand the following:-
|+||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)
Do comment if you have any doubts or suggestions on this Python operators topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.