filmov
tv
Python Tutorial: Manually Adjust Operator Precedence

Показать описание
Manually Adjust Operator Precedence In Python
In the previous tutorial, we looked at the operator precedence in Python and in this tutorial we are going to look at how to manually adjust operator precedence in Python. The ability for us to be able to instruct Python to evaluate an equation is a must have since we may want our equation to run in a certain order and we saw in the previous tutorial if an equation runs in an order that a programmer is not expecting it could through whole program off.
In Python we are able to control the operator precedence using parenthesis around the part of the equation we would like to run first. When a part of equation has parenthesis around it will run first before any other part of equation and then it will revert to its default order. Let's take a look at some examples of this.
Examples of Manually Adjusted Operator Precedence
#next two examples prove Python has an order
2 ** 2 + 2
6
2 + 2 ** 2
6
#A look at how to manually adjust the order
(2 + 2) ** 2
16
6 * (7 / 4)
10.5
7/4
1.75
1.75 * 6
10.5
6 + 5 - 3 + 4 * 10
48
(6 + 5 - 3 + 4) * 10
120
#Multi level
# inner most parenthesis will run first and then works its way out.
((6 + 5 - 3 + 4) * 10) ** 10
619173642240000000000
If you have any questions about how to manually adjust the operator precedence in Python leave a comment below and we will do our best to help you out.
Комментарии