by Shelley Walsh ©2001
The simplest thing to do with Python is to simply use the IDE, sort of a desk calculator help you check your work. You can do these problems with great ease with Python, because the standard order of operations conventions are built in to it. There are just a few small differences.
So as long as you make these small changes, all you have to do to get Python to do your homework for you for this kind of problem is to type in the problem. Here are some examples from my The Order of Grouping Agreement article.
![]()
>>> 5+3*2**3
29
![]()
>>> 2+3*(3**2-5)
14

>>> 5*3**2-17-2+8-(2-3)**2
33
So, after you worked these out by hand step by step, you could just type them into the Python IDE and check your answer like I have done above. Now of course you can also do this with an ordinary calculator, but I think it is nicer in the Python IDE, because you can catch and correct your mistakes a lot easier. You can also check your work along the way. If your answer differs from Python's, take it apart in smaller pieces to narrow done where you might have gone wrong. For example if you disagreed on the first one, you might first check the 3*2**3 part. If you disagreed on that part, you might check 2**3.
1. Take a creative approach.
With Python you can use variables like the letters you use in algebra as storage places to keep things. You can use this approach to break down complicated expression into several simpler ones. Take an expression and see how many different ways you can use this to do it.
>>> 2*3**3+5*(2**3-3*4)
34
>>> A=2*3**3
>>> A
54
[You don't really have to do this step. I just doing it in hopes of making things extra clear by showing what calculating is being done along the way.]
>>> B=5*(2**3-3*4)
>>> B
-20
>>> A+B
34
>>> A=3**3
>>> A
27
>>> B=2**3-3*4
>>> B
-4
>>> 2*A+5*B
34
You might find it helpful to circle the groupings like I did in The Order of Grouping Agreement when you are doing this.
2. Special Case of 2.
Go from the outside-in instead of inside out. when you compute such expressions you go from inside-out, first working out the innermost parentheses, but it is often good to be able to think about them from the outside-in.
>>> 3*2**3+4*(5**2-2**2)
108
>>> A=3*2**3
>>> A
24
>>> B=4*(5**2-2**2)
>>> B
84
>>> A+B
108
[I have colored the expressions represented by A and B different colors to show the grouping. You might want to try doing that if it helps or if you don't have any different colors you can circle them when you write the expression on your paper.]
>>> A=3*2**3
>>> A
24
[narrowing in now and breaking down my original first piece into parts.]
>>> B=2**3
>>> B
8
>>> A=3*B
>>> A
24
>>> 4*(5**2-2**2)
84
[now doing the same with the other part]
>>> A=5**2-2**2
>>> A
21
>>> 4*A
84
>>> 5**2-2**2
21
>>> A=5**2
>>> A
25
>>> B=2**2
>>> B
4
>>> A-B
21
3. Reverse the process.
By using variables it is possible to write any complicated expression as several simpler expressions. I give you an expression written in the IDE one step at a time, and you have to condense it into a single expression. To do this all you have to do is keep on substituting things. You can check yourself along the way, by typing it into the IDE and making sure the answer comes out the same as the original.
>>> A=2+3
>>> A
5
>>> B=A**2
>>> B
25
>>> C=2**3
>>> C
8
>>> D=4*C
>>> D
32
>>> B+D
57
>>> A**2+4*C
57
>>> (2+3)**2+4*2**3
57
I particularly like this variation, so I'll give you some problems like it to work on your own.Type these into the Python IDE, and then try to condense them into single expressions that do the same thing. A good systematic way to do this is the way I did above. Just keep substituting things in working backward until you get down to all numbers. In the example above, first I replaced B and D with they were assigned to, A**2 and 4*C, and then I replaced A and C with what they were assigned to 2+3 and 2**3. It is good practice to put parentheses around anything you substitute in like this, but when they are implied by the order of operations agreement you can do without them, so I didn't need parentheses around the A**2 or the 4*C, because powers and multiplication come before addition, but there would be no harm in putting them in, because you could always remove them when you saw that they were unnecessary.
Now keep all this in mind and try these three problems. If you answer comes out the same as the original you are probably right. You can email me if you get stuck.
1.
>>> A=3*4
>>> A
12
>>> B=A/3
>>> B
4
>>> C=11+2
>>> C
13
>>> C-B
9
2.
>>> A=4.0-7.0
>>> A
-3.0
>>> B=A**2
>>> B
9.0
>>> C=-1*B
>>> C
-9.0
>>> D=C/9
>>> D
-1.0
>>> E=4.0*2.0
>>> E
8.0
>>> F=D+6
>>> F
5.0
>>> G=F-3
>>> G
2.0
>>> G-E
-6.0
3.
>>> A=4+-1
>>> A
3
>>> B=-2+3
>>> B
1
>>> C=A**2
>>> C
9
>>> D=B**2
>>> D
1
>>> C+D
10