Coding example apps in Python 2.7 versus Python 3.5

January 23, 2016

Computer Science, Technology

Several readers and students have asked about running the example programs from “Teach Your Kids to Code” in IDLE for Python 2.7 (the common default on Mac OS X and Linux computers) – the good news is that there are two relatively easy options:

  1. Upgrade to Python v3 with the terminal commands:
    sudo apt-get install python3

    and

    sudo apt-get install idle3

    (let me know if you’re using a version of Linux that doesn’t work with apt-get, and I’m glad to post a video if it’ll be of help).
    All the examples in the book and the online course for “Teach Your Kids to Code” will work as-written for the newer Python/IDLE 3.
    – OR –

  2. Change just the input() and print() statements in any code examples to raw_input(), and print without the parentheses (the round brackets) for backward compatibility with Python v2.
    Example: Here’s what the YourName.py program from Chapter 1, p. 6 of the book, or Lecture 3 of the Udemy course would look like for Python 2.7:
# YourName.py - for Python 2.7 from p.6 or Lecture 3
# Teach Your Kids to Code
name = raw_input('What is your name?\n')
print 'Hi,', name
Python 2.7 code sample

Just a couple of small changes to your apps can help you code in Python 2.7

The other statements (and almost all turtle examples) should work the same from Python 3 back to Python 2, but the input and print statements changed between versions.
I hope this post this is of help, or message me/reply below if you’d like me to post a video – Python 2 is still used widely in the scientific and other communities (and still ships on new Linux/Mac computers). Enjoy either version of Python, and let me know if I can be of help anytime!

Happy coding!

BrysonSignatureTransparent

About Bryson Payne

Author of Teach Your Kids to Code, TEDx Speaker, and Professor of Computer Science at the University of North Georgia.

View all posts by Bryson Payne

No comments yet.

Leave a Reply