Python Tutorial: Operator Precedence in Python - Python Numbers #34

preview_player
Показать описание

Operator Precedence in Python
In this Python tutorial we will look at the operator precedence in Python understanding the order in which Python will run your equations are very important. If the equation runs out of order, we could end up with a completely different answer than we expected. Python has set rules for when certain mathematical operators but these are not set in stone. We can actually control the order and we will take a look at that in next tutorial.

Order of Precedence
In the list below the operator above will always run before the operator below unless it has been manually adjusted which we will see in the next tutorial.

** - Exponent
*, /, %, // - Multiplication, true division, modulo, floor division - In the case where the equation just contains these operators the equation will run from left to right of the actual equation.
+, - - Addition and Subtraction are at the bottom of the list.
Examples of Order of Precedence in Python
We will look at which order the equation will run. The order could have some serious complications on our equation if it runs in an order we are not expecting.

Example Using Exponent and Subtraction

5 ** 6 - 3
15622

#Example Break Down
5 ** 6
15625
15625 - 3
15622
Example Using Multiplication and True Division

5 * 6 / 2
15.0

#Break Down of The Equation
5 * 6
30
30 / 2
15.0
Example Using Multiplication and Addition

6 * 5 + 3
33
5 + 3 * 3
14
Example Using Addition and Subtraction
5 + 5 - 5
5
Now this is not too difficult just a quick study of the order you will remember it forever. We will explore how to manually adjust the precedence in the next tutorial. If you have any questions please leave a comment below.
Рекомендации по теме
Комментарии
Автор

PEMDAS - an acronym to help remember which mathematical operators precedence. The abbreviation is short for : Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction

ifoundthistoday
Автор

Please Can someone explain this to me (True==True) and false

efeonobrakpeya