Do print() functions evaluate the input before it prints?

Question

In this exercise, we placed expressions and mathematical operations inside of the print() function. Is the input always evaluated first before anything is printed?

Answer

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 ( ).

11 Likes

A post was split to a new topic: How did the variable C understood that its assigned to this mathematics

Aside from mathematical operations, can one then assign priorities to a set of variables for Python to evaluate in the background before output?

code_newbie

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

2 Likes

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

3 Likes

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
print(a+b)

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…

2 Likes

none of that is right

1 Like

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-

Python3 operator precedence-

4 Likes

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…

25 * (68 + 13) / 28 => 25 * 81 / 28 => 2025 / 28 => 72.32142857142857
8 Likes

I was using whichever Python is running on the codeacademy website, not tried it on my laptop. But also I think I was not using floats… so who knows.

Yeah, that’s helpful… Thanks lol

Thanks guys! This is all really helpful. By the time I want to ask my questions I can already see the answer here.

1 Like

We can print answer simply by 2 ways

print( 25 * 68 + 13 / 28)

or

a = 25
b = 68
c = 13
d = 28

print( a * b + c / d )

Am I right ??

So we can. In either case, what is the expected outcome? To a reader, what is the meaning of each term? Is b + c intended to be a significant value?

1 Like

As we have declared! I think so. :slight_smile:

Could you furnish us with more detail, please? There are a couple of unanswered questions.

I got exactly the same result by printing : (25*(68+13)/28)

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

Keep in mind that the only superseding needed is for the addition. Notice how Python evaluates this.

>>> 13 / 28
0.4642857142857143   # interpreter rounding a repeating decimal
>>> 26 * 68
1768
>>> 26 * 68 + 13 / 28
1768.4642857142858  # likewise rounding by interpreter
>>> 

This proves that the division after the plus sign takes place before the addition.

Do we want to add 13 to 68 before we divide by 28, or simply add 13 / 28?

1 Like