如何在Python上执行操作顺序? [英] How do order of operations go on Python?

查看:257
本文介绍了如何在Python上执行操作顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:

  10-7//2*3+1 

我应该求解方程式.

使用PEMDAS时,我的答案似乎是8

And 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;

但是当将其放入python时,我得到了2.为什么?

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

似乎程序顺序是这样的:

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

我不明白

推荐答案

PEMDASPEMDAS;乘法和除法的优先级相同,加法和减法也是如此.当除法运算符出现在乘法运算之前,除法优先.

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.

执行Python运算符的顺序受运算符优先级,并遵循相同的规则.优先级较高的运算符在优先级较低的运算符之前执行,但是当它们在同一组中时,它们具有 matching 优先级.

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.

对于10-7//2*3+1,您有2种运算符,从最低到最高:

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

  • +, -(与AS == 加法和减法相关)
  • *, @, /, //, %(与MD相关,所以是乘法和除法).
  • +, - (correlating with AS == addition and subtraction)
  • *, @, /, //, % (correlating with MD, so multiplication and division).

因此,首先执行//*;乘法和除法属于同一,此处不是固定顺序(MD并不意味着乘法先于除法):

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

因此,首先执行7 // 2,然后乘以3.然后从10中减去,最后加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.

我们已经解决了一个问题,该问题不会影响您的特定情况,但是对于编写实际的Python程序非常重要. PEMDAS与操作的顺序无关.它不会决定事物的评估顺序.这实际上是关于参数分组的问题. PEMDAS表示a + b + c * d被评估为(a + b) + (c * d),但没有说先评估a + b还是c * d.

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.

在数学中,只要尊重参数分组,首先评估什么都没有关系.在Python中,如果仅在a() + (b() + c())中对b()c()进行了评估,只是因为它们在括号中,则可能会得到完全不同的结果,因为Python函数可能会产生副作用.

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表达式评估通常从左到右进行.例如,在a() + b() + (c() * d())中,评估顺序如下:

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

  • a()
  • b()
  • 第一个+,现在它的参数已经准备好
  • c()
  • d()
  • *,现在它的参数已经准备好
  • 第二个+,现在它的参数已经准备好
  • 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.

这篇关于如何在Python上执行操作顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆