Precedence of operators and associativity

Operators are executed in the order of precedence. The operands and the operators are grouped in a specific logical way for evaluation. This logical grouping is called an Association. All problem-solving questions having at least one expression. Python interpreter evaluates an expression of operators in the following ways.

The order of precedence

==> Upper groups have higher precedence compared to lower ones

Examples for precedence

==> Almost all arithmetic operators have left to right associativity

#Left-right associativity
>>>print(6 * 4 // 5)

In the above example, left-right –> step1 6*4 = 20 –> step2 20 // 5 = 4. Therefore the final output is 4

#left-right associativity, but parenthesis first 
>>>print(6 * (4 // 5))

left -right –> step1 –>4 // 5 = 0 step2 6*0 = 0. Therefore the final output is 0

Exponent

Note: In Python, the exponent is right to left associativity

#right - left associativity
print(2 ** 2 ** 3)

right – left–> step1 –>2**3 = 8 step2 2**8 = 256. Therefore the final output is 256

#right - left associativity
print((2 ** 2) ** 3)

right – left–> step1 –>2**2 = 4 step2 4**3 = 64. Therefore the final output is 64

Merge

Merge in Dictionaarys
X = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z= {**X, **y}
print(z)

The Final output is {‘a’: 1, ‘b’: 3, ‘c’: 4}

My Previous Post

How to Impress a Girlfriend using python: https://iterathon.tech//how-to-impress-a-girlfriend-using-python/

LEARN PYTHON EASY AND SIMPLE ❣

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *