Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

532
Visualizações
How do order of operations go on Python?

My question looks like this:

  10-7//2*3+1 

I am supposed to solve the equation.

My answer seems to come out as 8 when using PEMDAS:

First its's 2*3 = 6; 10-7//6+1
second = 7//6= 1; 10-1+1
Third = 10-8 = 8;

But when putting it into python, I get a 2. Why is this so?

It seems the programs order is as such:

first: 7//2=3; 10-3*3+1
second: 3*3=9; 10-9+1
third:10-9+1= 2; 2

I don't get it.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

PEMDAS is P, E, MD, AS; multiplication and division have the same precedence, and the same goes for addition and subtraction. When a division operator appears before multiplication, division goes first.

The order Python operators are executed in is governed by the operator precedence, and follow the same rules. Operators with higher precedence are executed before those with lower precedence, but operators have matching precedence when they are in the same group.

For 10-7//2*3+1, you have 2 classes of operators, from lowest to higest:

  • +, - (correlating with AS == addition and subtraction)
  • *, @, /, //, % (correlating with MD, so multiplication and division).

So // and * are executed first; multiplication and division fall in the same group, not a set order here (MD doesn't mean multiplication comes before division):

10 - ((7 // 2) * 3) + 1

So 7 // 2 is executed first, followed by the multiplication by 3. You then get the subtraction from ten and adding one at the end.


We've glossed over an issue that doesn't affect your particular case, but is very important for writing real Python programs. PEMDAS isn't really about order of operations; it doesn't decide what order things are evaluated in. It's really about argument grouping. PEMDAS says that a + b + c * d is evaluated as (a + b) + (c * d), but it doesn't say whether a + b or c * d is evaluated first.

In math, it doesn't matter what you evaluate first, as long as you respect the argument grouping. In Python, if you evaluted b() and c() first in a() + (b() + c()) just because they're in parentheses, you could get a completely different result, because Python functions can have side effects.

Python expression evaluation mostly works from left to right. For example, in a() + b() + (c() * d()), evaluation order goes as follows:

  • a()
  • b()
  • the first +, now that its arguments are ready
  • c()
  • d()
  • the *, now that its arguments are ready
  • the second +, now that its arguments are ready

This is despite the high precedence of * and the parentheses around the multiplication.

over 4 years ago · Santiago Trujillo Relatório

0

PEMDAS is better expressed as

P   Parentheses, then
E   Exponents, then
MD  Multiplication and division, left to right, then
AS  Addition and subtraction, left to right

So in your expression, the division should be done before the multiplication, since it is to the left of the multiplication. After those are done, then do the subtraction then the addition.

over 4 years ago · Santiago Trujillo Relatório

0

This is documented here (Python Documentation / Expressions / Operator-Precedence):

  • Multiplication and division, including integer division, happen on the same precedence level, so the order is determined by the direction in which operands are grouped:
  • Evaluation order of all the multiplications and divisions is left to right (like for most binary operations, except exponentiation).
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda