Python for Kids: Bonus Project 1

In these posts I outline the contents of each project in my book Python For Kids For Dummies. If you have questions or comments about the project listed in the title post them here. Any improvements will also be listed here.

Bonus Projects!

Did you know that, in addition to the projects in my book, there are an addition 3 bonus projects available online? Hello GUI World, a Spline Drawing Program and Minecraft+Py+Pi. They were originally intended to be included in the book, but then they didn’t fit (which is a shame, since they were the book’s apotheosis). Happily for you though, they’re now online! – and there’s over 100 pages of extra material ! – it’s like you’re getting an extra third of the book! You can even do them even if you don’t have the book! It’s so exciting!! Follow the link in the sidebar to get them, even if you think I’ve overdone the exclamation points!!!

What’s in Bonus Project 1 (Hello GUI World!)

The body of the book lays the foundation for this project and Bonus Project 2. In this project you get to escape the shackles of the command line and create a graphical user environment (GUI) using the Tkinter widget toolkit that is included with Python.

This Project introduces the concept of a widget and introduces you to Label and Button widgets. You learn about callbacks. These are what you need to program in order for something to happen when you click the a button (for example). You learn how to change the configuration of widgets. This allows you, for example, to change the text that is displayed in the widget and the color of a widget.

Since you also need to know how to arrange different widgets in a window, the Project also covers the two main geometry managers (grid and pack). You’re shown how to open and close Tkinter applications and how to use the premade dialog boxes that come with Tkinter (tkMessageBox and tkFileDialog).

Improvements:
If you want to extend yourself, the accompanying cheatsheet contains a list of widgets, with some sample code needed to get the widgets working.
None atm.

Python for Kids: Python 3 Summary of Changes

While my Python 3 posts seemed to stretch for pages and pages with differences, there actually aren’t very many changes at all. Most of that space is taken up by the code outputs (which often had only minor changes) and unchanged code (that had to be there for context). In fact, while the book is about 300 pages long, just a handful of changes are needed to get the whole of the code in the book to run in Python 3. Those changes (in alphabetical order by topic) are below. Check them out if you’re having trouble with your other Python 2.7 code:

Links to the Python 3 updates for each of the Projects:

Project 2, Project 3, Project 4, Project 5, Project 6, Project 7, Project 8, Project 9, Project 10.

class

In Python 3, classes inherit from object automatically, so you don’t need (object) in the first line of the class definition. It’s not an error, but it is superfluous.

# Python 2.7
>>> class AddressEntry(object):
        """
        AddressEntry instances hold and manage details of a person
        """
        pass

# Python 3
>>> class AddressEntry: # note: no (object)
        """
        AddressEntry instances hold and manage details of a person
        """
        pass

Floating point division

Python 2 code in the book will work with Python 3. Some changes to floating point is now automatic in Python 3, so the code to change a number into floating point (eg float(2)) is unnecessary.

import cPickle as pickle

Python 3 uses cPickle by default, so replace import cPickle as pickle by just import pickle. If you try to import cPickle, you’ll get an error.

open

In Python 3 open() has the same syntax as in Python 2.7, but uses a different way to get data out of the file and into your hands. As a practical matter this means that some Python 2.7 code will sometimes cause problems when run in Python 3. If you run into such a problem (open code that works in Python 2.7 but fails in Python 3), the first thing to try is to add the binary modifier – you’ll need it when reading and writing pickle files for instance. So, instead of ‘r’ or ‘w’ for read and write use ‘rb’ or ‘wb’.

#Python2.7
>>> import pickle
>>> FILENAME = "p4k_test.pickle"
>>>  dummy_list = [x*2 for x in range(10)]
>>>  with open(FILENAME,'w') as file_object: #now dump it!
        pickle.dump(dummy_list,file_object)

>>> # open the raw file to look at what was written
>>> with open(FILENAME,'r') as file_object: # change w to r!!!
        print(file_object.read())

#Python3
>>> import pickle
>>> FILENAME = "p4k_test.pickle"
>>>  dummy_list = [x*2 for x in range(10)]
>>>  with open(FILENAME,'wb') as file_object: #### note: 'wb' not 'w'
        pickle.dump(dummy_list,file_object)

>>> # open the raw file to look at what was written
>>> with open(FILENAME,'rb') as file_object:  ##### note 'rb' not 'r'
        print(file_object.read())        

print

Print – mostly the same, since I used Python 3 print syntax in the book. There is an issue with the print continuation character (trailing comma). That needs to be replaced by an end parameter:

#Python 2.7 code:
>>> my_message = "Hello World!"
>>> while True:
...       print(my_message), #<- notice the comma at the end ... #Python 3 >>> my_message = 'Hello World!'
>>> while True:
...       print(my_message, end="")
...

If you’re using Python 2.7 code that’s not in my book, it might look like this:

#Python 2.7 code:
>>> print "Hello World"

To make this work in Python 3, you put brackets around what’s to be printed:

#Python 3
>>> print("Hello World")

raw_input v input

In Python 3 replace raw_input by input wherever you see it. Literally, input is simply a new name for raw_input.

Range vs xrange

The book uses range in anticipation of upgrading to Python 3, so mostly the code will work without changes! If you have code that uses xrange, just rename it to range and all should be well.

In one case the code assumed that the output of range is a list (which is is in Python 2.7, but not in Python 3). The code’s syntax was correct, but led to a logical error. That was corrected by choosing a way to test for the end of the loop that didn’t assume a list was involved.