Thursday, January 26, 2012

Beginner's Resources

I've turned up some nice beginner's resources on Python:

Dive into Python is an online book teaching Python step by step, the way I like to approach things in my classes.

Python Tutorials for Kids 8+ also looks like it'll be very useful.

Invent You Own Computer Games with Python is a course using games as the draw. I've ordered a hardcopy of the book, and the book is available online for free. This makes it easy to afford using it in my classes (and students can't claim to have forgotten their textbook when it's online!) I'll be trying it out with my middle school classes this semester.

There are many other python resources out there. Add your favorites in the comments, please!

Tuesday, January 24, 2012

Python 2, Python 3

Python 3 kinda gets different right away, for the beginner. It's not huge, just different.

The simple, free, and easy print 'Hello there!' becomes print ('Hello There!'). Doesn't look like too big a difference. Unless you're a beginner, where every single bit of unfamiliar punctuation becomes a step into darkness.

I'm planning on using Python in the classes I teach. I've already used it as one of the "broadening" languages I use. These are languages that aren't central to the class, but I give the students a bit of time with them as a way of letting them have a look at other languages, so that the language we are using doesn't become the only one they've ever seen.

So far, the core of my teaching has been around Java. We don't start right off in Java. We do some command line stuff, we get through some of the introductory concepts there and in BASIC. Then we do a lot with HTML and CSS, which is a great lead-in for more formal programming.

Python, Ruby, C, LISP and LOGO all have a place in my classroom. LOGO becomes one of the languages I use a lot with the middle school classes (6th-8th.) All my classes at least get a look at all these languages, to the Hello, World! level and maybe a bit more if time allows.

I'm strongly considering using Python as the main language for either middle school or high school or both next year. This immediately raises the version 2 or version 3 question.

One of my constraints, my time, immediately suggests an answer to this question. Python 2 is already installed on all the systems in the school lab. If I want 3, I need to go through and spend my own time installing it and checking it out on all the systems. Whereas 2 is there (2.7 right now, as I recall.) All I have to do is have the students open a command line and type 'python'.

All my systems at home are also on Python 2, except for my Windows 7 machine. It had no Python on it, I just installed the latest of the Windows easy installer versions on it last night (3.2-something, I believe.) I played with it for a bit, I'd already seen some information on the differences between 2 and 3 and was able to work my way through without any problems on the basis of those fuzzy memories. I'm not exactly a Python power user, so I only have to cover limited ground.

The same goes for my classes. The concepts we learn about controlling the computer and organizing tasks for it are more important that what language we use, or which version of a language we use. So I don't see going with 2 as a problem. I expect to have a Python 3 familiarity exercise, at least, as part of the class once we're well along and comfortable with Python in general. Just as I have familiarity exercises with different languages including those mentioned above and others that change from year to year.

Online interpreters/compilers help us do that without me having to install new software on the school computers. But those aren't adequate for our regular class work, just for "try it and see" type exercises.

So Python 2 it is. For now.

I'm planning to start doing some curriculum development with my middle schoolers in this Spring semester. They're enthusiastic about being guinea pigs for me. That'll make it easier for me to decide before year end how far to go with Python in my classes, and to have a start on the new materials for next year before summer, the time when I do most of my curriculum development.

Spring semester this year will still have me using Java with Greenfoot for my high school students. They're enthusiastic about that, too. It's been very successful for me for the past several years. It takes time and work to change directions in a class like this, the relative merits of the different languages are only one factor in the choice of language. Personally, I like both languages a lot. Each has advantages that I wish both shared.

Ruby is in the mix, too. Either for an expanded role in the class (such as replacing the time we used to spend on Windows command line), or as the main language in one or the other level of class. I try not to confuse things by introducing too many different languages, so I've got to be careful that I structure things so that one thing builds on another, rather than each new thing being something that adds confusion.

Sunday, January 22, 2012

IDLE Windows and Saves

Let's have a look at 'idle', the GUI-fied command line for python...

Idle, the Python GUI

OK, looks pretty standard. It's got "Save" in the menu. Let's give it a try...

Let's see what got saved:
mag@rincewind:~/programming/python$ cat python-shell.txt
Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 2.6.4
>>> print 'I\'m a lumberjack and I\'m OK!"
SyntaxError: EOL while scanning string literal
>>> print 'I\'m a lumberjack and I\'m OK!'
I'm a lumberjack and I'm OK!
>>>


Yuck. Everything got saved. How do we save a program? Lessee...

Aha, a "New Window" item on the menu. Maybe that'll give me an uncluttered window. But what about the prompt? Ah, let's just click it and find out.

Ah, a clear window appears. Let's type something in and save it as 'larch.py'.

A simple python program entered in a New Window in IDLE.


Let's have a look at the file:
mag@rincewind:~/programming/python$ cat larch.py
print "I'm a lumberjack and I'm OK!"


That's more like it. Let's try running it from the command line...
mag@rincewind:~/programming/python$ python larch.py
I'm a lumberjack and I'm OK!


OK, so idle works as an editor. Now, how about running it in idle? Hmmm...'Open' in the interactive window?

Nope. That just opens the other window.

I can copy and paste, and that works, but it's not very clean. Gotta be a better way.

D'oh! There's a menu item right there that says "Run". Let's see..."Python Shell"?

Nope, that just takes me back to the other window. How about "Run Module F5"?
>>> ================================ RESTART ================================
>>>
I'm a lumberjack and I'm OK!
>>>


Aha! That's the ticket.

Time for a soda.

Saturday, January 21, 2012

Playing with Print

print prints things. Well, duh. Let's see how....

>>>print 'hello'
hello


OK, single quotes work.

>>>print "Hello"
Hello


And double quotes. What about printing quotes? I bet I can nest one type of quote inside the others...

>>>print "Python's Print Puts Pennies in Paragraphs"
Python's Print Puts Pennies in Paragraphs


>>>print 'The Python Quoth "Bring out your dead!"'
The Python Quoth "Bring out your dead!"


What about printing both sorts together?

Hmmm...

Maybe I could concatenate...

>>>print 'The Python Quoth "' + "You're no fun anymore!" + '"'
The Python Quoth "You're no fun anymore!"


That worked. But Python's tricky. Maybe there's an easier way. Multiple quotes?

>>> print ""The Python Quoth "I'm a lumberjack and I'm OK!"""
File "", line 1
print ""The Python Quoth "I'm a lumberjack and I'm OK!"""
^
SyntaxError: invalid syntax


>>> print ''The Python Quoth "I'm a lumberjack and I'm OK!"''
File "", line 1
print ''The Python Quoth "I'm a lumberjack and I'm OK!"''
^
SyntaxError: invalid syntax


>>> print '''The Python Quoth "I'm a lumberjack and I'm OK!"''''
File "", line 1
print '''The Python Quoth "I'm a lumberjack and I'm OK!"''''
^
SyntaxError: EOL while scanning string literal


>>> print """'The Python Quoth "I'm a lumberjack and I'm OK!""""
File "", line 1
print """'The Python Quoth "I'm a lumberjack and I'm OK!""""
^
SyntaxError: EOL while scanning string literal


>>> print """'The Python Quoth "I'm a lumberjack and I'm OK!"""
'The Python Quoth "I'm a lumberjack and I'm OK!


Dang, lost the final double quotes. Lessee...

>>> print """'The Python Quoth "I'm a lumberjack and I'm OK!\""""
'The Python Quoth "I'm a lumberjack and I'm OK!"


Aha! Escaping quotes works!

>>>print 'I\'m a lumberjack and I\'m OK!'
I'm a lumberjack and I'm OK!


Let's try mixing some numbers in.

>>> print 'There are ' + 3 + 'Pythons upon the desk.'
Traceback (most recent call last):
File "", line 1, in
TypeError: cannot concatenate 'str' and 'int' objects


>>> print 'There are ', 3, ' Pythons upon the desk.'
There are 3 Pythons upon the desk.


Commas work, but too many spaces are in there.

>>> print 'There are',3,'Pythons upon the',3,'rd desk.'
There are 3 Pythons upon the 3 rd desk.


OK, the first number is spaced right, how do we get the other one without spaces?

>>> print 'There are',3,'Pythons upon the '+ str(3) +'rd desk.'
There are 3 Pythons upon the 3rd desk.


OK, so we can concatenate with '+', but only if it's a string.

We can use numerals if we put it together with a comma, but then we get an added space.

We can make a numeral into a string with str() and then we can concatenate with + so that we don't get added spaces.

I think I get it.

I'm sure there are a dozen other ways to do this, and at least ten of them may be better. But that'll do for now.