Python for Kids: Python 3 – Project 2
March 19, 2016 8 Comments
[Oct 2016: edits to correct escaped characters inserted by WordPress’s misbehavior]
Some people want to use my book Python for Kids for Dummies to learn Python 3. Choosing Python 2.7 over Python 3 was a difficult decision and I have given reasons why in the book.* Nevertheless, if I write a new edition of the book, it definitely will be in Python 3, so I plan to work through the code in the existing book, highlighting changes from Python 2 to Python 3 and providing code that will work in Python 3.
I am working from the downloadable code samples (they are cross referenced to page numbers in the book), so it might be an idea to get a copy, although working from the hard copy should also be fine. Get a copy from the link in the right hand sidebar.
For Project 2, most of the code works exactly the same in Python 2.7 and Python 3. There are some changes though later in the Project (from page 50). Those changes are set out below (page numbers from the 2015 printing).
Code on Pages 36 to Page 50
All of the code on these pages works in Python 3 and gives the same output.
Code on Page 50
#Python 2.7 code: >>> my_message = 'Hello World!' >>> while True: ... print(my_message), ...
In Python 2.7 you use the comma -> , to tell print to NOT include a new line at the end of what is printed.
In Python 3 print has become a function. Functions are not discussed till Project 5! Implementing this in Python 3 needs a lot of extra concepts, that I’m not going to explain here. Instead, I’m just going to give you working code. You will need to come back to this after you’ve done Project 5. Hopefully then it will make more sense.
#Python 3 >>> while True: ... print(my_message, end=" ") ...
Code on Page 51
#Python 2.7 code: >>> range(3) [0, 1, 2]
In Python 2.7 range() creates a list. In Python 3 it makes something like a generator** – and generators are not even covered in the book!😦 The main difference is that, with a list, all the items that you need are created ahead of time. However, with a generator, you only create the next item when you need it. Practically, the code will work in the same manner and you won’t be able to notice any difference (see the examples on the next page). For example, the numbers produced by range in Python 3 will start at 0 and run up to the number one less than the number you give to range. The good news though is that you can ignore my warning (on page 52) about using range() if you’re running Python 3. In Python 3 range doesn’t blow out your memory, so feel free to use it for numbers as big as you like.
#Python 3 code: >>> range(3) range(0, 3)
Code on Page 52
#Python 2.7 code: >>> range(3,10) [3, 4, 5, 6, 7, 8, 9] >>> range(3,10,2) [3, 5, 7, 9]
See comments on generators above. You won’t be able to see the practical difference until you cover for loops (see below).
#Python 3 code: >>> range(3,10) range(3, 10) >>> range(3,10,2) range(3, 10, 2)
Code on Page 53
#Python 2.7 code: >>> range(13,10,-1) [13, 12, 11]
Same comments on generators. See below.
#Python 3 code: >>> range(13,10,-1) range(13, 10, -1)
#Python 2.7 code: >>> for i in range(3): ... print(i) ... 0 1 2
#Python 3 code: >>> for i in range(3): ... print(i) ... 0 1 2
In this example both the code and the output from the Python 2.7 and Python 3 code was the same. However, the code created the output in different ways (the Python 2.7 code created a list, while the Python 3 code created a generator). Anywhere you use range in the book, you can use it when using Python 3.
Also, using this for loop structure you can test to see whether the earlier ranges are practically the same in Python 2.7 and Python 3.
For example (from page 52):
#Python 2.7 code: >>> range(3,10) [3, 4, 5, 6, 7, 8, 9] >>> range(3,10,2) [3, 5, 7, 9] #Python 3 code: >>> for i in range(3,10): ... print(i) ... 3 4 5 6 7 8 9 # note same numbers as in [3, 4, 5, 6, 7, 8, 9] >>> for i in range(3,10,2): ... print(i) ... 3 5 7 9 # note: odd numbers from 3 to 10
Code on Page 54
#Python 2.7 code: >>> my_message = "Hello World!" >>> for i in range(300): ... print(my_message), ...
See comments on print above.
#Python 3 code: >>> my_message = "Hello World!" >>> for i in range(300): ... print(my_message, end=" ") ...
Note that, in Python 2.7 when the print run had finished the >>> prompt started on a new line. However, in Python 3 the prompt >>> starts immediately after the last Hello World! on the same line.
* I had dreams of extending the book for parents to include, for example, loading applications to the cloud. Right now (March 2016, a year after I finished writing the book) Google App Engine, perhaps the easiest way to get an app live on the internet, still does not support Python 3 (although it is available through a separate service – GAE Managed VM hosting).
** It creates an “immutable sequence type” according to the docs. See Terry’s comment.