Arithmetic Operators in Python
The operators used for the four operations are already known. Operators + are used for addition, - for subtraction, * for multiplication, and / for division.
% is used to get the mode, that is, to find the remainder in the division operation.
a = 16 % 5 # the result will be 1.
By using // (double slash), we can find the result of the division operation, that is, the quotient . As a result of this operation, an integer is obtained by ignoring the remainder.
x = 16 // 5 # the result will be 3.
** (double asterisk) is used for exponentiation (calculating the power of a number).
result = 2**3 # the result will be 8.
Assignment (Valuation) Operators
The operators that can be used for assignment in Python are as follows:
Operator |
Sample |
equivalent |
= |
x = 10 |
|
+= |
x += 10 |
x = x + 10 |
-= |
x -= 10 |
x = x - 10 |
*= |
x *= 10 |
x = x * 10 |
/= |
x /= 10 |
x = x / 10 |
%= |
x % = 10 |
x = x 10% |
//= |
x //= 10 |
x = x // 10 |
**= |
x **= 10 |
x = x ** 10 |
&= |
x &= 10 |
x = x & 10 |
|= |
x |= 10 |
x = x | 10 |
^= |
x ^= 10 |
x = x^10 |
>>= |
x >>= 10 |
x = x >> 10 |
<<= |
x <<= 10 |
x = x << 10 |
Comparison Operators
Operator |
Explanation |
== |
if equal |
!= |
if not equal |
> |
if big |
< |
if small |
>= |
If greater or equal |
<= |
If less than or equal |
Logical Operators
Operator |
Explanation |
Sample |
and |
And - If the result of all the operations is True, the result is also True. If the result of even one of the comparison operations is False, the result will also be False. |
x > 0 and x < 100 |
or |
Or - If the result of one of the operations is True , the result is also True . If all operations return False, the result will be False. |
x > 50 or y > 50 |
note |
Not - Inverts the result of the logical operation. True is False, False is True. |
note(x < 5) |
Consider the following examples of logical operators.
x = 75
y = 45
z = 50
k1 = x > 50 or y > 50 or z > 50
k2 = x >= 50 and y >= 50 and z >= 50
k3 = grade ( x > 100 )
In the above example, the value of the variable k1 will be True. The k2 variable will be False and the k3 variable will be True.
python mathematical operators, python operators, python logical operators, python logical operations, python logical operations examples, python mathematical operations examples
EXERCISES
There are no examples related to this subject.
Read 959 times.