6.189 Homework 2 solution


Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment


5/5 - (2 votes)

Exercise 2.0 – Print vs Return
Note: This exercise is in section 2.2 of the 6.01 Course Notes. If you’ve already gone through it, you may skip this.
This isn’t really an exercise, just an important bit of reading. Download the template file homework 2.py. In it these two functions are defined:
def f1(x): print x + 1 def f2(x): return x + 1
Run this code in the shell. What happens when we call these functions?
f1(3) 4 f2(3) 4
It looks like they behave in exactly the same way. But they really don’t. Try this:
print f1(3) 4 None print f2(3) 4
In the case of f1, the function, when evaluated, prints 4; then it returns the value None, which is printed by the Python shell. In the case of f2, it doesn’t print anything, but it returns 4, which is printed by the Python shell. Finally, we can see the difference here:
f1(3) + 1 4 Traceback (most recent call last): File “