Springer-Verlag London 2014
Kent D. Lee Python Programming Fundamentals Undergraduate Topics in Computer Science
1. Introduction
The intent of this text is to introduce you to computer programming using the Python programming language. Learning to program is a bit like learning to play piano, although quite a bit easier since we wont have to program while keeping time according to a time signature. Programming is a creative process so well be working on developing some creative skills. At the same time, there are certain patterns that can be used over and over again in this creative process. The goal of this text and the course you are taking is to get you familiar with these patterns and show you how they can be used in programs. After working through this text and studying and practicing you will be able to identify which of these patterns are needed to implement a program for a particular task and you will be able to apply these patterns to solve new and interesting problems.
As human beings our intelligent behavior hinges on our ability to match patterns. We are pattern-matchers from the moment we are born. We watch and listen to our parents and siblings to learn how to react to situations. Babies watch us to learn to talk, walk, eat, and even to smile. All these behaviors are learned through pattern matching. Computer Science is no different. Many of the programs we create in Computer Science are based on just a few patterns that we learn early in our education as programmers. Once weve learned the patterns we become effective programmers by learning to apply the patterns to new situations. As babies we are wired to learn quickly with a little practice. As we grow older we can learn to use patterns that are more abstract. That is what Computer Science is all about: the application of abstract patterns to solve new and interesting problems.
PRACTICE is important. There is a huge difference between reading something in this text or understanding what is said during a lecture and being able to do it yourself. At times this may be frustrating, but with practice you will get better at it. As you read the text make sure you take time to do the practice exercises. Practice exercises are clearly labeled with a gray background color. These exercises are your chance to use a concept that you have just learned. Answers to practice exercises are included at the end of each chapter so you can check your answers.
1.1 The Python Programming Language
Python is the programming language this text uses to introduce computer programming. To run a Python program you need an interpreter. The Python interpreter is a program that reads a Python program and then executes the statements found in it, as depicted in Fig.. While studying this text you will write many Python programs. Once your program is written and you are ready to try it you will tell the Python interpreter to execute your Python program so you can see what it does.
Fig. 1.1
The Python Interpreter
For this process to work you must first have Python installed on your computer. Python is free and available for download from the internet. The next section of this chapter will take you through downloading and installing Python. Within the last few years there were some changes to the Python programming language between Python 2 and Python 3. The text will describe differences between the two versions of Python as they come up. In terms of learning to program, the differences between the two versions of Python are pretty minor.
To write Python programs you need an editor to type in the program. It is convenient to have an editor that is designed for writing Python programs. An editor that is specifically designed for writing programs is called an IDE or Integrated Development Environment. An IDE is more than just an editor. It provides highlighting and indentation that can help as you write a program. It also provides a way to run your program straight from the editor. Since you will typically run your program many times as you write it, having a way to run it quickly is handy. This text uses the Wing IDE 101 in many of its examples. This IDE is simple to install and is free for educational use. Wing IDE 101 is available for Mac OS X, Microsoft Windows, and Linux.
When learning to program and even as a seasoned professional, it can be advantageous to run your program using a tool called a debugger. A debugger allows you to run your program, stop it at any point, and inspect the state of the program to help you better understand what is happening as your program executes. The Wing IDE includes an integrated debugger for that purpose. There are certainly other IDEs that might be used and nothing presented in this text precludes you from using something else. Some examples of IDEs for Python development include Netbeans, Eclipse, Eric, and IDLE. Erics debugger is really quite nice and could serve as an alternative to Wing should Wing IDE 101 not be an option for some reason.
1.2 Installing Python and Wing IDE 101
To begin writing Python programs on your own computer, you need to have Python installed. There were some significant changes between Python 2.7 and Python 3 which included a few changes that make programs written for version 3 incompatible with programs written for version 2.7 and vice versa. If you are using this book as part of an introductory course, your instructor may prefer you install one version or the other. Example programs in this text are written using Python 3 syntax but the differences between Python 2 and 3 are few enough that it is possible to use either Python 2 or 3 when writing programs for the exercises in this text. Inset boxes titled Python 2
will highlight the differences when they are first encountered in the text.
Fig. 1.2
Installing Python on Windows
If you are running Windows you will likely have to install Python yourself. You can get the installation package from .
Fig. 1.3
Installing Python on Mac OS X
If you have a Mac, then Python is already installed and may be the version you want to use, depending on how new your Mac is. You can find out which version of Python you have by opening a terminal window. Go to the Applications folder and look in the Utilities sub-folder for the Terminal application. Start a terminal and in the window type python . You should see something like this:
Fig. 1.4
Installing Wing IDE 101 on Windows