Reviewers wanted

If you’ve got a relevant blog and are interested in reviewing my book Python for Kids for Dummies, the publishers have some review copies that can be sent out. Please use the contact form on the About page to drop me a line and say you’re interested in reviewing the book. I’ll get your details and have a copy sent out.

Python for Kids Book: Project 10

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.

What’s in Project 10 (Math Trainer)

Project 10 is the last of the projects in the book. Project 10 is a math trainer – it tests you on your times tables and prints them out to allow you to revise.  This project is largely an application of some of the earlier work in the book but involves a little added complexity and some thought in solving some user interface issues.  For example, I use a formatting template to present the times tables. It’s easy to print out a long list of the times tables, but not very readable, so I show you how to use a nested loop to present 5 tables at a time across the screen. This uses the slice operator [:] as well as string’s join method

In order to ask random questions I introduce random.choice.  The trainer allows you to compete against yourself on score and on time. In order to time each training session I introduce the time method from the time module – time.time().

There are three additional projects that, as at 8 September 2015, I have completed and am now waiting to be loaded online.
Update: The additional projects are on line! Follow the link in the sidebar to download a copy.

Improvements:

@306: The time module gives you the number of seconds since the Epoch. The Epoch started on 1 January 1970 (for Unix operating systems). Why is it 1 January 1970? No reason. It’s just one of those historical accidents that infest everything to do with time calculations.

Python for Kids Book: Project 9

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.

What’s in Project 9 (Address Book)

While Project 6 introduced the concept of objects and showed how even a lowly string is actually an object, Project 9 is about how you can make your own objects using the class keyword.  Project 9 uses classes to implement a simple address book application.  I discuss the difference between a class and an instance of the class and the importance of having a reference variable (self) so that an instance can refer to itself. You learn about constructor functions and how to override a method (__repr__) to make print statements work properly on your custom method.

Since having an address book is not much use if you can’t save and update it, I also introduce the pickle module. It is used to store general Python objects (if they’re hashable!).  The address book is given save and load functions as well as a rudimentary user interface.

Improvements (1st printing):

On page 278 in the add_entry method, the second through fourth occurrences of the code

if first_name == "q":

Should be


if family_name == "q":

if email_address == "q":

and

if date_of_birth == "q":

respectively. This should be corrected in the second printing. The code samples have the correct code.

Python for Kids Book: Project 8

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.

What’s in Project 8 (Silly Sentences)

Project 8 is a short project which teaches you about (old style) formatting strings. Formatting strings allow you to create a sentence template into which you can substitute words.  Four words are chosen from four lists (at random) and substituted into a sentence. The random matchings make nonsense sentences. The substitutions can be streamlined by using a data type called the tuple, so that data type is introduced and discussed. You learn how to unpack tuples and how to use a tuple to receive multiple values from a function.  The project also relies on the random.choice method.

Python for Kids Book: Project 7

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.

What’s in Project 7

Project 7 (Cryptopy) introduces dictionaries as a means of encoding text using a Caesar cipher. Along the way, you are introduced to the string module and the characters in string.printable. There is some discussion about escape sequences and examples of \n and \t are given. Since you don’t want to encrypt escape sequences the slicing operator is introduced in order to take string.printable and slice off the control characters. This becomes the character set that will be encoded.

An encryption dictionary is created and each of the characters in a test message are encrypted then joined using the join method of the empty string.  I explain why this is better than adding one character after another to an existing string.  I create a matching decryption function and decryption dictionary and test the round trip (plaintext-> ciphertext -> plaintext).

The project introduces file operations by reading a message from a file then writing the encrypted (or decrypted) message to another file.  This is first done with the base file operations open and close, then the with keyword is introduced to make the housekeeping a little easier.

I demonstrate how to use your newly written encryption functions from the command line by importing the code from your own file – your own third party module!  In order for this to work seamlessly you are introduced to the __name__ attribute and the if __name__ == “__main__”: construction.

Improvements:

On page 203, the first line of code:

>>> file_object.close()

is not necessary  (because you already closed the file in the code on the previous page. It shouldn’t give you an error message though).  Ignore  it.

On page 209, there is a reference to code being in C:\Python27. This reference is only relevant if you are using Windows. On Linux and Mac things are more complicated – if the file is in the same directory that the shell is running from then you should be ok.

Python for Kids Book: Project 6

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.

What’s in Project 6

Project 6 introduces the concept of objects and lists to create a program that converts text into hacker speak (ie text with numbers substituted for letters). The project revisits the creation of a simple my_message variable then shows, using the dir builtin, that the variable has a variety of characteristics (that is, attributes) other than its value. It shows that one of the attributes, upper, is like a function and calls the functions of an object methods. It shows how to call a method or access an attribute through the dot notation.

In order to implement the hacker speak project, the program must perform a number of substitutions. To do that, it uses a list. The project discusses how to make a list,  how to add elements to a list, how to iterate through a list and how to test whether something is in a list.  It highlights a ‘gotcha’ with list methods – some of them modify the list in place without returning a value.

The code includes a logical error in the manner in which substitutions are made. A print statement is used to debug and identify the location of the error before it is fixed.

The project also gives a short introduction to IDLE’s debugger. There is a problem with IDLE’s debugger on Macs (there is no right click to set a breakpoint. Command-click works for some people, but not all), so if you’re running a Mac and command-click doesn’t work for you, you might have to skip this bit.

Improvements:
Page 150 does not use the print syntax from Python 3 like I said I would. It should be:

>>> for i in dir(my_message):
          print(i)
__add__
__class__
__contains__
[...]

Page 164 does not use the print syntax from Python 3 like I said I would. Should be:

>>> substitutions = [['a','4'], ['e','3'], ['l','1'], ['o','0'],
                     ['t','7']]
>>> for s in substitutions:
          print(s)

['a',  '4']
['e',  '3']
['l',  '1']
['o',  '0']
['t',  '7']