In this exercise, we placed expressions and mathematical operations inside of the print() function. Is the input always evaluated first before anything is printed?
Yes, any expressions and operations inside the parentheses of the print() function is always evaluated first, with priority, then the result is printed out at the end. This can be seen to follow the general rule of the order of operations where anything inside of ( ) is evaluated with highest priority, which in this case would be anything inside the print() function’s ( ).
variables are not evaluated, they refer to concrete values
expressions are evaluated without delay whenever program control reaches them and they are executed, the question/answer here is misleading, print has nothing to do with that
If you want to “assign priorities” to a certain calculation or value inside your complete string of calculation the best to do it is to use parenthesis “()”. With parenthesis you will have the strict control of operation path performed by the code
mathematically speaking, the statement print print (26*68+13/28) returns an incorrect result, as it does not seem to follow BODMAS.
This needs to be enforced by creating separate variables and then adding them together:
a = 25*68
b = 13/28
I tried print ((26*68+13/28)), but it made no difference to print (26*68+13/28). I wonder if the output would be different based on IDE one is using…
Are you working with python2 or python3? In python2 dividing an integer by an integer will provide an integer (in this case, 0) which is probably not your intention and may explain any errors.
If it’s python3 then I’m not not sure what error you have when computing (26*68+13/28). There will be inevitable rounding errors for irrational numbers and there could be some floating point error which can differ from system to system but it should be fairly minor in a calculation like this.
If you wanted to make it more explicit ((26*68)+(13/28)) could be used but it shouldn’t make any difference for this particular calculation.
It’s worth noting that some calculations will differ with the addition of parantheses as expressions are generally calculated left to right when of the same order (floor division is especially noticeable), see the following for an example-
There are multiple possible outcomes for the above. Since multiplication and division take precedence to addition, that takes place first. If we want the addition done first, instead of last, we need to bracket that expression…
That is because of the grouping. The first operation will be to add 13 to 68. Then the multiplication will take place, followed by the division (left to right on equal precedence operators). Were you expecting something different?
My print command returned 1700.4642857142858 which is the result of solving the expression using the appropriate mathematical order of operations.
Many people might write the expression with parenthesis as (2568) + (13/28) to help limit confusion, but it isn’t technically required. If someone intended the operations to be performed sequentially, it would have to be written (2668+13)/28. Parenthesis in an expression are required when the usual order of operations is being superceded