Here we’re getting into writing programs to be used, actually used, on the command line, and that is so exciting. It’s been almost seven months since I made something to actually use, and while I have no real need for a sentence sorter, to have written a program that I can then manipulate in the py shell is REALLY REALLY NEAT, to me.
At first, I struggled a bit with some of the definitions, but I really enjoy separating the definitions from the actual what-you-want-it-to-do-ness of the program, as it were 🙂
"""Prints the first word after popping it off."""
word = words.pop(0)
"""Prints the last word after popping it off."""
word = words.pop(-1)
One of the Common Student Questions for this exercise, too, was “what is the difference between
return, and his answer is somewhat crummy, emphasis mine:
When should I print vs. return in a function?
You need to understand that print is only for printing to the screen and that you can actually do both print and return a value. When you understand this then you'll see that the question is kind of a pointless question. You use print when you want to print. You use return when you want to return.
So I do not really have a clear idea. It seems like once the function has done f(banana) to the stuff, you can either return the result or print it.
return seems more verb-y, but the end result seems identical. I will have to read about this!
As a whole this was not a challenging exercise, but it was really fun. I love that Zed Shaw focuses on being able to manipulate python via the structure-level command line rather than via an IDE, like the very popular IDLE which I will not link here. I’ve never imported a function in the shell to play with, and I feel like the possibilities have just expanded. Yeah!
If it’s the same as C/C++, printing should output to the screen, whereas returning should pass a variable back out of the fubction to where it was called. For example:
foo = myfunction();
Would set foo equal to whatever myfunction returned, as long as myfunction returned an integer in this case.
I agree, that is a really crummy worded explanation.
Oh that’s a very simple explanation, ok, neat! For its programming functionality, basically it sounds like RETURN can eat things and spit them out on command, where PRINT, well, yeah 🙂