1. Python For Computer Science

This page is designed to explore the language Python for use in the computer science curriculum. The goal is to briefly provide pointers and comments for those that would be interested in using Python in CS courses. This is not a Python-advocacy page, but rather an honest look at the pros and cons of switching to Python, and related issues.

We invite comments and questions from others who are also interested or have information on this issue.

The original proposal sent to the faculty can be found at PythonProposal

This material is available under a Creative Commons License (see the link at the bottom of this page).

1.1. Textbooks

This section lists possible textbooks for use in various courses. See also:

1.1.1. CS1 Introduction to Computing

1.1.2. CS2 Algorithms and Data Structures

1.2. Motivational Paradigms

This section explores the motivational paradigms one could use in a Python course. See below for examples.

Text processing - there is a book "Text Processing in Python" by David Mertz, Addison Wesley, 2003.

Image processing/Machine Vision - Python has PIL, Python Image Library

Multimedia - possibly using Guzdial's book

Web CGI Programming - Python has nice tools for this

Database Programming - Python has interfaces to most databases

1.3. Python Courses, Papers, and Articles

2. FAQ

The following questions assume a basic use of Python. You start python by typing 'python' at a command prompt; >>> is Python's top-level prompt; ... is Python's contination prompt; the hash is Python's comment starting symbol. Please feel free to add to thess questions, or answer them.

Before you ask a question, you may wish to get a quick look at Python at PyroModulePythonIntro.

1. Is Python at least as good as C++ at allowing the use of simple techniques from functional, imperative, data-abstraction, and full object-oriented coding styles?

In Python, many data structures are built into the language. For example:

>>> a = [] # a is a list
>>> b = {} # b is a map/hash

Python also makes many objects act as an iterator:

>>> for i in "abc":   # string as iterator
...       print i
>>> file = open("somefile.txt", "r")
>>> for line in file:   # file as iterator
...       print line

Some functional programming examples:

>>> import operator
>>> x = [1, 2, 3, 4, 5]
>>> sum = reduce(operator.add, x)

>>> delta_x = map(lambda v: v+ 1, x)
>>> delta_x
[2, 3, 4, 5, 6]

>>> parts = [.1, .2, .3, .4, .5, .6, .7]
>>> [round(z) for z in parts if z > .2]
>>> [0, 0, 1, 1, 1]

Python has functions, objects, multiple-inheritence, sets, continuations of a sort (through the yield command and iterators) and now some support for 'decorators' (syntax that automatically wrap functions by passing them to a function that returns functions).

2. Is Python used by at least some computational scientists for some sort of use in scientific computation?

Yes. There is an active research and development community using Python in numerically and computationally extensive applications. See, for example:

We use this in our neural network code in Python (all of the matrix computations are done using Numerical Python, so it is quite fast.)

3. How suitable is Python generally for CS1/2 (i.e., how good for expressing and executing algorithms and data structures at the introductory level, preferably with a minimum amount of notational and intellectual overhead)?

Some people describe Python as executable pseudocode and I think that that captures the essence of the language. See some of the reports above for examples of moving from pseudocode to Python (they are very similar).

4. How hard is it going to be for us (not the students) to do a little bit of graphics, e.g. draw circles on the screen based on mouse input?

>>> import Tkinter
>>> window = Tkinter.Tk()
>>> canvas = Tkinter.Canvas(window)
>>> canvas.pack()
>>> canvas.create_oval(10, 10, 20, 20)

Mouse input is done through events. So, even students can incorportate graphics into their introductory lessons, if you wanted to go that route.

5. What's the system for doing explicit type checking? We already have the students write preconditions for functions, so even a way of just asking "what is the type of value in this variable?" and then comparing it to some other type, in an assertion, would be fine. But my impression is that there's something more organized than this.

Python has assert(expression). You can get a variable's type with the type() function, and insure a variable's class is correct in the class hierarchy, like:

def function(var1, var2, var3):
    assert isinstance(var1, SomeBaseClass)
    assert isinstance(var2, (int, long, float, complex)) 
    assert var3 in range(5,11)

But you could also build a run-time type checking system like:

def studentDefineFunction(x, y, z):
    tempValue = x + y * z
    return tempValue

where 'checker' is something the instructor could write to run-time check the input and return types (and even values). Python is also evolving. There is some talk of adding optional syntax for type checking. There are active mailing lists for Python development and education. The Python developers think alot about how the language looks to the beginner, and expert.

6. What is debugger/IDE support like? Can I set breakpoints on code and on variable values, and single-step through subsequent code and watch variables change as I do so?

There is an interactive editor and development environment (called idle to continue the Monty Python allusions) that allow breakpoint, single step, environment exploration, etc. Idle comes with Python, and is written in Python. Because Python allows reflection and uses Python data structures to implement itself, it is also easy to add your own debug-like functions. See for example:

7. What is MPI or other cluster support like? Could we use Python to explore parallelism in the high-performance scientific computing course?

This is quite nice in Python. Pat Miller (who wrote the MPI wrapper) will be speaking at Bryn Mawr this Fall (and is the brother of one of the Chemists here). See, for example, this basic demonstration:

8. What impact will this have on the following courses?

In upper-level courses, we wouldn't be able to assume that the students know Java, C++, or C. We are planning on teaching memory management and compiling issues in our Programming Paradigms course.

9. What about the AP exams?

If students have experience in Java, having Python in the introductory course is a reason that they may still want to take it, rather than testing out of it.

Python has much less overhead than Java. It's a useful instructional language because you need only the structure that you are trying to teach at that particular moment; in Java, it's necessary to wrap ANY useful code in a class in order to compile. This wastes time on the first day of CS 101, and is viewed by new programmers as confusing magic.

If students learn Python first, classes can be introduced after basic concepts (functions, variable assignment) have been developed. Then when students transition to Java et al., classes will be old friends, and instantiating them will be old hat.

10. Is Python used in the real world? Are there real applications written in Python?

Python is gaining corporate usage. Google is one recent little company that uses Python extensively. One of the founders of Google wote a textbook on artificial intelligence (AI: A Modern Approach) and has begun moving all of the examples from Java to Python. This wiki is written in Python. The Linux hardware detector (kudzu) is written in Python.

11. What implications does switching to Python in CS1 and CS2 have for the rest of the curriculum?

12. Where in our curriculum will students learn Java, C, C++, etc.?

13. How will we change (or will we) our Programming Languages course? Will it now use Python as a case study? Most texts these days use C, C++, or Java. If students do not know these languages will it impact the teaching of Programming Languages?

14. A number of students take CS110 because they know that we teach it using Java and they want to learn Java. IOW, students have heard of Java and that attracts them to CS110. This is also true of C++. How will the use of Python affect this? (It is a similar situation to using Scheme...)

15. Graphics is probably not as important to be included in CS110. We do this in Java because Graphics is well interated into the Java Library. What will be the main schtick for engagement in Python?

16. Are we going to use "standard" Python or also include other python enabled packages? What about supporting, upgrading, etc.

17. What programming environment (IDE) will/can be used for beginning students?

18. What drawbacks will result from using a dynamically, not strongly-typed language in CS1?

19. Given that we already use Python in our other upper-level courses, don't we want to expose students to some other language earlier? Or do we intend to change from Python in those courses?

20. How will an "introduction to computer science" change (in content) by using Python? If it is not different from using any other language they why use Python?? Or, can we change the "content" of an introdcution to computer science in some way that makes switching to Python more compelling.

21. Python is an evolving language. How do we plan to keep track of these changes in the language and update the content of our CS1 and CS2 to keep up?

22. Is Python a good first language? Or is Python a better second language?

3. Student responses

This is selection of some student responses.

3.1. Survey #1

Student comments from:

Blank, D.S., Kumar, D., Meeden, L., and Yanco, H. (2005) Pyro: A Python-based Versatile Programming Environment for Teaching Robotics. To appear in the ACM Journal on Educational Resources in Computing (JERIC).

On-line: - pages 20-21

3.2. Student comments, via newsgroup

3.3. Student comments, via survey