# CS8, UCSB Using the Miller/Ranum text with Python 2.x

### Introduction

The textbook "Python Programming in Context" by Miller and Ranum is based on Python 3.0.

Miller and Ranum suggest in their "Note on Python 3.0" that the book can be used successfully with Python 2.x. in practice, some difficulties and challenges arise. This page describes these challenges, for two purposes:

• to help students and instructors understand why working with Python 3.0 would be preferable
• to help students and instructors through the problems they will encounter with Python 2.x,
if/when that is not feasible.

### Overview:

In general, using Python 2.x for book designed for 3.x is difficult because:

• There are differences in syntax and semantics between Python 2.x and 3.x
• Python 3.x is, by design, not backwards compatible

If the differences were only in esoteric or advanced features, this might not be a problem. However, most of the difficulties actually arise in Chapter 1, with the first three significant encounters with programming.

Use of IDLE ( Section 1.5 intro, p. 9)

To be fair, this is not a Python 2.x vs. 3.x issue, but rather a separate issue with the Python install on CSIL: although Python 2.5.2 is installed on CSIL, it appears that the separate package "python-tools" is not installed.

Therefore, the example of bringing up the IDLE environment on page 9 will not yet work correctly on CSIL.

I've requested that this be installed for the 2.x Python version on CSIL—if/when this is done, this issue will disappear.

Integer vs. Floating Point Division (Section 1.5.1, p. 13)

In Python 3.x:

• 15//2 yields 7, because the // is the integer division operator.
• 15/2 yields 7.5, because / performs normal division.
• The book presents this as "the way Python works" with no differentiation between 2.x and 3.x
• Unfortunately, this is covered during the very first encounter that students have with the Python language in the text, in a section that introduces performing simple arithmetic at the Python prompt.

In Python 2.x:

• 15/2 yields the same answer as 15//2—i.e. integer division is used in both cases.
• If you want floating point division:
• Either make the numerator or denominator (or both) be a floating point.
For example, 15.0/2, 15/2.0 and 15.0/2.0 all yield the answer 7.5 (as in C, C++, and Java)
• An alternative is to use a type conversion, e.g. float(15)/2
• Note to students: a common mistake among students is to try writing: float(15/2) to get 7.5
• In fact, this will divide 15/2 with integer division, yielding 7, which is then converted to 7.0

### From that point forward, things seem ok

As far as I can tell, I don't expect any further problems beyond these initial bumps in Chapter 1. So, if we can get past the problems listed above, it may be relatively smooth sailing with 2.x from that point forward.

There are, however, some serious caveats on this rosy picture:

• Though I've spot checked many of the examples from the book, I haven't yet run every example in the book on the Python 2.5.2 installation on CSIL (at least not yet as of 05/24/2009).
• My analysis is based on looking over the list of differences between 2.x and 3.x, then skimming the program code listings, and the index to see if any of the differences are things that would arise in practice. I didn't see any, but it is possible I missed something.
• One thing to keep in mind is that the feature comparison list for 2.x vs. 3.x is designed to help programmers port code from 2.x to 3.x, not the other way around. So, there may be gotchas that I'm not even aware of yet.

One note about Turtle Graphics (Section 1.5.3, p. 24)

The book relies on Turtle graphics right from the beginning.

In fact all the programming examples in Chapter 1 use the cTurtle module. The authors use Turtle graphics to introduce fundamental ideas about abstraction and problem solving, as well as the syntax of function definitions. So, you really can't "just skip over" this topic.

You can't just run the examples out of the book unless you load the cTurtle module—this applies both Python 3.x and Python 2.x.

But, the version of cTurtle you need to load is different for 2.x vs. 3.x

In Python 2.x:

• The cTurtle module is not included by default, so the code from the book produces an error.
• Workaround: the cTurtle modules is available on the CSIL systems in the directory `/cs/faculty/pconrad/cs8/modules`
• On CSIL, students can be directed to set their PYTHONPATH variable with this command:
`export PYTHONPATH=/cs/faculty/pconrad/cs8/modules`
• With that command in place (which could be added to the bash startup files), the cTurtle module should load just fine, and appears to work ok with the setup on CSIL.

For Python 3.x: