Posted on 16th September 2024|17 views
What is the order of operations in python?
Posted on 16th September 2024| views
The operators are (), +, -, *, **, / now let me let you the order
Let me explain with the example assume there are three numbers 2,3,4 and if we are running the code with mixed operations
Example 1
If we are running this code
>>> 2+3*4
Output: 14
Example 2:
If we are running this code
>>>(2+3)*4
20
This difference is because the compiler will always follow the precedence rules. In the first example, 3*4 is calculated first, and then the result is added to 2 because * precedence is higher than +, but in the second example, 2+3 is calculated first. Then the result is multiplied with 4 because () precedence is higher than *.