Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Types and Branching Numeric

If in the order of operations addition comes before substraction, why didnt you add 15 with 8 first then do 10 - 23?

If in the order of operations addition comes before substraction, why didnt you add 15 with 8 first then do 10 - 23?

2 Answers

Joseph Yhu
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Yhu
PHP Development Techdegree Graduate 47,388 Points

Addition does not come before subtraction in the order of operations. They have the same precedence, so the expression will be evaluated from left to right.

Oh okay thanks because he said in PEMDAS addition comes first had me confused

Hey Joseph could you elaborate more on this? I'm still confused even after reading your explanation, and the link in the teacher's notes didn't help.

Joseph Yhu
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Yhu
PHP Development Techdegree Graduate 47,388 Points

To use his example: 10 - 15 + 8, you would do the subtraction first because it comes before the addition (i.e. 15 + 8) and addition and subtraction have the same precedence.; however, if the expression was, e.g.: 10 - 15 * 8, you would do the multiplication first because multiplication has higher precedence over subtraction.

It's helps to understand if you remember that subtraction is simply addition by a negative: 10 - 15 + 8 is the same as 10 + (-15) + 8.