有人能给我解释一下操作员优先吗?就像。模(%),整数除法(//),指数(**),第一,第二,第三,等等。另外,请帮助我完成以下代码的输出,并逐步解释:
print(8 % 3 ** 4 // 3 + 2)我不懂操作员优先。请帮帮忙
发布于 2022-10-28 07:09:56
这是从括号到逻辑或运算符的操作符优先顺序。如果表达式中有括号和逻辑或运算符,则为。括号将首先计算,逻辑或运算符将根据运算符优先级的顺序进行最后的计算。
结合性是指当您有相同的precedence.then运算符时,它从左到右计算。例如(2*3/3):如果表达式中有乘法*和地板除法//。然后,首先求乘法,然后进行地板除法,因为它们都有相同的precedence.it,然后从左到右依次求出相关性。
• Operators • Meaning
• () • Parentheses
• ** • Exponent
• +x, -x, ~x • Unary plus, Unary minus, Bitwise NOT
• *, /, //, % • Multiplication, Division, Floor division, Modulus
• +, - • Addition, Subtraction
• <<, >> • Bitwise shift operators
• & • Bitwise AND
• ^ • Bitwise XOR
• | • Bitwise OR
• ==, !=, >, >=, <, <=, is, is not, in, not in • Comparisons, Identity, Membership operators
• not • Logical NOT
• and • Logical AND
• or • Logical OR
print(8 % 3 ** 4 // 3 + 2)---exponent is evaluated first
print(8%81//3+2)----modulo and floor division have same precedence.so it evaluates from left to right . first evaluates modulo.
print(8//3+2)----then goes for floor division
print(2+2)-----then addition operation
so the output is 4.https://stackoverflow.com/questions/74231584
复制相似问题