1. Python Programming 101
This Computer Science text further develops the skills you learned in your first CS text or course and adds to your bag of tricks by teaching you how to use efficient algorithms for dealing with large amounts of data. Without the proper understanding of efficiency, it is possible to bring even the fastest computers to a grinding halt when working with large data sets. This has happened before, and soon you will understand just how easy it can occur. But first, well review some patterns for programming and look at the Python programming language to makesure you understand the basic structure and syntax of the language.
To begin writing programs using Python you need to install Python on your computer. The examples in this text use Python 3. Python 2 is not compatible with Python 3 so youll want to be sure you have Python 3 or later installed on your computer. When writing programs in any language a good Integrated Development Environment (IDE) is a valuable tool so youll want to install an IDE, too. Examples within this text will use Wing IDE 101 as pictured in Fig. has directions for installing both Python 3 and Wing IDE 101. Wing IDE 101 is the freeversion of Wing for educational use.
There are some general concepts about Python that you should know when reading the text. Python is an interpreted language. That means that you dont have to go through any extra steps after writing Python code before you can run it. You can simply press the debug button in the Wing IDE (it looks like an insect) and it will ask you to save your program if you havent already done so at least once. Then it will run your program. Python is also dynamically typed. This means that you will not get any type errors before you run your program as you would with some programming languages. It is especially important for you to understand the types of data you are using in your program. More on this in just a bit. Finally, your Python programs are interpreted by the Python interpreter. The shell is another name for the Python interpreter and Wing IDE 101 gives you access to a shell within the IDE itself. You can type Python statements and expressions into the window pane that says Python Shell to quicklytry out a snippet of code before you put it in a program.
Like most programming languages, there are a couple kinds of errors you can get in your Python programs. Syntax errors are found before your program runs. These are things like missing a colon or forgetting to indent something. An IDE like Wing IDE 101 will highlight these syntax errors so you can correct them. Run-time errors are found when your program runs. Run-time errors come from things like variables with unexpected values and operations on these values. To find a run-time error you can look at the Stack Data tab as it appears in Fig.. When a run-time error occurs the program will stop executing and the Stack Data tab will let you examine the run-time stack where you can see the program variables.
In the event that you still dont understand a problem, the Wing IDE 101 (and most other IDEs) lets you step through your code so you can watch as an error is reproduced. The three icons in the upper right corner of Fig. let you Step Into a function, Step Over code, and Step Out Of a function, respectively. Stepping over or into your code can be valuable when trying to understanda run-time error and how it occurred.
One other less than obvious tool is provided by the Wing IDE. By clicking on the line number on the left side of the IDE it is possible to set a breakpoint. A breakpoint causes the program to stop execution just before the breakpoint. From there it is possible to begin steppingover your code to determine how an error occurred.
To motivate learning or reviewing Python in this chapter, the text will develop a simple drawing application using turtle graphics and a Graphical User Interface (GUI) framework called Tkinter. Along the way, youll discover some patterns for programming including the accumulator pattern and the loop and a half pattern for reading records from a file. Youll also see functions in Python and begin to learn how to implement your own datatypes by designing and writing a classdefinition.
1.1 Chapter Goals
By the end of this chapter, you should be able to answer these questions.
What two parts are needed for the accumulator pattern?
When do you need to use the loop and a half pattern for reading from a file?
What is the purpose of a class definition?
What is an object and how do we create one?
What is a mutator method?
What is an accessor method?
What is a widget and how does one use widgets in GUI programming?
1.2 Creating Objects
Python is an object-oriented language. All data items in Python are objects. In Python, data items that could be thought of as similar are named by a type or class. The term type and class in Python are synonymous: they are two names for the same thing. So when you read about types in Pythonyou can think of classes or vice versa.
There are several built-in types of data in Python including int , float , str , list , and dict which is short for dictionary. These types of data and their associated operations are included in the appendices at the end of the text so you have a quick reference if you need to refer to it while programming. You can also get help for any type by typing help(typename) in the Python shell, where typename is a type or class in Python. A very good language reference can be found at http://python.org/doc , the official Python documentationwebsite.
1.2.1 Literal Values
There are two ways to create objects in Python. In a few cases, you can use a literal value to create an object. Literal values are used when we want to set some variable to a specific value within our program. For example, theliteral 6 denotes any object with the integer value of 6.
This creates an int object containing the value 6. It also points the reference called x at this object as pictured in Fig.. All assignments in Python point references at objects. Any time you see an assignment statement, you should remember that the thing on the left side of the equals sign is a reference and the thing on the right side is either another reference or a newly created object. In this case, writing x
6 makesa new object and then points x at this object.
Fig. 1.2
A Reference and Object