As an interesting sidenote: CPython, which has a peephole optimiser, is able to optimise away the "easy" case with constant folding. But the "hard" case only gets as far as folding the `3 ** 24`

.

```
>>> def foo():
return 7 ** 3 ** 24 % 25
...
>>>
def bar():
return (7 ** 3) ** 24 % 25
...
>>>
dis.dis(foo)
2 0 LOAD_CONST 1(7)
3 LOAD_CONST 5(282429536481)
6 BINARY_POWER
7 LOAD_CONST 4(25)
10 BINARY_MODULO
11 RETURN_VALUE
>>>
dis.dis(bar)
2 0 LOAD_CONST 7(1 L)
3 RETURN_VALUE
```

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. Python follows the same precedence rules for its mathematical operators that mathematics does.,Multiplication and both division operators have the same precedence, which is higher than addition and subtraction, which also have the same precedence. So 2*3-1 yields 5 rather than 4, and 5-2*2 is 1, not 6.,An exception to the left-to-right left-associative rule is the exponentiation operator **. A useful hint is to always use parentheses to force exactly the order you want when exponentiation is involved:,Operators with the same precedence (except for **) are evaluated from left-to-right. In algebra we say they are left-associative. So in the expression 6-3+2, the subtraction happens first, yielding 3. We then add 2 to get the result 5. If the operations had been evaluated from right to left, the result would have been 6-(3+2), which is 1.

`16 - 2 * 5 // 3 + 1`

2 ** 2 ** 3 * 3

The operator precedence in Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones).,We can see in the above table that more than one operator exists in the same group. These operators have the same precedence.,This program runs if block even when money is 0. It does not give us the desired output since the precedence of and is higher than or.,To evaluate these types of expressions there is a rule of precedence in Python. It guides the order in which these operations are carried out.

For example:

>>> 5 - 7 - 2

For example, multiplication has higher precedence than subtraction.

# Multiplication has higher precedence # than subtraction >>> 10 - 4 * 2 2

But we can change this order using parentheses `()`

as it has higher precedence than multiplication.

# Parentheses() has higher precedence >>> (10 - 4) * 2 12

**Output**

Lunch being delivered

We can get the desired output by using parenthesis `()`

in the following way:

# Precedence of or & and meal = "fruit" money = 0 if (meal == "fruit" or meal == "sandwich") and money >= 2: print("Lunch being delivered") else: print("Can't deliver lunch")

**Note**: Exponent operator `**`

has right-to-left associativity in Python.

```
# Shows the right - left associativity of **
# Output: 512, Since 2 ** (3 ** 2) = 2 ** 9
print(2 ** 3 ** 2)
# If 2 needs to be exponated fisrt, need to use()
# Output: 64
print((2 ** 3) ** 2)
```

Python Python Home date Samples String List Math Functions Built in Functions File Handling Error Handling Class Object Tkinter Numpy Pandas Python & MySQL SQLite , Post your comments , suggestion , error , requirements etc here

print(4 + 2 * 3) # Output 10 print(4 + (2 * 3)) # Output 10 print((4 + 2) * 3) # Output 18

*or*

```
math = 40
english = 50
avg = 45
if math >= 40 or english >= 50 and avg >= 50:
print("Passed")
else:
print("Failed")
```

```
math=40
english=50
avg=45
if math>=40 or english>=50 and avg>=50:
print("Passed")
else:
print("Failed")
```

Passed

`if math>40 or (english > = 50 and avg > 50) `

( if math > 40 or english > = 50) and avg > 50

## Associativity

print(10 / 2 * 6) # output is 30.0 print(24 / 6 //2)# output is 2.0

Operator precedence determines the order in which operations are processed. In this tutorial, you will perform a mathemagical trick using nested parentheses to control Python operator precedence. If you’re just joining us, you may want to start with our previous post, Python Operators are Mathematical!,Finally, we subtract our original number. It’s not necessary to wrap it in parentheses because it will be the last operation executed:,Let’s look at our magic trick again, this time using the rule of precedence with nested parentheses AND the magic of Python. Just to prove that it works with any number, I will use the unlucky integer 13. You may use any positive whole number you wish. Begin building your statement on your Python prompt, but don’t hit return until I say so.,We now return to the continuing saga at Rossum’s Universal Robot Hospital. Egos flare as Drs. Plus, Minus, Slash, Asterisk and Powers debate who has the most authority. An appeal is made to the president of the hospital, Parentheses. Like any good robot hospital, there is a hierarchy among operations.

>>> 1 + 2 * 3 7

>>> (1 + 2) * 3 9

>>> (13 + 5)

>>> ((13 + 5) * 2)

>>> (((13 + 5) * 2) - 4)

>>> ((((13 + 5) * 2) - 4) / 2)

Through the acronym, we can see that the bracket/parenthesis operator comes before the exponentiation operation in Python, according to the order of operations.,Finally, the results of both the bracket (8) and exponentiation (4) operators are then executed using the multiplication operator (M): 8 * 4 = 32.,The order of operations of the arithmetic operators can be remembered using the acronym BEDMAS.,The order of precedence of the membership operators in Python from left to right is in, not in.

X = (5 + 3) * 2 ** 2 print(X)

x = 3 + 8 * 2 ** 3 print(x) y = (4 * 2) + 6 / 3 - 2 print(y)

# using the assigm operator x = 6 y = 2 # to generate the Add AND operator i.e x + y = 8 x += y print(x) # to generate the subtract AND operator i.e x - y = 8 - 2 = 6 x -= y print(x) # to generate the multiply AND ooperator i.e x * y = 6 * 2 = 12 x *= y print(x) # to generate the divide AND operator i.e x / 2 = 12 / 2 = 6 x /= y print(x) # to generate the modulo AND operator 1. e x %= 2 = 6 % 2 = 0 x %= y print(x)

```
x = 6
y = 2
if x == y:
print('x is equal to y')
else:
print('x is not equal to y')
if x != y:
print('x is not equal to y')
else:
print('x is equal to y')
if x > y:
print('x is greater than y')
else:
print('x is not greater thab y')
if x < y:
print('x is less than y')
else:
print('x is not less than y')
if x >= y:
print('x is greater than or equal to y')
else:
print('x is not greater than or equal to y')
if x <= y:
print('x is less than or equal to y')
else:
print('x is not less than or equal to y')
```

# Precedence of 'or' & 'and' name = "Theophilus" age = 0 if name == "Theophilus" or name == "John" and age >= 2: print("Hello! Welcome.") else: print("Good Bye!!")

```
x = 2
y = 4
list = [1, 2, 3, 6, 7];
if (x in list):
print("Yes x is in the list")
else:
print("x can not be found on the list")
if (y not in list):
print("y is not in the given list")
else:
print("y is in the given list")
```

Posted on July 25, 2021 • tagged with python

Note: An expression can be represented as

`2 + 5 - 2`

where the operators are`+`

,`-`

and the operands (or values) are`2`

,`5`

,`2`

.

a = 5 + 4 - 3 + 2 + 1 print(a) # 9

Now for something a bit more involved:

b = 30 / 2 * 5 + (10 + 5) - 3

`print(b) # 87`

`print(b) # 87`

To avoid interfering with the expected operation of the generator expression itself, yield and yield from expressions are prohibited in the implicitly defined generator.,Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): -1**2 results in -1.,Due to their side effects on the containing scope, yield expressions are not permitted as part of the implicitly defined scopes used to implement comprehensions and generator expressions.,An asterisk * denotes iterable unpacking. Its operand must be an iterable. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking.

Python supports string and bytes literals and various numeric literals:

literal:: = stringliteral | bytesliteral | integer | floatnumber | imagnumber

A parenthesized form is an optional expression list enclosed in parentheses:

```
parenth_form:: = "(" [starred_expression]
")"
```

Common syntax elements for comprehensions are:

```
comprehension:: = assignment_expression comp_for
comp_for:: = ["async"]
"for"
target_list "in"
or_test[comp_iter]
comp_iter:: = comp_for | comp_if
comp_if:: = "if"
or_test[comp_iter]
```

A list display is a possibly empty series of expressions enclosed in square brackets:

```
list_display:: = "[" [starred_list | comprehension]
"]"
```

A set display is denoted by curly braces and distinguishable from dictionary displays by the lack of colons separating keys and values:

```
set_display:: = "{"(starred_list | comprehension)
"}"
```

A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces:

```
dict_display:: = "{" [key_datum_list | dict_comprehension]
"}"
key_datum_list:: = key_datum(","
key_datum) * [","]
key_datum:: = expression ":"
expression | "**"
or_expr
dict_comprehension:: = expression ":"
expression comp_for
```