Post

PythonPython

S
Swetha Shetty

Posted on 24th April 2024|13 views

0
votes

Python Order Of Operations

What is the order of operations in python?

Answers
P
divija reddy

Posted on 24th April 2024

The operators are (), +, -, *, **, / now let me let you the order

  1. ()
  2. **
  3. *
  4. /
  5. +
  6. -

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 *.

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos