Python – Operators
Python – Operators:
Operators in Python helps us to perform the mathematical operations with numbers. There are different operators in Python as below.
Operators | Description |
// | Integerdivision |
+ | addition |
– | subtraction |
* | multiplication |
/ | Float division |
% | Provide remainder after division(Modulus) |
** | Perform exponent (raise to power) |
Let us try implementing every operator now.
- Addition: symbol used (+)
>>> 10+10
20
>>>20+30
50
>>>50+50
100
- Substration: symbol used (-)
>>>20-10
10
>>>50-40
10
>>>100-30
70
- multiplication: Symbol used (*)
>>>5*2
10
>>>10*2
20
>>>20*2
40
- Float Division: This will divide and provide the result in floating value and the symbol used (/)
>>>5/2
2.5
>>>10/2
5.0
- Integer Division: This will divide and truncate the decimal and provide the Integer value and the symbol used (//)
>>>5//2
2
>>>7//2
3
- Exponentiation Operator: This will help us to calculate a power b and return the result
>>>10**3 # This mean 10*10*10
1000
- Modulus Operator: This will provide the remainder after the calucation and symbol used (%)
>>>10%3
1
What if we want to work with multiple operators at a time. Here comes the Operator precedence in Python.
Operator | Description |
lambda | Lambda expression |
if – else | Conditional expression |
or | Boolean OR |
and | Boolean AND |
not x | Boolean NOT |
in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, including membership tests and identity tests |
| | Bitwise OR |
^ | Bitwise XOR |
& | Bitwise AND |
<<, >> | Shifts |
+, – | Addition and subtraction |
*, @, /, //, % | Multiplication, matrix multiplication division, remainder [5] |
+x, -x, ~x | Positive, negative, bitwise NOT |
** | Exponentiation [6] |
await x | Await expression |
x[index], x[index:index], x(arguments…), x.attribute | Subscription, slicing, call, attribute reference |
(expressions…), [expressions…], {key: value…}, {expressions…} | Binding or tuple display, list display, dictionary display, set display |
Share: