Re: [Tutor] How to teach Python
Thanks to everyone for your helpful answers! -Elaine __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to teach Python
hi elaine, welcome to Tutor. this list is really for those learning Python and/or programming, so i'm going to assume that your question is directed to mainly tutors, but perhaps tutees in reference to their experience in learning Python and how it was/is taught to them. if you want to address teachers and other educational individuals, you should probably post to the EDU-SIG mailing list -- i'd recommend cross-posting there if you wish: http://mail.python.org/mailman/listinfo/edu-sig there have been so many good responses so far that i can just say that you will see "as others have said" quite often in my reply here. > I am going to be teaching "Introduction to Python > Programming" in the Fall at Foothill College in Los > Altos Hills, CA. that's great! i am also a Python teacher professionally and happen to live just 5 minutes away from the campus. if you ever need a substitute and don't wish to cancel class for any some reason, drop me a line. > I have been teaching programming for 20 years now, > including Java and C++ as of late. i have been teaching since 1983... BASIC, C, and Python are the programming languages i have taught over the years. > 1) Programming Style > while 1: > x = next() > if not x: break > > I have never allowed students to use break or continue > in any programming class I have taught. I think of this > type of code as belonging with the dreaded GOTO. > > I have always thought of break and continue as hacks that > are not compatible with structured programming techniques. > The only exception is using break in a switch in C, C++ and > Java. > > However, all Python books seem to contain sample > programs like this one. Do you think that code like > this is desirable? as others have mentioned already, because assignments are not expressions in Python, it is quite a common idiom and should be covered and implemented in this manner to keep the code cleaner and more consistent. remember that "readability counts." its are in all the books because it is one of those basic idioms that ppl *have* to use to get things done. also as others have said, GOTO does not have the stigma that it once had and that there is nothing wrong in principle with break and continue when used with loops, which is what they were designed to do. you pretty much have to be a very experienced programmer in order to know when and where are the right places for that beast. with that said, it would be a good idea to review the Zen to truly understand the philosophy of Python. it is one thing teaching and learning Python, but it is another step when you are able to understand it and program in a "Pythonic" way. (if away from the internet or a book, you can access then Zen using 'import this' or 'python -m this' from the shell.) > 2) Since this is an introductory class, I am tempted > to leave out "optional" topics like argument matching > modes, walk, map, filter, reduce, apply. Do you think > these are required for any Python programmer? list comprehensions (and generator expressions) have obsoleted map() and filter() because in general, their performance is better, but that is not always the case. apply() was deprecated by the new function invocation mechanism using * and ** back in Python 1.6. reduce() will stick around. i'm not sure what "walk" is, and argument matching is important. in fact, you need to cover standard/formal/required argus, default args, keyword args, and variable args (* tuples and ** dictionaries). > 3) I need to teach a GUI in this class. I would like > something that is easy, standard, and multi-platform. > I am considering Tkinter and Jython. Any opnions on > this? Tkinter was chosen as the default for Python way back in the early days because that was easy to understand and teach and was available on the big 3 platforms (Unix, Win, Mac). i teach this in my courses (and also in "Core Python," the textbook i wrote for college-level courses as well as for working professionals), but also try to cover more advanced and related widget sets like Pmw and Tix. if you want to teach the most basic concepts, even leaving off event-driven programming, take a look at EasyGUI. since then, however, it is no longer the only GUI toolkit. wxWindows (and its adapter wxPython) has grown in userbase quite a lot in the last few years -- it is also cross-plat- form. one of its greatst features is that it uses a system's native widget set. in other words, GUIs written on Win32 look like Windoze apps, and the same for Mac and *ix. there are some nice "intro" libraries like PythonCard. for building GUIs in wxPython, one of the most painless ways is using Boa Constructor. you can drag-n-drop widgets onto a canvas. Boa writes the boilerplate code, and you only have to worry about supplying the callbacks. fwiw, i teach both an "Introduction to Programming using Python" course as well as an (intensive) "Introduction to Python" programming c
Re: [Tutor] How to teach Python
Hi Elaine, There is a separate mailing list for people who teach Python as opposed to people learning Python. They might have a more pertinent view on this. However, for what its worth here are my views... > students who already know one programming language. The language they know might make a big difference. For example if they know Lisp or Smalltalk then the aspects of Python that will be familiar are very diffferent than if they know C++/Java. > 1) Programming Style > > while 1: > x = next() > if not x: break Personally I don;t like this style either, its not intuitive and harder to read, but it is now the standard Python idiom in many cases. The reason for this is that you cannot use assignment in the test in Python, as you can with C/C++ etc so you would need to repeat the assignment: x = func() while x: doit() x = func() Some folks don't like the double assignment and so prefer while 1 x = func() if not x: break doit() Personally I prefer the first version... > I have never allowed students to use break or continue > in any programming class I have taught. I think of this type > of code as belonging with the dreaded GOTO. GOTO is no longer consider quite as evil as it used to be... :-) It just has to be used with discretion. > complex conditions in a loop use multiple if's with > break's I agree, and this can be a significant barrier to the understanding and use of boolean expressions. Its quite common to see that on this mailing list, although I admit we very rarely pick it up and comment. > inside the loop instead. Also, it is MUCH easier to > read a loop if the condition for continuing in the loop is > right up at the top of the loop where it belongs. Again I agree. > I have always thought of break and continue as hacks > that are not compatible with structured programming > techniques. In a pure sense you are right because strictly speaking structured programming requires a single exit point in a function or block. But break/continue are not the only culprits here, multiple return statements have the same effect. In practice multiple returns and break/continue can improve the readibility of code significantly as well as the performance. (And this is also true of GOTO which is why academics are giving GOTO a cautious rebirth) > The only exception is using break in a switch in C, > C++ and Java. And that's purely because the switch statement, like so much else, is a badly designed construct in both languages. The default position should be to exit the case condition at the end of the block, not fall through to the next block!. > However, all Python books seem to contain sample > programs like this one. Do you think that code like > this is desirable? No, but it is standard Python idiom so for the sake of students underdstanding other people's Python code you probably need to teach it - although you might leave it to near the end, after showing the alternatives! OTOH I don't discuss break/continue in my tutor until into the advanced topics... And didn't use them at all in the original vesion (c 1998) or in the book. > 2) Since this is an introductory class, I am tempted > to leave out "optional" topics like argument matching > modes, walk, map, filter, reduce, apply. Do you think > these are required for any Python programmer? The functional programming aspects can be left out, argument matching, if you mean what I think, is quite important in a dymanically typed language. I'd keep that in. If you teach list comprehensions you will make the map/filter/reduce set almost irrelevant anyway. > 3) I need to teach a GUI in this class. I would like > something that is easy, standard, and multi-platform. > I am considering Tkinter and Jython. Any opnions on > this? If they already know Java then teach Jython. Otherwise Teach Tkinter because 1) its the python standard 2) its best documented 3) its pretty easy 4) it has a limited widget set (but explore Tix too) which is easy to extend 5) its based on Tk which is also the standard GUI for Tcl and Perl - and there are versions of it for Lisp and C. HTH, Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to teach Python
>> 2) Since this is an introductory class, I am tempted to leave out >> "optional" topics like argument matching modes, walk, map, filter, >> reduce, apply. Do you think these are required for any Python >> programmer? >> > Since many of there will disappear in Python 3 it might be OK to omit > them. OTOH for those of us who come from languages like APL, these > constructs are dear to our hearts. Hi Elaine, More generally: the "function is as first-class value" is a fundamental concept. I'm not sure when that concept should be introduced, but it definitely belongs in an introductory programming course. If one looks at code like this: if input == 'a': doA() elif input == 'b': doB() ... if we know that functions are avlues, we can reconsider it as: dispatchTable = { 'a' : doA, 'b' : doB, ... } dispatchTable[input]() Other textbooks have covered this ground; you may want to look at them for inspriation. Here's my favorite one now (it's not Python, but it's good stuff.): http://www.htdp.org/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to teach Python
Elaine wrote: > I am going to be teaching "Introduction to Python Programming" in the Fall at > Foothill College in Los Altos Hills, CA. Great. Welcome to the Python Teachers Association. I'm your "neighbor" to the north, above Berkeley. > This is a 5 quarter unit class for students who already know one programming > language. > I presume you mean "at least one". > I have been teaching programming for 20 years now, including Java and C++ as > of late. > I did it for 10 years. Assembler, APL, BASIC, FORTRAN, PL/I,. Pascal, REXX, > As I prepare this class, I have some questions that I I am hoping you can > shed some light on. > > 1) Programming Style > > while 1: > x = next() > if not x: break > > I have never allowed students to use break or continue in any programming > class I have taught. I think of this type of code as belonging with the > dreaded GOTO. > Consider that all "structured" programming statements are some form of GOTO. The question is not which are and which are not but rather the effect on readability and maintainability. The alternative to your code fragment is: x = True while x: x = next() IMHO the former is easier to read, understand, maintain. It gets even worse when the terminating condition occurs in the middle of the loop code. Then we are stuck with: limit = 45 x = limit while x >= limit: x = next() if x < limit: do more stuff with x or while 1: x = next() if x >= 45: break do more stuff with x I'd rather have the 2nd. > I find that students who are having difficulty writing complex conditions in > a loop use multiple if's with break's inside the loop instead. Also, it is > MUCH easier to read a loop if the condition for continuing in the loop is > right up at the top of the loop where it belongs. > In some cases that IS where it belongs. In many others that is debatable. See my example above. A nice example from http://en.wikipedia.org/wiki/Control_flow#Early_exit_from_loops: for n in set_of_numbers: if isprime(n): print "Set contains a prime number" break else: print "Set did not contain any prime numbers" > I have always thought of break and continue as hacks that are not compatible > with structured programming techniques. The only exception is using break in > a switch in C, C++ and Java. > > However, all Python books seem to contain sample programs like this one. Do > you think that code like this is desirable? > Consider also that a feature of Python is the generator, in which a function will suspend execution to return (yield) a result, then resume following the yield statement. Extremely useful, and hard to defend using a limited view of structured programming. > 2) Since this is an introductory class, I am tempted to leave out "optional" > topics like argument matching modes, walk, map, filter, reduce, apply. Do you > think these are required for any Python programmer? > Since many of there will disappear in Python 3 it might be OK to omit them. OTOH for those of us who come from languages like APL, these constructs are dear to our hearts. Also it is worth noting that filter and apply can be rewritten as list comprehensions, so they can be omitted. > 3) I need to teach a GUI in this class. I would like something that is easy, > standard, and multi-platform. I am considering Tkinter and Jython. Any > opnions on this? > Jython is not a GUI. > Thanks for any answers you might have. > Thanks for asking. And good luck with the class. And just for grins see http://www.fortran.com/come_from.html for FORTRAN's COME FROM statement. -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to teach Python
On Sat, 2006-08-12 at 15:09 -0700, Elaine wrote: > I am going to be teaching "Introduction to Python > Programming" in the Fall at Foothill College in Los > Altos Hills, CA. This is a 5 quarter unit class for > students who already know one programming language. > > I have been teaching programming for 20 years now, > including Java and C++ as of late. As I prepare this > class, I have some questions that I I am hoping you > can shed some light on. > > 1) Programming Style > > while 1: > x = next() > if not x: break > > I have never allowed students to use break or continue > in any > programming class I have taught. I think of this type > of code as belonging with the dreaded GOTO. > > I find that students who are having difficulty writing > > complex conditions in a loop use multiple if's with > break's > inside the loop instead. Also, it is MUCH easier to > read a > loop if the condition for continuing in the loop is > right up > at the top of the loop where it belongs. > > I have always thought of break and continue as hacks > that > are not compatible with structured programming > techniques. > The only exception is using break in a switch in C, > C++ and > Java. > > However, all Python books seem to contain sample > programs like this one. Do you think that code like > this is desirable? YES. In C you can code while ((c = getchar()) != EOF) which combines the assignment and the conditional The equivalent Python code is something like: c = getchar() while c != EOF: c = getchar() Because the assignment is a separate statement and not an expression, it cannot be combined with the condition. So the Python consensus seems to be that you are better of with one assignment and embed the condition with a break at the proper spot within the loop. The loop is simply while True: # with newer Pythons c = getchar(1) if c == EOF: break In translating from other languages, note that many while loops can become for loops in Python. The while loop above came from K&R page 17, (The C Programming Language, Kernighan and Ritchie) counting lines in a file. A more natural Python translation would use nl = 0 for line in input: nl += 1 print nl Certainly break and continue can be abused. I would normally expect all break and continue testing to be in one section of the while loop so that it looks like while True: I'm glad to see Python getting taught in schools. When my daughter's college decided to stop teaching C++ as the first programming language (the year she took the course), I urged them to seriously consider Python, but they went with Java. > 2) Since this is an introductory class, I am tempted > to leave out "optional" topics like argument matching > modes, walk, map, filter, reduce, apply. Do you think > these are required for any Python programmer? I think the argument matching is too important to skip. For cases like HTTP POST data, passing a dictionary around is pretty convenient when you can pass it using the ** notation. It allows you to code: def celsius(fahrenheit, **kwds): return (fahrenheit - 32) * 5 / 9.0 postdata['celsius'] = celsius( **postdata) So functions can specify just the dictionary keys they care about. My little example imagines an HTML form with a variety of variables that include temperature conversions. Also zip(*args) is the transpose function. This is too handy to omit, but requires covering the single * mode of matching arguments. > > 3) I need to teach a GUI in this class. I would like > something that is easy, standard, and multi-platform. > I am considering Tkinter and Jython. Any opnions on > this? (not from me.) > > Thanks for any answers you might have. > > -Elaine Haight > > > > __ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Lloyd Kvam Venix Corp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to teach Python
I am going to be teaching "Introduction to Python Programming" in the Fall at Foothill College in Los Altos Hills, CA. This is a 5 quarter unit class for students who already know one programming language. I have been teaching programming for 20 years now, including Java and C++ as of late. As I prepare this class, I have some questions that I I am hoping you can shed some light on. 1) Programming Style while 1: x = next() if not x: break I have never allowed students to use break or continue in any programming class I have taught. I think of this type of code as belonging with the dreaded GOTO. I find that students who are having difficulty writing complex conditions in a loop use multiple if's with break's inside the loop instead. Also, it is MUCH easier to read a loop if the condition for continuing in the loop is right up at the top of the loop where it belongs. I have always thought of break and continue as hacks that are not compatible with structured programming techniques. The only exception is using break in a switch in C, C++ and Java. However, all Python books seem to contain sample programs like this one. Do you think that code like this is desirable? 2) Since this is an introductory class, I am tempted to leave out "optional" topics like argument matching modes, walk, map, filter, reduce, apply. Do you think these are required for any Python programmer? 3) I need to teach a GUI in this class. I would like something that is easy, standard, and multi-platform. I am considering Tkinter and Jython. Any opnions on this? Thanks for any answers you might have. -Elaine Haight __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor