Python for Kids: Python 3 – Project 2


[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),
...

Comment

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]

Comment

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]

Comment

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]

Comment

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

Comment

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),
...

Comment

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.

Note:
* 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.

8 Responses to Python for Kids: Python 3 – Project 2

  1. Pingback: Python for Kids Book: Project 2 | Python Tutorials for Kids 13+

  2. Terry Reedy says:

    Thinking of and calling range or range() a generator is a mistake that creates problems you don’t really have. In Python 3, range is a class of immutable, compactly represented, arithmetic sequences. Which is to say, ‘multiples of n up to some limit’. its instances are iterable sequences just like lists and even more like tuples. Like them, ranges have length and can be indexed and tested for containment. The main thing you cannot do with ranges that you can with tuples and lists is add and multiply. On the other hand, ranges can represent sequences that would not fit in memory if expanded.

    >>> r7 = range(0, 1000000000000, 7) # multiples of 7 under one trillion
    >>> len(r) # how many
    142857142858
    >>> r[140000000000]
    980000000000
    >>> 987987987000 in r
    True

    • brendanscott says:

      Hi Terry
      Thanks for reading the blog and for your comment. I did agonize over what word to use there after reviewing a number of sites, including the Python 3.5 docs. I’m glad you’ve commented on it, since it confirms my unease. I settled on the word “generator” because I thought the term “immutable sequence type” was not viable given the audience. I like your examples. They are a bit mind expanding.

      • abarnert says:

        But “generator” is completely wrong. In particular, it’s not true that, as you say, ranges generate the values one at a time in order. (Sure, range_iterator, the thing you get from calling iter on a range object, does that–but then so does list_iterator, the thing you get from calling iter on a list object, and you wouldn’t call list a generator, would you?)

        It’s meaningless to your students now, and actively misleading when they eventually learn what generators are. And, because it’s wrong, your attempt to explain it can only make it more misleading, not less.

        It’s true that ranges are “lazy” or “virtual”, but there’s no reason you need to explain that to your students at all. Beyond maybe the one sentence about how they don’t blow away your memory so make them as big as you want, there’s no difference in behavior that your students will see. So, why say anything? You can write “for i in range(10, 20):” or “r = range(10, 20); print(r[3])”, or “for x in reversed(r): print(x)” and they’ll all do exactly the same thing in Python 3 as in Python 2.

        (Eventually, more advanced students will introspect the type/repr/pickle-state/internals of r, or ask how it magically avoids blowing away their memory, or just want to build something similar themselves. But at that point, they’re ready to learn about dunder methods and the data model and the Sequence ABC and the iteration protocol and all that stuff, so you don’t need a simplified explanation.)

  3. catafest says:

    You can replace the > with one image with text TAB.
    This allow to understending why the python using indentation for blocks and nesting.
    Also using tab image the the source code is more readable.
    Thank you. Regards.

  4. Pingback: Python for Kids: Python 3 – Project 3 | Python Tutorials for Kids 13+

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: