Preface

This book is intended to teach introductory programming. Material is included for the introductory computer science course, but also for students and readers in science and other disciplines. I firmly believe that programming is an essential skill for all professionals and especially academics in the 21st century and have emphasized that in the content discussed in the book.

The book uses a “just-in-time” approach, meaning that I try to present new information just before or just after the reader needs it. As a result, there are numerous examples, carefully selected to fit into their proper places in the text. Not too soon, and not too late.

I believe in object-oriented programming. My master’s thesis in the late 1970s was on that subject, cut my teeth on Simula, was there when C++ was created, and knew the creator of Java. I do not believe that object-oriented programming is the only solution, though, and realized early that good objects can only be devised by someone who can already program. I am therefore not an “objects first” instructor, but a “whatever works best” instructor.

Many of the examples involve computer games and game development. As we know, the majority of undergraduate students play games. They understand them better than, say, accounting or inventory systems, which have been the typical early assignments. I believe in presenting students assignments that are interesting.

I don’t think that catering to any particular language form in an introductory text serves the student or the language. The student, if sensible, will learn other languages. Bringing Python idioms into play too soon may interfere with the generality of the ideas being presented and will not assist the student when learning Java, C++, or Ruby.

This book introduces a multimedia code module Glib that can assist the programmer with graphics, animation, sound, interaction, and video. Glib is included on the companion disc or can be downloaded from the book’s web site. The basic library, static Glib, needs nothing but a standard 3.4 or better installation of Python. It uses tkinter as a basis, which is distributed with the language. The expanded library uses pygame, and that is easily downloaded and installed. The extended Glib, called dynamic Glib, allows exactly the same interface as does static Glib, but extends it to also include sound, interface, and video. Thus, if static Glib compiles and runs a program, then dynamic Glib should too.

There is a wiki concerning the book at https://sites.google.com/site/pythonparker/ and I am happy to receive comments, code fixes, extensions, extra teaching material, and general suggestions. I see a good textbook as a community, and encourage everyone – especially first year students, the target audience of this book - to send me their experiences and ideas.

Software (any computer program) is ubiquitous. Cars, phones, refrigerators, television, (and almost everything in our society) are computerized. Decisions made about how a program is to be built tend to survive, and even after many modifications, they can affect how people use that device or system. Creating efficient software helps in achieving a productive and happy civilization.

Python is a great language for beginning programmers. It is easy to write the first programs, because the conceptual overhead is small. That is, there’s no need to understand what “void” or “public” means at the outset. Python does many things for a programmer. Do you want something sorted? It’s a part of the language. Lists and hash tables (dictionaries) are a part of the language. You can write classes, but do not have to, so it can be taught objects first or not. The required indentation means that it is much harder to place code incorrectly in loops or if statements. There are hundreds of reasons why Python is a great idea.

And it is free. This book was written using version 3.4, and with the PyCharm API. The modules used that require download are few, but include PyGame and tweepy. All free.

Overview of Chapters

Here’s a brief outline of the book. It can be used to teach computer science majors or science students who wish to have a competency in programming.

Chapter 0: Historical and technological material on computers. Binary numbers, the fetch-excute cycle. This chapter can be skipped in some syllabi.

Chapter 1: Problem solving with a computer; breaking a problem down so it can be solved. The Python system. Some simple programs involving games that introduce variables, expressions, print, types, and the if statement.

Chapter 2: Repetition in programming: while and for statements. Random numbers. Counting loops, nested loops. Drawing a histogram. Exceptions (try-except).

Chapter 3: Strings and string operations. Tuples, their definition and use. Lists and list comprehension. Editing, slices. The bytes type. And set types. Example: the game of craps.

Chapter 4: Functions: modular programming. Defining a function, calling a function. Parameters, including default parameters, and scope. Return values. Recursion. The Game of Sticks. Variable parameter lists, assigning a function to a variable. Find the maximum of a mathematical function. Modules. Game of Nim.

Chapter 5: Files. What is a file and how are files represented. Properties of files. File exceptions. Input, output, append, open, close. Comma separated value (CSV) files. Game of Jeopardy. The with statement.

Chapter 6: Classes and object orientation. What is an object and what is a class? Types and classes. Python class structure. Creating instances, __init__ and self. Encapsulation. Examples: deck of playing cards; a bouncing ball; Cat-a-pult. Designing with classes. Subclasses and inheritance. Video game objects. Duck typing.

Chapter 7: Graphics. The Glib module. Drawing window; color representation, pixels. Drawing lines, curves, and polygons. Filling. Drawing text. Example: Histogram, Pie chart. Images and image display, getting and setting pixels. Thresholding. Generative art.

Chapter 8: Data and information. Python dictionaries. Latin to English translator. Arrays, formatted text, formatted input/output. Meteorite landing data. Non-text files and the struct module. High score file example. Random access. Image and sound file types.

Chapter 9: Digital media: dynamic Glib module. Using the mouse and the keyboard. Animation. Space shuttle control console example. Transparent colors. Sound: playing sound files, volume, pause. Video: play and position a video, accessing frames and pixels in a video.

Chapter 10: Basic algorithms in computer science. Sorting (selection, merge) and searching (linear, binary). Timing code execution. Generating random numbers; cryptography; data compression (including Huffman codes and RLE); hashing.

Chapter 11: Programming for Science. Roots of equations; differentiation and integration. Optimization (minimum and maximum) and curve fitting (regression). Evolutionary algorithms. Longest common subsequence, or edit distance.

Chapter 12: Writing good code. A walk through two major projects: a word processor written as procedural code and a breakout game written as object oriented code. A collection of effective rules for writing good code.

Chapter 13: Dealing with real world interfaces, which tend to defined for you. Examples are Email (send and receive), FTP, inter-process communication (client-server), Twitter, calling other languages like C++.

Chapter 14: A reference for both versions of Glib.

Chapter Coverage for Different Majors

A computer science introduction could use most chapters, depending on the background of the students, but Chapters 0, 7, 9, and / or 11 could be omitted.

An introduction to programming for science could omit chapters 0, 10, 12.

Chapter 13 is always optional, but is interesting as it explains how social
media software works under the interface.

Basic introduction to programming for non-science should include
Chapters 0, 1, 2, 3, 4, 5, and 7.

Companion Files (Disc included in physical book or files available for downloading)

The companion files contain useful material for each chapter:

•  Selected exercises are solved, including working code when that is a part of the solution.

•  All significant programming examples are provided as Python code files(over 100), that can be compiled and executed, and that can be modified as exercises or class projects. This includes sample data files when appropriate.

•  An important aspect of this book is the use of a graphics library named Glib. Source code for this module is provided on the disc and online. There are two versions: one that works with the built-in module tkinter which allows graphics, and a second that extends the previous module using
pyGame and allows videos, interaction, and sound.

•  All figures are available as images, in full color.

Instructor Ancillaries

•  Solutions to almost all of the programming exercises given in the text.

•  MS PowerPoint lectures provided for an entire semester (35 files) including some new examples and short videos.

•  Likely the most important aspect of this book, aside from the very practical viewpoint, is the provision of the Glib graphics and multimedia library. This comes in two versions: a universal version that handles basic graphics and that can execute without any extra installation step; and the full multimedia extension that handles sound, video, and interaction, but that requires that pyGame be installed, which is a simple process.

•  All of the Python code that appears in the books has been executed, and all complete programs are provided as .py files. Some of the numerous programming examples (over 100) that are explored in the book and for which working code is included:

o  An interactive breakout game

o  A text formatting system

o  Plotting histograms and pie charts

o  Reading Twitter feeds

o  Play Jeopardy Using a CSV Data Set

o  Sending and receiving Email

o  A simple Latin to English translator

o  Rock-Paper-Scissors

•  Hundreds of answered multiple choice quiz and examination questions in MS Word files that can be edited and used in various ways.

Dedicated Web Site

An online community has been started at https://sites.google.com/site/pythonparker/ for comments, new exam questions and exercises, extra code, and as a place to report problems.

Please consider contributing material to the on-line community, and do have fun. If you don’t, then you’re doing it wrong. 

 

J. Parker

October 2016