E
E
Eugene2019-07-31 12:33:29
Python
Eugene, 2019-07-31 12:33:29

What is the order of evaluation in Python?

Hello!
There is such an expression:
10 - 2 + 3 - 6 =?
They write that the order of calculations in Python is as follows:
1. Expression in brackets;
2. Exhibitors;
3. Multiplication;
4. Division;
5. Addition;
6. Subtraction.
Those. addition comes first, and then subtraction. So, the answer to the expression at the beginning of the text position is "-1".
However, Python returns "5".
How so? O.o
Python just calculates in order from left to right?
Tried in Jupiter Notebook and Wing101

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladislav Lyskov, 2019-07-31
@Vlatqa

Those. addition comes first, and then subtraction. So, the answer to the expression at the beginning of the text position is "-1".

10 + 3 = 13
13 - 2 = 11
11 - 6 = 5
how did you get -1?

C
chupasaurus, 2019-07-31
@chupasaurus

Because subtraction is addition to a negative number, permuting the terms does not change the value.
The interpreter reads your expression as 10 + 3 - 2 - 6.

R
res2001, 2019-07-31
@res2001

Operation precedence in python.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question