82
Python Programming by Example Page 1 of 82 Richard Riehle Richard Riehle April 5, 2013 For Python Version 3.0 and later

Python by Example - 9 June 2014

Embed Size (px)

DESCRIPTION

phython textbook

Citation preview

Page 1: Python by Example - 9 June 2014

Python Programming by Example Page 1 of 82 Richard Riehle

Richard Riehle

April 5, 2013

For Python Version 3.0 and later

Page 2: Python by Example - 9 June 2014

Python Programming by Example Page 2 of 82 Richard Riehle

Table of Contents

About this Book ....................................................................................................................................... 4Computer Basics ...................................................................................................................................... 4Computer Programming............................................................................................................................ 6Data Types ............................................................................................................................................... 9Programming Languages ........................................................................................................................ 11About Python ......................................................................................................................................... 12Getting and Using Python....................................................................................................................... 13Python 3.0 and higher ............................................................................................................................. 14IDLE...................................................................................................................................................... 15Simple Python Statements ...................................................................................................................... 16Console Input and Output ....................................................................................................................... 18Python Lists ........................................................................................................................................... 20Print Items From A List .......................................................................................................................... 21More List Operations.............................................................................................................................. 23Store List in an External File................................................................................................................... 25Nested Lists – also called Matrices.......................................................................................................... 25Python Dictionary Type.......................................................................................................................... 31Python Strings ........................................................................................................................................ 35A simple string example ......................................................................................................................... 35String Methods....................................................................................................................................... 36Iteration Operations................................................................................................................................ 37Alternate version of Summation Example .............................................................................................. 38For Loop Examples in Python Functions................................................................................................. 39While Loops........................................................................................................................................... 40Working With Sets ................................................................................................................................. 41More on Python Functions...................................................................................................................... 41Function with a While Loop.................................................................................................................... 43File Variation of Interest Function .......................................................................................................... 44Numbers in other Number bases ............................................................................................................. 46Functions as First-class Objects. ............................................................................................................. 47Passing a function as a parameter............................................................................................................ 48Make the function example more explicit................................................................................................ 48Assertions for pre-, and post-conditions .................................................................................................. 50lambda functions .................................................................................................................................... 51Recursion ............................................................................................................................................... 52Python Files ........................................................................................................................................... 54Python Modules...................................................................................................................................... 56A Bubble Sort......................................................................................................................................... 57Abstract Data Type (ADT)...................................................................................................................... 58Object-Oriented Programming................................................................................................................ 59A Stack Class Example........................................................................................................................... 60A Bounded Stack Class........................................................................................................................... 61An Account Class Example .................................................................................................................... 62Windows Applications (tkinter) ............................................................................................................. 63More sample Programs........................................................................................................................... 68Documentation in Python........................................................................................................................ 72Software Engineering with Python.......................................................................................................... 73Appendix A -- Fundamental Algorithm Structures................................................................................. 80

Page 3: Python by Example - 9 June 2014

Python Programming by Example Page 3 of 82 Richard Riehle

.

Note:

Programs in this book have been written and executed in Python version 3.2 or later.

Page 4: Python by Example - 9 June 2014

Python Programming by Example Page 4 of 82 Richard Riehle

About this Book

This book is intended to help the newcomer to computer programming learn how to create programs in the Python programming language. It will present simple examples of Python that you can create using your own computer. In some examples, a line of source-code may be commented. Comments in Python are line-oriented, and begin with the symbol, #. We will often use two of these (e.g., ##) to make clear that there is a comment. You will get the most benefit from this book by entering the source code examples exactly as shown, and then experimenting with them to see what will happen when you modify the working programs to try something different. This is the “what if I do this” approach to learning how to program.

Before looking at the Python programming language, we first review a few fundamental ideas about computer programming. These ideas should help you understand Python, but will also give you some insight into other programming languages that you may encounter in the future.

Computer BasicsA computer operates because it has a combination of hardware and software. The Hardware is the physical circuitry of the computer and includes many millions of logic gates. The software is the computer program(s). A program is required for the hardware to do its work. Without a computer program, the computer is simply a lot of inert circuits that do nothing. If you remove the software from the hardware, the computer will not operate.

Hardware

The physical computer consists of the following fundamental components:

Central Processing Unit (sometimes more than one); CPU Input and Output capabilities A primary memory (accessible directly by the CPU)

In addition, a computer system may include any of the following components:

Display devices (e.g., Flat-screen, LED’s, Cathode Ray Tube, etc.) Pointing devices (e.g., mouse, light-pen, finger-sensitive touch screen) Secondary Memory (e.g., disk drive, USB storage, SD card, tape drive) Printer Any other kind of data capture device (e.g., scanner, bar-code reader, etc.) Communication ports (for such things as Internet communication) Many other things still be invented in the future.

Software

The pre-loaded software for a computer will usually include:

Operating System (sometimes not included or necessary)

Page 5: Python by Example - 9 June 2014

Python Programming by Example Page 5 of 82 Richard Riehle

Application programs Utility Programs

In addition to the pre-loaded software, the computer needs some application software. Applications are the programs that do the work. This software can include accounting software, games, general utilities, spreadsheets, word processors, and a wide range of industry specific software. We sometimes speak of software in terms of vertical applications versus horizontal applications.

Horizontal applications are those that are not specific to a given industry or line of work. These would include word processors, spreadsheets, etc. Vertical applications are industry specific. Examples are inventory system for an automobile parts wholesaler, sales order system for a manufacturer, and premium billing system for a life insurance company. Horizontal software is rarely industry-specific. Vertical software is frequently focused on the needs of a small segment of some industry with a lot of common needs.

We also have development software. Python and other programming languages fall into this category. Other development languages are C, C++, Java, Ada, COBOL, Scala, Ruby, Fortran, Scheme, Erlang, OCAML, and many more. With these languages we create the source -code solutions (program) both vertical and horizontal applications. This is called computer programming. The person who does this kind of work is called a computer programmer.

There are other jobs related to computer programming. These jobs include software architects, software engineers, software testers, and many more. The fact is that there are many kinds of jobs involved in creating and maintaining software besides that of computer programmer.

Many people begin their career as computer programmers and move beyond programming to some of the other jobs listed in the previous paragraph. It seems to be a widely held view in the world of computer software that everyone, regardless of job classification, should have learned the skills associated with computer programming.

Page 6: Python by Example - 9 June 2014

Python Programming by Example Page 6 of 82 Richard Riehle

Computer Programming

Computer programming is the process of creating a set of instructions for the computer so it can do useful work. The program instructions are usually written in a human-readable language, such as Python, (or one of hundreds of other languages) that programmers call source-code. The source-code is then translated (using specialized translation software) into the native language of the computer. Once the translation is complete and linked into the operational environment of the computer, we say the computer program is executable.

There are two requirements for a computer program: the data on which it will operate and the behavior (collection of instructions) for performing operations on that data. The behavior associated with a computer program is often called an algorithm. A famous computer scientist named Niklaus Wirth summarized this idea with the statement that a computer program is Data plus Algorithms, summarized as:

Program = Data + Algorithms

There are three fundamental behaviors associated with any computer programming.

1. Sequence A series of instructions with no decision logic2. Iteration A loop with some kind of logic to terminate the loop3. Selection Controlled by a TRUE or FALSE condition

Sequence means that the program is simply a set of instructions, one after another, step-by-step. Iteration is means that the program goes into what programmers often call a “loop.” A loop continues until some condition (unless the loop is infinite) is satisfied. Selection is based on the idea that the program can test conditions of relationships between one or more items of data (True/False) and execute a different set of instructions based testing that condition.

There are other parts of a program you will learn in this book. For example, a program, even when designed using the fundamental structures just described, will be broken into smaller groups such as functions, procedures, modules, and classes. These small groupings make it easier to see a set of program behaviors without having to understand the entire program. A simple function is shown below.

Of course, real and practical functions will be a bit more robust than the one just shown. We can create functions to do very complicated mathematical formulae, compute interest on a bank account, or regulate the cruise-control on an automobile. The point is that a program, in any

A fourth kind of construct is called recursion. We cover that separately in this book.

def compute(x, y):return x + y

Page 7: Python by Example - 9 June 2014

Python Programming by Example Page 7 of 82 Richard Riehle

language, is developed by creating many small functions and putting them together in a larger set of functions, modules, classes, or procedures.

Page 8: Python by Example - 9 June 2014

Python Programming by Example Page 8 of 82 Richard Riehle

Three Fundamental Behaviors

Sequence Iteration Selection

The above three behaviors are often regarded as the fundamental structures. You can think of them as smallest kind of algorithms, as well as the building blocks for more complicated algorithms. However, programmers understand there are many variations of each of these structures. For example, an iteration might test for the condition before entering the loop, or wait until reaching the end of each complete iteration. Selection might be an alteration (if … else … end if) or what some languages call a case construct which we sometimes call a muti-way selection. In Appendix A of this book, you will see more examples of these diagrams, including some variations on them, as they might be used in the construction of algorithms.

The example shown below is an illustration of sequence. The instructions simply proceed from one to another without any decision making.

This example is a bit more complicated. We have a while loop which iterates until some condition (selection) is reached. It is not unusual to combine fundamental behaviors in an algorithm as we have in this example.

>>> def sequenceExample(x, y, z):temp = xx = zz = yy = tempprint(x, y, z)

def IterationExample():alist = []while True:

avalue = input("Enter an Integer: ")if int(avalue) >= 0: ## convert the input to an integer

alist.append(avalue)else:

return alist

Page 9: Python by Example - 9 June 2014

Python Programming by Example Page 9 of 82 Richard Riehle

Data Types

As noted in our reference to Niklaus Wirth, programs contain algorithms and data. Just as there are many kinds of algorithms, there are also many kinds of data. The data are classified according to a scheme we call data-types. Data types are sometimes unique to a given programming language, but they typically have following fundamental properties (sometimes more for some languages):

1. The name of the type (some types might be anonymous)2. The set of values (or allowed states) for an instance (item of data) of the type3. The set of operations/behaviors for instances of the type4. A set of rules; often language-specific rules for instances of the type

The concept of data-type is important in programming. In languages such as Python, Perl, and Ruby, the type of an item of data is called dynamic typing. In static-typed languages, such as those listed below, the type name is how we classify instances of data of a given type. For example, integer (often abbreviated as int) is the name of the type, not the instance. Therefore,

x : integer; ## Ada, Pascal, Modula, other languagesq, r, s : float; ## Ada, Pascal, Modula, other languages

orint x; ## C, C++, Java, C# (note type name comes first)float q, r, s; ## C, C++, Java, C#

In the above examples, the values x, q, r, s are all instances of the integer of float data-type.. Think of the data-type as a cookie-cutter from which you can stamp multiple cookies, where int (or integer) is the cookie-cutter, and x, q, r, and s are the cookies.

The primitive types also have a representation in the memory of the computer. For most static typed languages, the size of the word (number of bits) will be a determining factor for how the data of a given type is represented. Using a 32 bit word as an example,

Byte 8 bitsCharacter Either 8 or 16 bits depending on implementationUnsigned Integer 32 bitsSigned Integer One bit for sign; 31 bits for mantissa (value)Floating-Point Bit zero for sign of mantissa; bits 1 – 8 for Exponent to determine

positive or negative placement of decimal point. 9 – 31 for mantissa.Double Floating-point Similar to above, but with 64 bitsBoolean Varies by language

Page 10: Python by Example - 9 June 2014

Python Programming by Example Page 10 of 82 Richard Riehle

The floating-point representation is a bit complicated. For IEE-754 (the current standard), the exponent is designed so it can represent the position of a decimal point. So, the following mantissa, 32458365 can have a decimal point located according the exponent. Examples are:

3.24583650.3245836532458.365324.5836532458365.0

Notice how the decimal point can float depending on the value of the exponent for the mantissa, even as the mantissa remains the same. This is sometimes called scientific notation.

As a Python programmer, using a dynamically type language, you are rarely concerned with these under-the-hood details. However, it is sometimes useful to know them. Moreover, some of you aspiring future programmers will be wondering about this so now you know a little more about it that you did a few minutes ago.

Python does not require the programmer to explicitly associate data with data types before they are used or declared. However it does have a type model for built-in types. However unlike many other languages, Python infers the data-type from the initial assignment of an instance of that type, as well as from certain uses of grouping symbols (e.g., parentheses, curly braces, quotationmarks, and square braces). Built-in types include integers, floating-point, strings, lists, dictionaries, sets, and others.

In addition to the built-in data-types for Python, the programmer can invent entirely new types. The new types have the same four properties (listed above) as a built-in type. When a programmer creates a brand-new data type, one not already in the language, it is called an “abstract data type” (ADT). There are two kinds of ADT, extensible and non-extensible ADT. In Python an ADT of either kind is designed using the word “class” instead of “type.” The extensible ADT is used for Python’s version of Object-oriented Programming (OOP).

We will have more to say about ADT’s and OOP later in this book. For now, as a new programmer (or new to Python), you will be using Python built-in types.

Page 11: Python by Example - 9 June 2014

Python Programming by Example Page 11 of 82 Richard Riehle

Programming Languages

As noted earlier, programmers write/compose their programs in a human-readable programming language. The completed composition is sometimes called source-code. The programmer’ssource-code is readable by other programmers who are trained in that programming language. The computer programmer has available a large choice of programming languages. Some of these languages are close to the native language of the machine, the computer called Assembler. We call Assemblers low-order languages. Assembler programs are very difficult for most humans to read unless they are trained in that language. Also, Assembler programs are usually not portable from one computer to another.

A popular language such as C is a low-order, but human readable language sometimes called a “universal Assembler. High-order languages abstract away nearly everything about the low-level details of the computer. Python is a high-order language. There is a continuum between the lowest-level and the highest-level so that some languages have both high-order features and low-level features. A highly skilled Python programmer can create programs that interface with low-level languages to produce programs that are more efficient, but not as easily readable.

With a high-order language such as Python, a person with little knowledge of the underlying features of a computer can learn to write powerful computer programs. Low-order programming languages require more knowledge of how computers do their work. As noted earlier, the most common lowest-order language is called Assembler because the programmer writes source code in a human-readable symbolic code that is close to the native language of the computer. The symbolic source code is then “assembled” into that native language to produce an executable (runnable) computer program.

Some programming languages are interactive from the keyboard. Python provides interactive capabilities. Other languages require the programmer to submit the completed source code to a separate translator called a compiler. Programs that can be translated on-the-fly (interactive) are sometimes called “interpreted” source-code programs. Others, require a compiler. Both styles of programming have their advantages. Python is an interactive language that can be interpreted as each line of code is entered. That is, the Python programmer can see the effect of every statement as it is entered, or wait for the entire program to execute as a whole. This makes it easy to see mistakes in real-time. However, Python programs can also be compiled. This means the Python programmer can benefit from the best of interactive programming as well as from the ability to create larger more comprehensive compiled programs.

Page 12: Python by Example - 9 June 2014

Python Programming by Example Page 12 of 82 Richard Riehle

About Python

A person using Python can write programs without much knowledge of how a computer works. Python is a high-order programming language that can be used for solving a large variety of programming problems. Under most circumstances, Python is “interpreted.” However, there are translators (compilers) available for Python. You, as a new Python programmer, will learn the benefits and usefulness of both approaches to programming in Python.

If you are new to computer programming and/or new to Python, we recommend you begin your study using a Python built-in development environment. The environment integrated into Python is called IDLE. IDLE is an Integrated development environment for Python. Using IDLE will make your early experience with Python more productive. Later, when you are more comfortable with programming, you may choose to use any of a large number of other development tools and editors for creating source-code. Some skillful programmers prefer to write their Python programs as close to the machine as possible. We do not recommend that for most programmers.

We suggest that you enter the programs in this book as they appear. Then, experiment with them by making little changes to see, “What if …” . Every experiment should help you understand Python a little more.

If you are really a scholar/scientist, you will keep a notebook of what did work and what did not. That is, keep a journal of your experiments so you can learn from them, avoid past mistakes, and continue to improve your understanding.

Page 13: Python by Example - 9 June 2014

Python Programming by Example Page 13 of 82 Richard Riehle

Getting and Using Python

One of the most important features of Python is its portability. It rarely matters what operating system or computer platform you want to use to write your programs when you are using Python. There are a small number of environments that do not accept Python. Most notable among the more popular devices is the Apple Ipod/iPad series which only accept programs written in a compiled language called Objective-C. All versions of Windows and Unix support Python. As of this writing, there is work being done for Python support the pad computers running the Android operating system, but Android support is not quite ready.

On the Internet, enter http://www.python.org. From there, download the most recent version of Python appropriate to your choice of operating systems. For the lessons in this book, be sure to use a Python version higher than 3.0. The earlier versions have significant differences from the examples in this book. Then, when Python is installed on your computer, use IDLE, the built-in development environment for most of the exercises in this book. Later, after you are experienced with IDLE, you may choose to do your programming with a different editor, or even from the command line prompt. For now, IDLE makes Python easy to get started.

Every Python installation is integrated with IDLE, so using it for your with this book will make your learning experience a bit faster. You can configure IDLE to satisfy your particular choice of font, color, etc. We recommend you start by using the default options.

IDLE will allow you to open a development window in which you can create your programs. You can save this window each time you write a program in it. Also, you can have multiple functions defined in that window. We have several exercises where we will do exactly that..

The prompt in the IDLE command window has three angle brackets shown as >>>. You will enter your Python commands/instructions at that prompt. Later, when you are writing larger programs using IDLE’s programming window, that >>> prompt will not be part of the window in which you write your Python programs.

A final note on entering source code into Python. Unlike many other languages, Python uses indentation as the delimiter for its statements. This is especially important for loops, conditional statements, and the design of Python functions. IDLE will do the indentation for you automatically. Therefore, rely on IDLE as a novice instead of trying to to the indentation yourself. With more experience, you can configure your own indentation scheme.

Page 14: Python by Example - 9 June 2014

Python Programming by Example Page 14 of 82 Richard Riehle

Python 3.0 and higher

This book is focused on Python 3.0 or later. As of this edition of this workbook, we are using Python 3.2. A higher numbered version will also work, but not a version prior to 3.0. Download a 3.2 or higher version for the programming exercises in this book.

You will find examples of Python programs all over the Internet. Many of those examples are written for versions of Python prior to 3.0. If your code does not work when you try to run examples from the Internet, the chances are that it is from a version of Python earlier than 3.0.. Look for the statements that are no longer valid (statements that are producing error messages) and change them to conform to Python 3.0.

The student may modify (update the syntax) and use programming examples from earlier versions of Python even though they may not work, as coded, with Python 3.0 and later. The most typical of these is the Python print statement. Fix this by changing a statement such as:

print x to print (x) ## parentheses required in Python 3.0

The command line prompt for Python is a set of three angle brackets that looks like >>>At that command prompt (using IDLE or the command line on your computer), you can enter any Python statement; that statement will execute on pressing the ENTER key on your keyboard.

There are many other important differences between 3.0 and earlier versions. For Python 3.0 you do not need “raw-input” and its associated overhead. Many libraries, such as Tkinter, are now lowercase (e.g., tkinter). If you see a library module in an example from, say, Python 2.6 and try to import it without success, it might be that the module is now lowercase. For example,

>>>import Tkinter ## no longer works; must be tkinter>>>import tkMessageBox ## is now tkinter.tkmessagebox

The correct syntax for these import statements is now:

>>>import tkinter ## OK for 3.0 and higher>>>import tkinter.tkmessagebox ## OK for 3.0 and higher

Prior to Python 3.0, input was entered using the command raw_input. That is now replaced with the simple input command. This newer command requires the programmer to do a “type conversion” (sometimes called type-casting) for numeric input. For example,

>>> x = input("Enter a number: ") # Get a number from the terminal with promptEnter a number: 42 # The number is an integer (int)>>> x # Check to see if that is what you entered '42' # Yes, but it is recorded as a string >>> y = 2 # Create another integer>>> y + int(x) # Arithmetic operation requires type conversion of x44 # Correct answer is an integer due to type conversion

Page 15: Python by Example - 9 June 2014

Python Programming by Example Page 15 of 82 Richard Riehle

IDLE (An Editor available with every Python installation)

Python’s built-in editor is called IDLE. And IDE (interactive development environment) is commonly provided with most modern programming languages. However, many programmers choose alternative editors. If you are a beginner, we recommend you use IDLE until you have more experience. An IDLE screen will look like this:

The prompt is designated by the three angle brackets >>>. This is where you enter your commands when in interactive mode. IDLE also allows you to open a window for composing a programs.

This window is designed so you can save your programs, re-edit them later, and execute them directly from the window. You can also, after saving one of these files, execute it directly without having to open the Python IDLE dialogue window.

There is an OPTIONS menu in Python for configuring your Python environment. We recommend you do not use option this until you are more experienced with interactive Python programming.

Page 16: Python by Example - 9 June 2014

Python Programming by Example Page 16 of 82 Richard Riehle

Simple Python Statements

The following statements illustrate Python’s interactive capabilities we mentioned earlier. We remind you, again, to use IDLE for this set of statements even though you could use these at the command-line prompt on some of your Python-supported computers.

The Python command prompt is three “>”angle-brackets: >>> At each command prompt, you can enter any valid Python instructions or statements. The Python environment wil give you and instant reply. The reply will either be the result of evaluating your input or, if you get it wrong, an error messge. In IDLE, your error message will be in red. Python comments can be on the same line as your statement and are denoted with a #. In this book, we will use ## for most comments, and sometimes print the comment in red.

>>>print (“Hello Happy Student”) ## print a string; be sure to include parenthesesHello Happy Student ## result of keyboard entry>>> 42 ## display a number42 ## output of previous line>>> 42 + 5 ## add two numbers47 ## result of addition>>> 42 * 2 ## multiply two numbers84 ## result of multiplication>>> 42 / 3 ## divide one number by another14.0 ## floating-point result from />>> 42 // 3 ## divide one number by another14 ## truncated to integer with //>>> 42 ^ 12 ## xor operation38 ## result of XOR operation>>> 42 or 12 ## logical or operation42 ## result of logical OR operation>>> 42 and 12 ## logical and operation12 ## result of logical AND operation>>> x = [56, 67, 12, 96, 34] ## a list of integers>>>

Order of Evaluation

For the normal Algebra expression, 6/2(2+1) you might get the answer 9 or 1, depending on how you parse that expression. The famous PEMDAS rule suggests first adding 2 +1, then dividing 6 by 2 and then multiplying by 3. Or it might be interpreted in some other way by the human doing the problem. In Python, that expression will be evaluated as follows:

>>>x = 6 / 2 * (2 +1)>>> x9.0

This might not be what you wanted. In all of computer programming, not just Python, it is essential that you be clear. Therefore, you might want to code the following, instead:

>>>x = (6 / (2 * (2 +1))

PEMDASParentheses Exponentiation Multiplication Division Addition Subtraction

Page 17: Python by Example - 9 June 2014

Python Programming by Example Page 17 of 82 Richard Riehle

This kind of defensive programming is always recommended when you have an expression of mixed operations. In some languages (e.g., Ada) the language rules demand that you always code mixed expressions with parentheses to avoid order-of-operation surprises.

Exercises

1. Open the Python IDLE programming environment. Try some of the examples shown above using your own numbers. Experiment with computations using parentheses to see whether you can control the order of operations. That is, if you put part of your computation in parentheses, does that change the order of evaluation of your expression.

2. For the last example, “a list of integers” try the print statement on the list named, x. Remember that, as of Python 3.0, the print statement requires parentheses. Prior to Python 3.0, the parentheses are not required.

Page 18: Python by Example - 9 June 2014

Python Programming by Example Page 18 of 82 Richard Riehle

Console Input and Output

Python’s console input (e.g., keyboard) and output (e.g., video display terminal) capabilities will work equally well with any kind of computer and any operating system that supports Python.

Input

For Python 3.0 and later, there are some changes to how input is acquired from the keyboard. In earlier version of Python there was a function for “raw-input”. This is no longer appropriate. Instead, you will use the built-in function named, input along with a type conversion (type cast for you C/C++ programmers).

Example 1:

>>> data = input("Enter: ") ## a variable named data.Enter: hello ## any string of characters>>> data ## key in the data name'hello' ## outputs the content of the named data

Example 2:

>>> data = int(input("Enter: ")) ## convert input to an integerEnter: 45 ## enter a valid integer value>>> data ## key in the data name45 ##The data is an actual integer

Example 3:

>>> data = int(input("Enter: ")) ## convert input to an integerEnter: “hello” ## entered a non-integer, invalid valueTraceback (most recent call last):

File "<pyshell#34>", line 1, in <module>integer = int(input("Enter: "))

ValueError: invalid literal for int() with base 10: '"hello"'

In Example 3, we tried to convert a non-numeric string to an integer and received an error message. This error will occur with any attempt to convert invalid input using a type conversion syntax. While Python is dynamically-typed, it is not weakly-typed.

Example 4:

>>> fdata = float(input("Enter value: ")) ## input using float conversionEnter value: 32.5 ## input data with decimal point>>> fdata ## check to see it it is correct32.5 ## display the value entered>>> fdata = float(input("Enter value: ")) ## input function, againEnter value: 67 ## input without a decimal point>>> fdata ## check to see if it is correct67.0 ## a decimal point was added

This is a typical error message when you make a mistake. We will look at some of these in more detail later.

Page 19: Python by Example - 9 June 2014

Python Programming by Example Page 19 of 82 Richard Riehle

Example 5:

Sometimes it is better to put the input action in a try-block. We cover exception handling in more detail later, but this example demonstrates an elementary form of this capability.

>>> def get_integer(): ## own input functiondata = input("Enter Integer: ") ## get a string of datatry: ## start of try block

idata = int(data) ## try to convert the input dataexcept: ## start of exception handler

return ("Input not integer! ") ## input data was wrongelse: ## but if it was OK then,

return idata ## return the converted data

Example 5 in use:

>>> get_integer() ## call the functionEnter Integer: 34 ## function displays the prompt34 ## user enters an integer value>>> get_integer() ## call the functionEnter Integer: dhdadfa ## user enters a non-integer value'Input was not an integer! ' ## function returns an error message

Console Output

The simplest form of output is as text to the video display [screen] on your computer.

Examples 1:

print ("Hello Python Programmer") ## a simple string print statement

print ("We can embed ", 42, " in the output.") ## 42 ; a great hockey star!

Example 2:

>>> print (" line 1 \n", "line 2 \n", "line 3 \n", "all done") ## \n means line feed

line 1 line 2 ## four lines of output from the print statementline 3 all done

Later in this book we will show you how to get started displaying messages with Python in a more interesting format using the Microsoft Windows environment.

Backslash n ( \n ) causes a carriage return and line feed

Page 20: Python by Example - 9 June 2014

Python Programming by Example Page 20 of 82 Richard Riehle

Python Lists

A Python list is a collection type (sometimes called a composite type). A collection type is one that contains more than one value, sometimes all of the same type, sometimes of differing types. You can perform many powerful operations on Python composite types. A Python list is mutable, and can contain data of more than one datatype (even other instances of lists and other collection types). Being mutable means you can change the values in the list, remove items, or add items. The list is a fundamental data type in computer software; Python hides the implementation details, but we will consider those later. Consider these examples.

>>> alist = ['howdy', 67, 'qwerty', '*indalated', 666, 'SANDMAN']>>> alist ## enter list name and press the return/enter key['howdy', 67, 'qwerty', '*indalated', 666, 'SANDMAN']>>>>>> numberList = [65, 23, 78, 48323, 32767, 12, 94, 43]

Note the use of square brackets [ ]for declaring a list. Using the wrong kind of bracket for a collection type is a common Python error. Later you will see structures (collection data types) that look like lists, but are delimited by {}, ( ), and quotation marks (single or double). Python has special (and often unique) rules for each collection data type.

More list examples:

>>> anEmptyList = [ ] ## We can populate the empty list later>>> nameList = [‘john', 'preethi', 'qiaoyun', 'miyako', 'abdul']>>> numberList = [325, 872, 921, 173, 368, 735] ## list of numbers

Note that a list can contain a mixture of data types. It is also possible to nest lists within lists. For example.

>>> listOflist = [List1, List2] ## Two lists (from above example) nested in outer list

Page 21: Python by Example - 9 June 2014

Python Programming by Example Page 21 of 82 Richard Riehle

Simple List Operations

Here is a simple list named “breakfast” followed by some operations on the list. Notice that we can refer to all the list items, some of them, the last few items, the first few items, or any combination of the items. List processing is a powerful capability of Python (as well as many other languages.

>>> breakfast = ["ham", "eggs", "oatmeal", "sausage", "orange juice"]>>> breakfast ## Display the entire list['ham', 'eggs', 'oatmeal', 'sausage', 'orange juice']>>> breakfast[2] # get the third item in the list; first item is breakfast[0]'oatmeal'>>> breakfast[-1] # get the last item in the list'orange juice'>>> breakfast[0] # get the first item in the list'ham'>>> breakfast.append("toast") # add another item to the list>>> breakfast['ham', 'eggs', 'oatmeal', 'sausage', 'orange juice', 'toast']>>> breakast[-1] # spelled it wrong; we get the following immediate error messageTraceback (most recent call last):File "<pyshell#50>", line 1, in <module>breakast[-1]

NameError: name 'breakast' is not defined>>> breakfast[-1]'toast'>>> breakfast[4] = "grapefruit juice" # change orange juice>>> breakfast['ham', 'eggs', 'oatmeal', 'sausage', 'grapefruit juice', 'toast']>>>

Notice that a list has a set of indices (plural word for index). The first index value is zero (0), so it is a common error to get an off-by-one error when indexing through a list with iteration operations.

The common indexing iteration operations include for statements and while statements. There is also a less commonly used, but powerful iteration capability called recursion. We have a lesson in Python recursion later in this book. Meanwhile, please look at the diagrams in in Appendix A.

Note 1: Iteration is a word that means doing something over and over. Iteration of a list means, reference each item of the list on time until we have come to the end of the list. See Appendix A for diagrammed examples.

Reminder: A list is delimited between square brackets, [ and ].

Note 2: Index values for a list start with 0 and end with some other integer value. The length of the list is the last integer – 1.

Page 22: Python by Example - 9 June 2014

Python Programming by Example Page 22 of 82 Richard Riehle

Print The” List Items

What follows is an example of one kind of Python iteration. This is the so-called for…loop. The for…loop is an excellent construct for iterating over a fixed amount of data. You will see more examples of this construct in other parts of this book. The alternative to the for…loop is the while…loop. We use the while…loop when the amount of data to be processed is not bounded by a value in the data description. In the following examples, we put the Python special words in bold-faced type so you can distinguish them from the data itself.

## Declare a list of items>>> menu = ['hot dog', 'hamburger', 'french fries', 'apple pie']## create a for loop over the list>>> for i in menu:

print (i) ## print each item in the list

You can also print just the indices for this list:

>>> for index in range(len(menu)):print (index) ## print the index value for each item

Which will print,

0123

>>> fruit = ["apple", "orange", "pear"] ## create a list>>> emptyList = [] ## create an empty list>>> emptyList.append(fruit) ## append fruit list to an empty list>>> emptyList ## display emptyList[['apple', 'orange', 'pear']] ## a list within a list>>> emptyList[1] ## this will produce an error message>>> emptyList[1] [2] ## this will also produce an error message>>> emptyList[0] [1] ## this will produce the value “orange”'orange'

Recall that index values begin with 0

Page 23: Python by Example - 9 June 2014

Python Programming by Example Page 23 of 82 Richard Riehle

Or, from the above list named menu, you could explicitly address each item with a subscripting syntax,

>>> for index in range(len(menu)):print (menu[index]) ## print each item using the corresponding index

Notice that the subscript (index) for the list must be in square brackets instead of curved parentheses or curly braces. The variation in what kind of bracket to use and when to use it is a source of annoyance to newcomers to Python. Eventually, you will find it less annoying.

More List Operations

## Create a list of soft drink labels>>> sodaList = ["Pepsi", "Royal Crown", "Dr. Pepper", "7 Up", "Birch Beer"]>>> sodaList['Pepsi', 'Royal Crown', 'Dr. Pepper', '7 Up', 'Birch Beer'] >>> sodaList[0] = 'pepsi' ## make the correction to the soda name in place>>> sodaList[0] ## check the correction for correctness'pepsi' ## OK. The correction was successful>>> for soda in sodaList: ## create a loop to print the soda names

print (soda)pepsiroyal crowndr pepperbirch beer

Sort the List

## sort the sodaList creating a new list>>> Ordered_Soda_List = sorted(sodaList)## print the sorted list using a for loop>>> for soda in Ordered_Soda_List:

print (soda) ## remember to use parentheses for print7 UpBirch BeerDr. PepperPepsiRoyal Crown>>> seven upsprite## test equivalencies using x == y and x is in y syntax>>> OSL = Ordered_Soda_List>>> OSL is Ordered_Soda_ListTrue ## OSL and Ordered_Soda_List point to the same data

Page 24: Python by Example - 9 June 2014

Python Programming by Example Page 24 of 82 Richard Riehle

>>> OSL == Ordered_Soda_ListTrue ## OSL and Order_Soda_List are identical>>> OSL == sodaListFalse ## OSL is in different order from sodaList>>> New_OSL = sorted(sodaList)>>> New_OSL is OSLFalse ## New_OSL and OSL point to different locations>>> New_OSL = OSL>>> New_OSL = sorted (sodaList)>>> New_OSL == OSL ## New_OSL and OSL are identical?True>>> New_OSL is OSL ## New_OSL and OSL point to same data?False

Deleting an Item from a List

Consider the following list.

>>> theList = [23, 45, 92, 67, 82, 12, 78] ## original list content

>>> del(theList)[4] ## delete the 4th Item (82)>>> theList ## display the altered list[23, 45, 92, 67, 12, 78] ## 82 is now removed

Named Lists

You can give the list a name, and use that name for a large number of operations.

>>>CSClass = ["Lane", "Smith", "Johnson", "Sato", "McMann", "Conn", "Huynh", "Wright", "Witmer"] ## a list of names>>> for i in CSClass: ## loop through list of names

print (i) ## and print each name

>>>CSClass.append("Carpenter") ## how to append to the list>>>CSClass [5] ## display an item in the list

Here are some number list examples. Experiment with this list using IDLE.

>>> num01 = [23, 76, 42, 12, 17, 87, 43, 32, 93]>>> num02 = [62, 53, 41, 38, 25, 18, 16, 11, 5]>>> num01.append(num02) # a list that contains items of the same type is an array>>> num01.append(772) # arrays are an important kind of data type in programming

What does num01 look like after these operations? Try this.

>>> num03 = sorted(num02)

What does num03 look like after this operation?

Page 25: Python by Example - 9 June 2014

Python Programming by Example Page 25 of 82 Richard Riehle

Store List in an External File

You can also store the values from the list in a file using the following function. Note how we can use the print command for this implementation. Later, we will see other ways to do this.

>>>def creatfile(filename, the_list): ## create a file functionf = open (filename "w") ## open (create) a file named f

for i in the_list: ## traverse the list f.write(i) ## put each item in the filef.write(“\n”)

The file name can include a directory name for the computer. In most cases, you will want to put the file in a specific directory. The \n puts an end-of-line for each item.Here is sample code to create such a file.

>>> the_list = ["abc", "xyz", "qrs", "klm", "hcl"]>>> the_file = "c:\python32\plist.txt">>> crefl(the_file, the_list)

Get List from External File

>>> def getfl(filename):f = open(filename, "r")for theString in f:

print(theString)

Exercises

1. Create a list of animals or some other kind of list. Add new items to the list using the append command. Remove items from the list using indexing capability. Remember that indices in Python lists are zero relative. That is, x[0] would be the first item in the list.

2. Write a short loop that will display each item in the list, one item at a time, on the console. You will need the print() function for this exercise.

3. Create another list and append that list to your first list. Repeat the exercise 1 and 2 for this new list.

File Notes:

There are many ways to read and write file in Python. The above example is only one way. Another is a feature called “Pickle.” Pickle is one of the easiest file management routines.

Page 26: Python by Example - 9 June 2014

Python Programming by Example Page 26 of 82 Richard Riehle

Nested Lists – also called Matrices

A list can contain other lists. This allows for matrix operations and other more complex structures. Consider the following list that contains three other lists.

>>> nested = [ ['q', 'w', 'e'], ['a', 'b', 'c'] , ['x', 'y', 'z' ]]

This two-dimensional matrix represents a row and a column. For Python and most languages (not all languages), the matrix is row-major. This means subscript references such as

nested[2] [0] will address the letter ‘x’ in the third row and first column. Try to find the letter ‘b’ with a subscripted query.

Note: Matrices are an essential structure for a large number of mathematical problems. Python makes this easy for you with libraries of modules to support matrix mathematics. There is even a compatible library for MatLib users.

>>> for data in nested:print (data)

['q', 'w', 'e']['a', 'b', 'c']['x', 'y', 'z']>>>

Lists are zero-relative. The following example illustrates this point.

>>> nested [1] ## get the second list from parent list['a', 'b', 'c'] ## the result of nested[1]>>> nested [2] ## get the last nested list['x', 'y', 'z'] ## result of nested[2]>>> nested [2][2] ## get one item from nested[2]'z' ## returns a single character nested list>>> nested [0][0] ## get the first item in the first list'q'

Here is another, more complicated, nested list. Try to get a specific item from the list using the subscripting example from above.

>>> threeMeals = [['ham', 'eggs', 'oatmeal', 'sausage', 'grapefruit juice', 'toast'], ['hamburger', 'french fries', 'apple pie', 'cold soda', 'churro', 'tofu taco'], ['roast beef', 'baked potato', 'string beans', 'gravy', 'red wine', 'pecan pie with ice cream']]>>> threeMeals[['ham', 'eggs', 'oatmeal', 'sausage', 'grapefruit juice', 'toast'], ['hamburger', 'french fries', 'apple pie', 'cold soda',

q w ea b cx y z

From this example, a simple square matrix, we see that each level contains one alphabetic character. It prints a two dimensional array. Note that the array is row major

Page 27: Python by Example - 9 June 2014

Python Programming by Example Page 27 of 82 Richard Riehle

'churro', 'tofu taco'], ['roast beef', 'baked potato', 'string beans', 'gravy', 'red wine', 'pecan pie with ice cream']]

Lists can be nested as deep as you need to represent solutions to a large number of complex problems. You can also include instances of other composite data types in a list, and mix instances of different data types in those lists.

The next list is a bit difficult. It is a cube. This example could be used to represent any kind of three-dimensional matrix. You should experiment with it so you can understand it.

nested3D = [[[3,4,5], [5,2,7], [1, 7,9]], [[5,7,1], [9, 1,9], [7,8,7]],[[6,5,2], [4,1,4], [6,1,6]]]

A for loop will let us see this as a table.

>>> for i in nested3D:print (i)

[[3, 4, 5], [5, 2, 7], [1, 7, 9]][[5, 7, 1], [9, 1, 9], [7, 8, 7]][[6, 5, 2], [4, 1, 4], [6, 1, 6]]

The diagram shown above represents the values the three-dimensional matrix coded above. The major subscript is shown in the box to the left. On the right are some examples that produce the result shown in the assignment statement.

3 4 5

5 2 7

1 7 9

5 7 1

9 1 9

7 8 7

6 5 2

4 1 4

6 1 6

This example produces a 3D matrix with a row, a column, and a third element at each row and column. We need three subscripts to access an integer value in the table.

The for loop shows us the table elements as a matrix.

0

1

2

[0] [0]= 3, 4, 5

[1] [0] = 5, 7, 1

[1] [2] = 7, 8, 7

[2] [1] = 4, 1, 4

Examples

Page 28: Python by Example - 9 June 2014

Python Programming by Example Page 28 of 82 Richard Riehle

Now experiment to see what happens if you subscript this nested list.

>>> print (nested3D [0] [0] [0]) ## This should print 33 ## It does print 3>>> print (nested3D [2] [2] [1]) ## This should print 11 ## Voila! It is a 1>>> x = (nested3D[0] [0] [0]) + (nested3D [1] [2] [0])>>> x ## Adding 3 + 7 = 1010 ## Yes, we do get a 10

There is no limit to how many dimensions of a list you may have. However, for most people, there is a mental limit. In mathematics, we generally limit ourselves to two dimensions, and sometimes three dimensions. For most of us, the fourth dimension is a kind of twilight zone.

Exercises:

1. create a pair of two-dimension matrices of the same size and add them together creating a third matrix.

2. In a two-dimensional matrix, locate a specific value using the subscripting feature3. Write a nested loop that will traverse a two-dimensional matrix4. Define a parameterized function (def) that contains a two-dimension matrix traversal.5. Try to create a four-dimensional matrix or one of more dimensions. 6. Create two all numeric matrices and do linear algebra operations such as add and

multiply on them.

Page 29: Python by Example - 9 June 2014

Python Programming by Example Page 29 of 82 Richard Riehle

Some Simple Python Function Design Examples(These examples can help you understand how to design your own functions)

Functions are a fundamental feature of Python (and most other languages). You can enclose algorithms within functions and reuse them over and over without changing them. It is important that you include a parameter list for most functions. The function parameter list (some languages call this an argument) is enclosed in parentheses. Also, the function definition requires a colon ( : ) before writing the algorithm.

def area(height, width):return height * width

def cubeSurface(sideLength):return sideLength * sideLength * 6

def trapezoidArea(longBase, shortBase, height):return .5 * (longBase + shortBase) * height

To call the trapezoidArea function try the following:

>>>x = trapezoidArea(longBase = 4, shortBase = 3, height = 5)

def getAndPrint():data = input ("Enter a value: ")print ("The Input is: " + str(data))

def getComputePrint(Value):InputValue = input ("Enter a number: ")print (Value + int(InputValue)) ## Need to convert input to int for arithmetic operation

def buildIntList(): ## A simple way to build a list of integers from the keyboardtheList = []while True:

data = input("Enter an Integer: ")if data.isnumeric():

theList.append(int(data))else:

print("End of input to build list ")return theList

def build2DArray(): ## Creates a two dimensional array of integers from the keyboardrowList = []colList = []while True:

col1 = input("Column 1 ")col2 = input("Column 2 ")if col1.isnumeric() and col2.isnumeric:

colList.append(col1)colList.append(col2)rowList.append(colList)colList = [] ## Be sure to clear the colList

else:print("End of input to build list ")return rowList

Page 30: Python by Example - 9 June 2014

Python Programming by Example Page 30 of 82 Richard Riehle

Here is another example of a matrix using a nested List. In this example, we see how we can do simple matrix arithmetic, as suggested in the exercise from the last section.

Notice the title box, below. This is a good way to do document all your programs.## ============================================================## Title: Matrix_Methods.py Date: 3 Nov 2008## Author: John Jacob Jingleheimer Schmitdz## Description: A set of matrix operations## ============================================================##def ZeroIt(m1):

result = m1for i in range(len(m1)):

for j in range(len(m1[i])):result [i] [j] = 0

return result

def SumIt(m1, m2):assert len(m1) == len(m2), "make sure the matrices are the same size"result = m1for i in range(len(m1)):

for j in range(len(m1[i])):result [i] [j] = m1[i][j] + m2[i][j]

return result

def scalarMultiply(theScalar, theMatrix):result = theMatrixfor i in range(len(theMatrix)):

for j in range (len(theMatrix[i])):result[i][j] = theScalar * theMatrix[i][j]

return result

The student is encouraged to expand this example with additional matrix operations. For example, simultaneous sets of linear equations in multiple unknowns are sometimes easier to solve using matrix inversion. You could add a matrix inversion function to the above set of functions. This might require those of you who have forgotten how matrix math works to consult a textbook.

The assert statement is a useful way to check the preconditions for your defined function.

In mathematics, it is common to use i, j, and k as index names when dealing with matrices.

Page 31: Python by Example - 9 June 2014

Python Programming by Example Page 31 of 82 Richard Riehle

Dictionary Type

In a dictionary, we can group information by name. A dictionary is defined with curly-braces instead of square brackets. This is one of the most powerful features of Python. With it, you can create lists that include a key, and later, access the content referred to by that key.

The format of a dictionary item is:

for as many items as you wish to include in the dictionary structure. The key can refer to a list, another dictionary item, a set, or a string. We can also store the dictionary in a file. In this section you will see many kinds of dictionary items, and we will create some functions that you can use to create your own dictionary structures.

>>> simpleDict = {1 : "Open", 2 : "Close", 3 : "Ajar"}>>> simpleDict[2]'Close'

The above simpleDict has three elements keyed with numbers. There are three dictionary items named: “Open”, “Close”, “Ajar”. The keys for these items are respectively, 1, 2, and 3. The key named 1 will access the item we call “Open”.

For a dictionary, the number is not the relative location of the information. Rather, it is a lookup key. Consider the following revision where the numbers are in another order.

>>> simpleDict = {2 : "Open", 1 : "Close", 3 : "Ajar"}

In this version, the 2 is the first key. It refers to “Open”. Now, the same statement as before,

>>> simpleDict [2]'Open'

will produce a totally different answer because we altered the definition of the key. The lesson is that the key is not a physical index location; it is a logical key that, regardless of its value, can be located anywhere in the dictionary structure.

Item key

Actual Item

(key identifier : item, item, item …, key identifier : item, item, item, …)

Page 32: Python by Example - 9 June 2014

Python Programming by Example Page 32 of 82 Richard Riehle

A dictionary can do the following, using the nested list from the example shown earlier, putting each nested group in a dictionary (note the two-line definition):

>>> nested = {'qwerty' : ['q', 'w', 'e', 'r', 't', 'y'], 'constants': ['a', 'b', 'c'], 'variables': ['x', 'y', 'z']}>>> nested['constants'][1]'b'

In the above example, the keys are named: ‘qwerty’, ‘constants’, and ‘variables’. We can directly access each of the sub-categories of information using a name instead of simply a number. In many kinds of applications, especially when there is a lot of information, the dictionary is a good choice for how to represent your data in Python.

A better way to define threeMeals (earlier example) is to use a Python Dictionary type so we can explicitly identify each kind of meal by its classification. For example,

>>> threeMeals = {"breakfast" : ['ham', 'eggs', 'oatmeal', 'sausage', 'grapefruit juice', 'toast'], "lunch" : ['hamburger', 'french fries', 'apple pie', 'cold soda', 'churro', 'tofu taco'], "dinner" :['roast beef', 'baked potato', 'string beans', 'gravy', 'red wine', 'pecan pie with ice cream']}>>> threeMeals["dinner"]With the result of the query printing on the screen,

['roast beef', 'baked potato', 'string beans', 'gravy', 'red wine','pecan pie with ice cream']>>>

In the above example, notice that the threeMeals are identified as breakfast, lunch, and dinner (in quotes) where each kind of meal is followed by a colon. Immediately after the colon, we see a list (in squared brackets) listing the items for that kind of meal.

We can change the kind of potato we have for dinner with a simple statement,

>>> threeMeals["dinner"][1] = "mashed potato" ## change dinner [1]>>> threeMeals ## display entire dictionary{'lunch': ['hamburger', 'french fries', 'apple pie', 'cold soda', 'churro', 'tofu taco'], 'breakfast': ['ham', 'eggs', 'oatmeal', 'sausage', 'grapefruit juice', 'toast'], 'dinner': ['roast beef', 'mashed potato', 'string beans', 'gravy', 'red wine', 'pecan pie with ice cream']}>>>

Or we can see threeMeals what’s for dinner by simply entering,

>>> threeMeals["dinner"] ## List elements for dinner['roast beef', 'mashed potato', 'string beans', 'gravy', 'red wine', 'pecan pie with ice cream']>>>Note that the basic structure for a Dictionary item is:

DictionaryItemName = {key1 : data, key2 : data, key3: data, … , keyn : data}

We use bold letters to identify the keys. This is not true of Python in use.

Page 33: Python by Example - 9 June 2014

Python Programming by Example Page 33 of 82 Richard Riehle

… where keyn is the last item in the dictionary. There are more ways to declare and process Dictionary data, but you are directed to find those in one of the many on-line tutorials available for Python.

Here are some additional examples for you to enter and use for your own experimentation.

>>> d2 = {'stockno' : "", 'quantity' : "", 'description' : ""}>>> d2 ## List elements of d2{'stockno': '', 'description': '', 'quantity': ''}>>> d2['stockno'] = 3472 ## change value of ‘stockno’>>> d2 ## list elements of updated d2{'stockno': 3472, 'description': '', 'quantity': ''}>>> d2['description'] = 'the description' ## update d2>>> d2['quantity'] = 42 ## update d2>>> d2 ## list updated d2{'stockno': 3472, 'description': 'the description', 'quantity': 42}>>> for value in d2.values(): ## loop through the values of d2

print (value)3472the description42

The following function allows you to add items to a list.

>>> def dictMaker(theKey, theList, theDict = {}):theDict[theKey] = theListreturn theDict

Below are two lists we will add into a dictionary we will call d1

>>> d1 = {}>>> aList = ['a', 'b', 'c']>>> bList = ['n', 'g', 'r']>>> dictMaker (theKey = 1, theList = aList, theDict = d1){1: ['a', 'b', 'c']}>>> dictMaker (theKey = 2, theList = bList, theDict = d1){1: ['a', 'b', 'c'], 2: ['n', 'g', 'r']}>>> d1[1]['a', 'b', 'c']>>> d1[2]['n', 'g', 'r']

In the above example, we created a function that takes three arguments, the key of an item in the dictionary, the name of the data (specified as a list, but it could be anything), and the name of the dictionary.

We created two lists. Then we called the dictMaker function twice using named association (specifying the parameter name = argument name) to create two entries in the dictionary. Finally, we listed each item individually.

Page 34: Python by Example - 9 June 2014

Python Programming by Example Page 34 of 82 Richard Riehle

Dictionary Version of Case Statement

A common complaint about Python is that there is no “case” statement. This is easily accommodated using a dictionary.

def f1():print ("called f1")

def f2():print ("called f2")

def f3():print ("called f3")

ftable = {1: f1, 2 : f2, 3 : f3} ## ftable contains keys and functions

def fselect(number):ftable[number]()

This example also illustrates another important feature of Python: functions can be first-class objects. By that we mean that a function can be used as if it were data. This is an example of a feature from functional programming called, referential transparency. It means that the name of a data item or a function is seen by Python in the same way. The programmer can pass functions around, use functions as parameters to other functions, and treat functions as if they are data.

The reason this is possible in Python is that the name given to both a function and a data item is simply a reference to some other location in memory. In language such as C or C++, the programmer must create a pointer to accomplish this. In Java, it requires creating an entire class with a method. Python simplifies this for the programmer by treating every name of every element of a program in the same way.

Explanation:

1. Create some functions using the def command. These examples would be do more in real design. 2. Then, create a dictionary table of the functions with a dictionary key. In this case, we simply used a number.3. Then, create a function that will call the program from the table based on value in the parameter of that

function.4. Test the design by calling the fselect() function for each function in the dictionary.

f3 is a function

Page 35: Python by Example - 9 June 2014

Python Programming by Example Page 35 of 82 Richard Riehle

Python Strings

The list and dictionary data-types we have studied so far are mutable. That is, they can be added-to, have data items inserted, or removed. This is not true of Python strings. Strings are immutable. They cannot be changed once they are created. Where a list uses square brackets as the delimiter for its data, a string uses parentheses. The fact that Python uses different delimiters for different data types is sometimes confusing to the Python novice.

A simple string example

## First create a string using parentheses instead of square brackets.## Note that, unlike lists, strings are not mutable (changeable).

>>> s1 = ("Now is the time for all good people to come to the aid of their friends")>>> len(s1) ## What is the length of the string?71>>> x = sorted(s1) ## Sort the characters in the string producing new string>>> x ## Print the new sorted string; s1 has not changed[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'N', 'a', 'a', 'c', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'f', 'f', 'f', 'g', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'l', 'l', 'l', 'm', 'm', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'p', 'p', 'r', 'r', 'r', 's', 's', 't', 't', 't', 't', 't', 't', 'w']>>> s1[0] ## What is the first character in the string?'N'>>> if 'all' in s1: ## Is the word ‘all’ present in the string?

print ('OK')OK

## Create a function that will look for a word in the string>>> def checkIt(aWord, theString):

if aWord in theString:print ('The word is present')

else:print ('The word is not present in this string ')

>>> checkIt('all', s1) ## Is the word all present in the string?The word is present>>> checkIt('ukemi', s1) ## Is the word ukemi present in the string?

Page 36: Python by Example - 9 June 2014

Python Programming by Example Page 36 of 82 Richard Riehle

The word is not present in this string

String Methods

str.isalnum() True if string contains only alphabetic characters or digitsstr.isalpha() True if string contains only alphabetic charactersstr.isdigit() True if string contains only numeric digitsstr.isspace() True if all alphabetic characters are lower-casestr.isupper() True if all alphabetic characters are upper-case

str.lower(0 Return lower-case version of stringstr.lstrip() Return string with leading whitespace removedstr.rstrip() Return string with trailing whitespace removedstr.strip() Return string with leading and trailing whitespace removedstr.upper() Returns string converted to upper-casestr.split([sep[, maxsplit]]) Break string into words and return resultstr.swapcase() Swap upper and lower case and return resulting string

An important concept in string handling (as well as other most other collection types) is the ability to reference a selected part of the collection. We illustrate this several times in this book because it is so important. Here is an example for printing part of a string.

>>> str1 = ("How now brown cow")>>> str2 = ("The quick brown fox jumped over the lazy dog")>>> print(str1[3:6] + str1[13:17]) ## select two parts of the string to printno cow ## result of previous statement

The reader is encouraged to experiment with this feature. The more you experiment, the better.

You can also subscript from the end of the string using a negative sign. Here is an example that gives the exact same result as the previous example.

>>> print (str1[3:6] + str1[-4:]) ## catenate 3:6 with the last four charactersno cow ## same result as before

It is sometimes useful reverse a string entirely with the following syntax:

>>> print (str1[::-1]) ## request string in reverse orderwoc nworb won who ## print the string in reverse order

Page 37: Python by Example - 9 June 2014

Python Programming by Example Page 37 of 82 Richard Riehle

Iteration Operations

Python provides three methods for iteration: for loops, while loops, and recursion. We provide some examples of each in this section. Some of the examples use the Python function definition feature which we discuss in more depth a little later.

The loop shown above illustrates the fundamental idea (see other examples toward the end of this book). The program tests a condition for true or false. In the illustration shown, if the result is true, we do a process; if false we drop through to the end of the iteration. The small circle indicates the end of scope for the loop.

For Loops

The for loop is often used when we have a well-defined and bounded structure such as a list or dictionary over which to iterate.

>>> import math>>> for i in range(1,10): ## index from 1 to 10

print (i, 2*i, i**2, math.factorial(i)) ## use i1 2 1 1 ## list of output from print iteration2 4 4 23 6 9 64 8 16 245 10 25 1206 12 36 7207 14 49 50408 16 64 403209 18 81 362880

T

F

ProcessCondition

This is one of the annoying features of Python. The second value in the range is not inclusive but terminal. Therefore, this loop stops at 9 instead of 10 as we might have expected

The word iterate means to repeat over and over. In programming, it means to repeat over and over in some kind of data structure until we have visited every item of data item in that structure. Sometimes we skip every other item, or select which items we want to include in the iteration.

Page 38: Python by Example - 9 June 2014

Python Programming by Example Page 38 of 82 Richard Riehle

Using the Python Range feature in a for loop

>>> def SumToMax(max): ## call with max valueresult = 0 ## declare a resultfor i in range(1, max + 1): ## add 1 to max for correct result

result = result + i ## add i to the resultreturn result ## return the result

>>> SumToMax(5) ## call the SumToMax function15

Alternate version of Summation Example

>>> def SumToMaxDirect(max): ## Thanks to Karl Friedrich Gaussreturn (max * (max + 1)) / 2

Here is a version of the above program that will produce the sum of any starting value to some maximum value.

>>> def sumFromAnyStart(start, max):temp1 = 0temp2 = 0temp1 = (start * (start - 1)) / 2temp2 = (max * (max + 1)) / 2return temp2 - temp1

>>> sumFromAnyStart(start = 5, max = 100)5040

Although this section is about iteration, we show the two alternatives to demonstrate that many functions that might be programmed as iterations, can sometimes be simplified as a direct computation.

This example shows a simple summation function.

Now we call the function

This version is more efficient

This version will take any starting value to any maximum value

Page 39: Python by Example - 9 June 2014

Python Programming by Example Page 39 of 82 Richard Riehle

For Loop Examples in Python Functions

## ============================================================## Author: John Jacob Jingleheimer Schmidt## ## Sample for loops#### ============================================================##from random import * ## import random number generator

def f1(n): ## call with a single parameterprint (n) ## e.g., f1(56749)

def f2():data = input ("enter some data: ")

def f3(n): ## Scalar (single value) parameterreturn n + 1 ## This example returns a value

def f4(fn, aList): ## This function takes another functionfor data in aList: ## as its first argument. It applies that

fn(data) ## function to members of a list

def f5(aList): ## function takes a single listfor data in aList: ## each item is assigned to data

print (data) ## print each item in the list

def f6(aList):for data in range(len(aList)):

print (str(data) + " " + str(aList[data])) ## converts data to a string

def forExample_1():for i in range(1, 10): ## This will not include 10

print (i) ## Prints up to, but not including 10

def forExample_2(args): ## requires a listfor data in args:

print (data)

def forExample_3(*args): ## requires a list of numbersresult = 1 ## e.g., forExample_3(12, 56, 32, ...)for data in args:

result *= datareturn result

An author, date, and revision banner is always useful in a production program

Page 40: Python by Example - 9 June 2014

Python Programming by Example Page 40 of 82 Richard Riehle

While Loops

The while loop has two general forms. The first and most common while loop has a condition test prior to executing the instructions in the algorithm. This is called a “while do” variation (or sometimes called a test-before loop). The second version tests the condition after completing some action and continues to repeat this test until the algorithm results in some terminating condition. This is called a “do while” loop or sometimes called a “test-after” loop.

The while loop is used when the condition is based on some future condition rather than, as for the for loop, a condition based on the consumption of all the data in a bounded structure.

def whileExample_1():data = 0while data >= 0:

value = input("Enter a number: ")print (value)data = int(value) ## convert the input to an integer

print (" Program terminated. data value = " + str(data))

def whileExample_2():while True:

value = input("Enter a number: ")if int(value) < 0:

print (value + " and not OK. Program stops ")break

else:print (value + " OK value. Continue. ")

print(" Program is stopping because of negative value in input ")

def while_withTryBlock():print ("Enter an integer for the list ")print ("Enter single * to terminate program and return the list")inputData = ''theList = []while True:

try:inputData = (input("Enter a number: "))theList.append(int(inputData))

except:if inputData == "*":

print ("Program is finished ")return theList

else:print ("The input must be an integer ")continue

return theList

Page 41: Python by Example - 9 June 2014

Python Programming by Example Page 41 of 82 Richard Riehle

Working With Sets

## A set is defined with curly-braces; a set is not a list; it is its own unique data type

>>> set_1 = {'sdf', 'qaz', 'wsx', 'edc', 'rfv', 'tgb', 'yhn', 'ujm', 'ik', 'ol', 'p'} ## create a set>>> set_1 ## show the items in the set{'edc', 'ol', 'yhn', 'rfv', 'ujm', 'sdf', 'qaz', 'tgb', 'p', 'ik', 'wsx'} ## set is not ordered

>>> for i in l1:print (i) ## for loop to print set item one at a time

edcolyhnrfvujmsdfqaztgbpikwsx

>>> set_2 = sorted(set_1)>>> "p" in set_1 ## Is “p” in the setTrue>>>set_1.add(“32”) ## Add “32” to the set>>> set_1.remove('rfv')>>> set_3 = set_1.copy()>>> set_3{'edc', 'ol', 'yhn', '23', 'ik', 'p', 'qaz', 'tgb', 'ujm', 'sdf', 'wsx'}>>> set_1 = {}>>> set_3{'edc', 'ol', 'yhn', '23', 'ik', 'p', 'qaz', 'tgb', 'ujm', 'sdf', 'wsx'}>>> set_1 = set_3>>> set_1{'edc', 'ol', 'yhn', '23', 'ik', 'p', 'qaz', 'tgb', 'ujm', 'sdf', 'wsx'}

There are many more set operations. It is important to remember that sets are not indexed in the same way as other kinds of structures. Also, they are not ordered. They do not have more than one instance of any element.

Page 42: Python by Example - 9 June 2014

Python Programming by Example Page 42 of 82 Richard Riehle

More on Python Functions

A mathematical function is defined as y = f(x) where y is the value returned after the function performs a computation on the parameter named x. A function may have more than oneparameter as in y = f(a, b, c) where it performs an operation involving those three parameters and returns a result in y. Python allows for the same kind of operations by allowing the programmer to invent and compose functions that behave much like mathematical functions.

Simple Python Function

def f1(x):return x + 1

This function will increment the value given to the parameter “aValue” and return the result to the caller. Here is an example:

>>> y = f1(24) ## y = f1(x)>>> y ## display the value of y25 ## result of calling the function

Where f1 (the name of the function) is called with an actual parameter of (24). The actual parameter is associated (replaces) the formal parameter (aValue) and is used in the computation. When the computation is completed, a special statement (return) replaces x (on the left side of the assignment statement) with the result of that computation.

Function Sample Two

>>> def f2(a, b, c):return (a + b) / c

This function will increment the value given to the parameter “aValue” and return the result to the caller. Here is an example:

>>> y = f2(12, 3, 6) ## y = f1(a, b, c)>>> y2.5

Where f2 (the name of the function) is called with three actual parameters. We could also have called the function (any function, really) using named association. So, we could have also called f2 with the following and achieved the same result:

y = f2(a = 12, b = 3, c = 6) ## using named association

Page 43: Python by Example - 9 June 2014

Python Programming by Example Page 43 of 82 Richard Riehle

The technique just shown (named parameter association) allows the programmer to be specific in how the actual parameters are associated with the formal parameters. It is often useful in production programming where there are a lot of formal parameters, or where the function is in a library not easily visible to the user of the function. Name association is not common in other languages, but another language that does support it is Ada. It is a feature that, for large-scale programs, allows for an additional level of self-documenting code.

Function with a While Loop

This is an example of a Python function. This function does not have a return value. Instead, it does a computation and prints the result on the default terminal (usually a video display terminal). The name of the function is “interest.” In parentheses there are three parameters (sometimes called arguments). We often call these the “formal” parameters. Notice the indentation. If you are using IDLE the indentation will occur automatically. You can reconfigure IDLE to change the level of indentation, but once you change it, use those changes and let IDLE decide how to do that indentation.

def interest(principal, rate, numyears):year = 1while year <= numyears:

principal = principal * (1 + rate)print "%3d %0.2f" % (year, principal)year += 1

To call this function from the Python Shell or from IDLE you can write:

>>> x = interest(2000.0, 5.0, 24):

or>>> x = interest(principal = 2000.0, rate = 5.0, numyears = 24)

In the invocation of the interest function, we provide “actual” parameters that will associate with the formal parameters. The first example shows for actual parameters alone. The second example shows how we can, in Python, directly associate the formal parameters with an actual parameter using “named association.” This allows us to call the function with the parameters in any order we wish. For example, the following is legal Python:

>>> x = interest(numyears = 24, rate = 5.0, principal = 2000.0)

allowing the call to specify the actual parameters in any order by using the explicit “named association” feature.

Page 44: Python by Example - 9 June 2014

Python Programming by Example Page 44 of 82 Richard Riehle

File Variation of Interest Function

The following variation allows the interest function to open a file, and print the result of the computation to that file. The open feature, which is part of standard Python, allows our program to read, write, or append to a file. In the example shown, the file is opened using a “w’ indicating that the file is opened for writing.

def interest(principal, rate, numyears):f = open ("interestfile.txt", "w")year = 1;while year <= numyears:

principal = principal * (1 + rate);print >>f, "%3d %0.2f" % (year, principal)year += 1

f.close()

Nested Function

Sometimes it is useful to be able to nest one function within another. The outer function will call the inner function. The outer function will return the result. Keep this in mind when we discuss Python decorator functions.

def outer(a, b):temp = 0print ("intializing outer ")def inner(x, y): ## a nested function

print ("Executing inner ")return x + y

print ("executing call to inner from outer ")temp = inner(x = a, y = b) ## call inner from outerprint ("now we return the result of inner ")return temp

Suppose we need a function that will take a variable number of arguments. We can accomplish this with a function where the formal parameter is simply *args. Below is an example.

>>> def variableArgCount(*args): ## unknown number of argsresult = 0 ## temporary local storagefor arg in args: ## get each arg one at a time

result = result + arg ## produce sum of argsreturn result

>>> variableArgCount(2, 2, 2) ## call function with three args6 ## 2 + 2 + 2 = 6

Page 45: Python by Example - 9 June 2014

Python Programming by Example Page 45 of 82 Richard Riehle

From the software engineering perspective, nested functions are usually discouraged. An important idea to keep in mind is that a function should do only one thing, not multiple things. In computer science, that rule/guideline, is called: cohesion. The other rule/guideline is to design a function so it is not dependent of other functions. This allows functions to be reused in many ways throughout a design. For example, the math.sin function is loosely-coupled and can be used anywhere one might need such a function. Computer scientists call this: low-coupling.

Exercises

1. Write a function that takes two parameters and returns the sum of those two parameters.2. Write a function that takes a list (see the list examples) and prints all the items in the list.3. Write a function that calls another function, where that function being called returns a

value to the calling function, which in turn returns a value to the original caller. Example, you might a function that returns the sum of a list of values and a function that produces the mean of that list. Have the mean function call the summation function to get the sum of the values, and then divide that sum by the number of items in the list.

4. Write a function that computes the area of a polygon such as a triangle. Try some other polygons; try a circle; try an ellipse; try the area of a cone; other shapes.

Page 46: Python by Example - 9 June 2014

Python Programming by Example Page 46 of 82 Richard Riehle

Numbers in other Number bases

Computers are handy for representing numbers in more ways than the simple decimal numbers.Sometimes it is useful to represent a number in its pure binary form, hexadecimal, or even octal notation. Python provides a rich set of operations for doing this kind of thing Here are some examples of numbers in other bases that could be useful to you in the future.

>>> n1 = 42 ## declare a value for n1>>> n2 = bin(n1) ## convert n1 to binary and bind to n2>>> n2 ## check the string value of n2'0b101010'>>> n3 = hex(n1) ## convert n1 to hex and bind to n3>>> n3 ## check the value of n3'0x2a'>>> n4 = int(n2,2) ## convert n2 to an integer; bind to n4>>> n4 ## check the value of n442>>> n4 = int(n2,2) + 8 ## add 8 to integer value of n2>>> n4 ## check n4; is it correct?50>>> 234 / 5 ## divide 234 by 546.8 ## result is a floating-point number>>> 234// 5 ## divide 234 by 5 using //46 ## result is truncated integer value>>> 234 % 5 ## do modulus division4 ## remainder result is 4>>> divmod(234, 5) ## use the divmod function(46, 4) ## get dividend with remainder>>> dec25 = 25 ## December 25 (Christmas)>>> oct31 = 0o31 ## October 31 (Halloween)>>> dec25 == oct31 ## Compare December 25 to October 31?True ## Apparently Christmas == Halloween!

Page 47: Python by Example - 9 June 2014

Python Programming by Example Page 47 of 82 Richard Riehle

Functions as First-class Objects.

In the next section, we look at the notion of a Python function. This is one of the most important features of Python. It is a little different from other programming languages, so you may find yourself confused, at first, if you are accustomed to languages such as Java, C, C++ or even Ada.One of the most important ideas about Python functions is that they are first-class objects. Before proceeding with the discussion of functions, we need to explain what we mean by a first-class object.

In Python, every item you define to the left of an assignment statement is a first-class object, and every item you define with a def operator is also a first-class object. Every first-class object can be treated as if it is data. Therefore, a first-class object, including a function name, can be used as a parameter to another function.

The reason for this is because of how Python creates data at the time an item is defined. If one defines a function as:

def f1(x):return x

Python creates a reference named f1 that points to the actual implementation of the function.

Everytime you reference f1 you are simply referencing a pointer to the actual function. Therefore, f1 is not the function, it is only a pointer to the real function. If you should decide to assign a value to f1, instead of calling it as a function, you will break the pointer to the function and reference data instead. For example,

>>> f1= 42>>> f142

The original function, f1, is now gone. Python has automatic “garbage collection” so the original storage for f1 as a function has not been lost.

There is a category of programming languages explicitly called functional programminglanguages. This include LISP, Scheme, ML, OCAML, Haskell, and others. In those languages, a function is always a first-class object. This capability, along with lambda expressions (discussed later) gives the Python programmer the ability to emulate functional programming. As a point of interest, the C# programming languages does support this capability with delegates.

Implemention of f1f1

A data storage area containing 42f1

Page 48: Python by Example - 9 June 2014

Python Programming by Example Page 48 of 82 Richard Riehle

Passing a function as a parameter

One of the unique and powerful features of Python is its ability to pass a function as a parameter to another function. This provides a capability to incorporate functional programming into otherwise procedural programs. In languages such as C, C++, and Ada, the programmer is required to use a pointer or reference to do this. In Java, the programmer needs to design an entire, single-method, class to accomplish this. With this feature, the designer can create generalized (generic?) functions that can behave differently depending on what function is passed as a parameter. This capability, in Python, makes the language hospitable to a variety of mathematical algorithms that would be otherwise more cumbersome.

## Define a function that sums a list with a function as parameter>>> def SquareSum(aFunction, aList): ## aFunction is an actual function

result = 0for i in aList:

result = result + aFunction(i)return result

## Define a function that squares a value>>> def SquareItem(anItem):

return anItem * anItem## Create a list of numbers>>> theList = [2, 3, 4, 5]## Call the function with a list and the function, SquareItem>>> SquareSum(aFunction = SquareItem, aList = theList)54 ## Result of calling the SquareSum function shown above

Make the function example more explicit

## put the word being tested in the output string

def checkIt(aWord, theString):if aWord in theString: ## membership testing is powerful in Python

print ('The word is present')else:

print ('The word ', aWord,' is not present in this string ')

## Now test the string for presence of the word.>>> checkIt(aWord = 'time', theString = s1) ## check the stringThe word is present>>> checkIt(aWord = 'howdy', theString = s1) ## check the stringThe word, ‘howdy’,is not present in this string

The mathematically-inclined reader will quickly see the potential for using this kind of function in solving calculus problems such as numerical integration

Page 49: Python by Example - 9 June 2014

Python Programming by Example Page 49 of 82 Richard Riehle

Numerical Integration

## Integration_One.py## Demonstrates function as a first-class object## to enable the Calculus of numerical integration.#### This is a rectangle version of computation where the function## simply multiplies the height by the width.def integrand(width, height):

return width * height

## This version has a fixed increment width so the list of values## specifies only the height of the rectangledef integrate_1(theFunction, dataWidth, heightList):

result = 0for height in heightList:

result = result + theFunction(width=dataWidth, height=height)return result

## This version requires a two-dimensional array where the## the width of the increment may varydef integrate_2(theFunction, heightAndWidthList):

result = 0for data in heightAndWidthList:

result = result + theFunction(data[0], data[1])return result

## Sample Test for Integrate_1 ## Should produce 222>>> heightData = [12, 15, 18, 14, 9, 6]>>> widthData = 3>>> x = integrate(theFunction = integrand,

dataWidth = widthData,heightList)= heightData)

## Sample Test for Integrate_2 ## Should produce 222>>> heightAndWidth = [[12, 3], [15, 3], [18,3], [14,3], [9, 3], [6,3]]x = integrate_2(theFunction = integrand, heightAndWidthList = heightAndWidth)

Another example with Two-Dimension Array (List of Numbers)

def sumRectangles(compute, theArray): ## compute is a functiontheAnswer = 0for item in theArray:

theAnswer = theAnswer + compute((item[0]), (item[1]))return theAnswer

The above function takes a two dimensional list, applies the compute function to each item in the array, and add it to the theAnswer. At the conclusion, it returns the result of the algorithm.

Page 50: Python by Example - 9 June 2014

Python Programming by Example Page 50 of 82 Richard Riehle

Assertions for pre-, and post-conditions

It is often useful to put constraints on the values being computed in a function. Python has several ways of doing this. Assertions are the easiest approach.

>>> def compute_1(a, b):assert a <= 9assert b <= 9return a * b

>>> compute_1(6, 2)12>>> compute_1(12, 4)Traceback (most recent call last): ## failed the pre-conditionFile "<pyshell#300>", line 1, in <module>compute_1(12, 4)

File "<pyshell#297>", line 2, in compute_1assert a <= 9

AssertionError>>> def compute_2(a, b):

assert a <= 9, "precondition error"assert b <= 9, "precondition error on b"return a * b

>>> compute_2(8, 12)Traceback (most recent call last):File "<pyshell#308>", line 1, in <module>compute_2(8, 12)

File "<pyshell#307>", line 3, in compute_2assert b <= 9, "precondition error on b"

AssertionError: precondition error on b>>> compute_3(12, 3)36>>> compute_3(19, 12)Traceback (most recent call last):File "<pyshell#317>", line 1, in <module>compute_3(19, 12)

File "<pyshell#314>", line 3, in compute_3assert result <= 100

AssertionError>>> def compute_3(a, b):

result = a * bassert result <= 100, "postcondition error on result"return result

>>> compute_3(19, 12)Traceback (most recent call last):File "<pyshell#323>", line 1, in <module>compute_3(19, 12)

File "<pyshell#322>", line 3, in compute_3assert result <= 100, "postcondition error on result"

AssertionError: postcondition error on result

The concept of pre-, and post-conditions originates with the work of Floyd and Hoare. The model is sometimes shown as:

Pre-condition {Process} Post-conditionand called a Hoare triplet; alternative, from Bertrand Meyer,

Require {Algorithm} Ensure

Page 51: Python by Example - 9 June 2014

Python Programming by Example Page 51 of 82 Richard Riehle

lambda functions

## You can build entire functional programs with lambda functions and never write a loop. ## These are fundamentally stateless. This is a powerful feature of Python, and is possible ## because every variable is a first-class object (see earlier discussion on this topic)

>>> s = lambda x: "" if x == 1 else "s" ## test for 1>>> s(3)'s'>>> s(1)''>>> f1 = lambda x: (x * (x +1)) / 2 ## compute sum of 1 thru x>>> f1(5)15.0>>> f2 = lambda x, f: f(x) ## apply function to x>>> def squareIt(v): ## sample function

return v * v

>>> f2(5, squareIt) ## apply the squareIt function25>>> result = f2(25, squareIt) ## apply the squareIt function>>> result625>>> result = f2(625, math.sqrt) ## apply the math.sqrt function>>> result25.0>>>>>> h = lambda a, b: math.sqrt(a**2 + b**2) ## Pythagoras formula>>> h (3, 4)5.0>>> f8 = lambda x: x**2>>> h2 = lambda a, b: math.sqrt(a + b) ## a and b already squared>>> h2 (f8(3), f8(4)) ## call with a lambda function5.0>>>>>> rectangular_box = lambda length, width, height : length * width * height>>> rectangular_box(20, 14, 56)15680>>> rectangular_box_volume = lambda length, width, height : length * width * height>>> rectangular_box_surface_area = lambda length, width, height : ((2 * width * length) + (2 * width * height) + (2 * length * height))>>> rectangular_box_surface_area(20, 14, 56)4368

An important idea for functional programming.

Page 52: Python by Example - 9 June 2014

Python Programming by Example Page 52 of 82 Richard Riehle

Recursion

We covered a kind of iteration earlier using the Ptyhon for loop and while loop. There is another kind of iteration that is sometimes used in software applications: recursion. In recursion, we have the counter-intuitve concept of a function calling itself over and over. Recursion requires each call to place an entry on a stack. A properly designed recursion requires logic in the function to determine when to stop making the calls to itself. Without such logic, the function would continue calling itself until it ran out of stack memory.

The typical first example of recursion in any programming language is the factorial computation. Those familiar with mathematics will recall this as a notation such as 5! where the exclamation point is read, “factorial”. Therefore, we read this notation as “five factorial”. Consider the following example:

Here is a diagram from a Stanford University site that shows how this works.

Each iteration puts a multiplied value in the stack. At five factorial, the value is 120 (as in our Python example). This diagram shows that 6 X 120 = 720. Consequently, the answer will be based on the largest value of the factorial for the starting value (always the larger number) we decide we want for (n).

def factorial( n ):if n <1: # ends the recursion function

return 1else:

return n * factorial( n - 1 ) # recursive call

>>> factorial(5)120

Page 53: Python by Example - 9 June 2014

Python Programming by Example Page 53 of 82 Richard Riehle

Here is another example of a recursive algorithm in Python. In this example, we have an ordered list of fourteen numbers, indexed from 0 through 13. We want to find the index value for one of those numbers.

theList = [12, 14, 49, 57, 60, 63, 67, 71, 74, 78, 80, 83, 88, 90]

The algorithm will look like this:

We can call it using named association for clarity and good documentation as follows:

BinSearch(SearchItem = 80, OrderedList = theList,lowIndex = 0, highIndex = len(theList))

Since the SearchItem = 80, the algorithm will return the index value of 10. Note that this algorithm is recursive. It continually calls itself, modifying the value of the parameters based on what it discovers with each iteration. Is the value too high, search a lower half from where we are? If it is too low, search a higher index from where we are.

There are many opportunities for recursion in computer programming. The most important rule: a recursive algorithm must encounter a condition under which it will terminate. We encourage you to go on-line and search for more examples of recursion. However, be careful with published examples. There are Python examples of the binary search algorithm, and others, that are incorrect as published. The examples in this book actually behave correctly. They have been tested for correctness.

def BinSearch(SearchItem, OrderedList, lowIndex, highIndex):if lowIndex > highIndex:

return -1midPoint = int((lowIndex + highIndex) / 2)foundItem = OrderedList[midPoint]if foundItem == SearchItem:

return midPointelif SearchItem < foundItem:

return BinSearch(SearchItem, OrderedList, lowIndex, midPoint - 1)else:

return BinSearch(SearchItem, OrderedList, midPoint + 1, highIndex)

Page 54: Python by Example - 9 June 2014

Python Programming by Example Page 54 of 82 Richard Riehle

Python Files

So far, we have been working with primary memory. Most computers also include secondary memory. The difference between primary and secondary memory is easy to understand. Primary memory is the memory directly accessible by the Central Processing Unit (CPU). Secondary memory must be transferred to primary memory before the CPU can access it. Examples of secondary memory include disk files, USB drives, CD-ROM, DVD media, tape drives, and, in an earlier era of computing, punched cards. For this discussion, we will focus on disk-based files.

A software container is a place where we store information. A file is a secondary memory container in which we store data that we want to keep for a long time. There are containers that vanish when we turn-off the computer. Some containers, those stored in primary memory, have been discussed earlier: lists, sets, dictionaries, strings, etc. It is often important to save those containers more permanently, and external (secondary) memory is the place to do that.

Note: File Management is a big topic. We do not intend to cover every possible variation of this topic. Rather, we will get you started with some simple techniques. You can later expand your knowledge once you have experimented with the examples in this section.

Pickle

By far, the easiest way to save and retrieve information in Python is with the pickle utility. Here is a pickle example using a simple list.

In the first line, we create a list called, names. Then we import pickle. On line three of this example, we use the dump option of pickle to store the list (names) in a file we called nameFile.txt. The initials, “wb”, tell us to write the file in binary format. If a file with the same name already exists, it is overwritten with this one. Otherwise, it is created automatically. To test that pickle has been successful, we load it back into our primary memory with a pickle.load function using the same name. This time, the file mode is “rb”, which instructs pickle to open the file for reading in a binary format.

Exercises:1. Try pickle on a Python dictionary2. Try pickle on a Python string

Pickle is useful for simple file storage and retrieval, but not for more advance applications. In fact, pickle is not always a safe Python feature, especially if you are working with files from some untrusted source. Use it for your own temporary files, but not for files you find elsewhere.

Secondary Memory Storage

>>> names = ["sam", "fred", "george", "mary", "alice", "harold", "norma"]>>> import pickle>>> pickle.dump( names, open( "nameFile.txt", "wb" ) )>>> inNames = pickle.load( open( "nameFile.txt", "rb" ) )>>> inNames['sam', 'fred', 'george', 'mary', 'alice', 'harold', 'norma']>>>

Page 55: Python by Example - 9 June 2014

Python Programming by Example Page 55 of 82 Richard Riehle

Simple IO Example

Consider the following code that works with Python 3.0 or higher.

In the first line, we define a list. Then we open a file with the internal name, theFile, and an external name. f.txt for writing. On the third line, we write the list to the file. On line four, we open the file for reading. The for loop gets one line at a time from the file (based on the \n character). Within the for loop, we print one line at a time from the file. Finally, after getting all the lines from the file, we close the file.

Note: \n is a character used to effect a new-line operation.

Exercises:1. Put the file reading loop in a parameterized function2. Return the items you read to another list instead of simply printing them3. Experiment with other kinds of Python structures: lists, strings, dictionaries, etc.

>>> name = ['w \n', 'g \n', 'q \n', 'k \n']>>> theFile = open(file = 'f.txt', mode = 'w')>>> theFile.writelines(name)>>> theFile = open('f.txt', 'r')>>> for line in theFile:

print(line, end='')

w g q k >>>theFile.close()

Page 56: Python by Example - 9 June 2014

Python Programming by Example Page 56 of 82 Richard Riehle

Python Modules

One of the handy features of Python is the ability to collect a set of common functions in a module. Some modules are available with any standard implementation of Python. You may also create your own module. A module can be included in your program using the import command. Consider the following examples from the Python math module.

As you can see, there are three ways to import a module. You need to be careful using the asterisk option when importing multiple modules, especially when there are functions with similar (or nearly identical) names in each module. Modules are a great way for you to collect related functions for use in your own applications. A module can contain functions, abstract data types, or OOP classes.

Exercises:1. Experiment with other standard Python modules such as os,

system, and tkinter.2. Create modules of you own with a set of functions and

experiment with them

Page 57: Python by Example - 9 June 2014

Python Programming by Example Page 57 of 82 Richard Riehle

A Bubble Sort

One of the most frequently used algorithms in programming is that of sorting data. There are many different sort algorithms. Here is an example of a bubble-sort.

def bubblesort(theList):“”"Sorts list in place and returns it."”” ## a Python docstringfor passesLeft in range(len(theList)-1, 0, -1):

for index in range(passesLeft):if theList[index] < theList[index + 1]:

theList[index], theList[index + 1] = theList[index + 1], theList[index]

return theList

This is one of many kinds of sorting algorithms used in computing. As you progress in your study of computer science, you will discover many more variations on sorting. We recommend you create an unordered list of numbers and pass it to the example bubblesort shown above. Then, following the algorithm by hand, replicating on a sheet of paper the algorithmic behavior so you can see what is actually happening in the algorithm.

Every sort algorithm has its own special properties. One of the most important of those properties is something we call “Big O’ notation. This is a notation used for the relative speed of an algorithm. We also recommend you do an Internet search for that term and get to know how it affects your choice of sorting algorithms, as well as other algorithms you may choose.

Most well-known sorting algorithms (probably all of them at this writing) have already been coded in Python, and are available in a variety of modules. These include (but not limited to):

Insertion sort Merge sort Quicksort (an early example of a good use of recursion)

Bubble sort

each of which has its own virtues and drawbacks with regard to efficiency. Donald Knuth, one of the foremost experts on algorithms of all time, says that much of what we do with non-mathematical algorithms involves sorting and searching. We have tried to include examples of both in this book.

Page 58: Python by Example - 9 June 2014

Python Programming by Example Page 58 of 82 Richard Riehle

Abstract Data Type (ADT)

Data abstraction is a fundamental concept in modern software development. An abstract data type is a data type not present in the underlying programming language. It is a programmer-designed type, a data type invented by the programmer for convenience and safety. Consider the following example:

class COUNTER():## Note the default values for all arguments in this ADTdef __init__(self, ## local data items are part of self

initial=0, ## initial value argument step=1, ## integer indicating step incrementminValue = -2**31,## smallest allowed countermaxValue = 2**31):## largest value for counter

self.mn = minValue ## for smallest value assertself.mx = maxValue ## for largest value assertassert initial >= self.mn and initial <= self.mxself.data = initial ## save initial value argument

def increment(self):self.data += stepassert self.data >= self.mn and self.data <= self.mxreturn self.data

def decrement(self):self.data -= stepassert self.data >= self.mn and self.data <= self.mxreturn self.data

def reset():pass ## set the counter to its intial value

def clear():pass ## set the counter to zero

def currentValue():pass ## return, non-destructively, value of counter

The ADT is a valuable tool for the experienced software developer. The novice will take a little longer to realize its value and be able to use it effectively.

In the next section, we examine a special kind of ADT, the OOP class. In software parlance, especially in languages such as Python, a class is usually an extensible ADT. By extensible, we mean that we can use the existing properties of the type (see properties of a type as explained at the beginning of this book), and add new properties, including new a new set of values and a new set of behaviors. We sometimes say that we designer a specialization of the base type. This specialization capability is a key property of the class in Object-oriented Programming.

Page 59: Python by Example - 9 June 2014

Python Programming by Example Page 59 of 82 Richard Riehle

Object-Oriented Programming

In the early 1970’s, Alan Kay, working from contributions of earlier pioneers such as Doug Englebart (inventor of the mouse), Ole-Johan Dahl and Kristen Nygaard (inventors of Simula), and many others, developed a style of programming he called object-oriented programming (OOP). The language he designed was called Smalltalk. It is based on the concept of a class, a special kind of datatype, a dynamically extensible abstract data type (ADT), that can be extended and specialized (sometimes called inheritance), and which wraps the data and operations on the data into a single entity. In the class, the enclosed data is said to be encapsulated since the only access to that data is via the public operations. Many newer languages, including Python, C++, Java, Ada, Eiffel, and Ruby support the concept of a class, along with many other object-oriented programming capabilities. Even Fortran and COBOL have recently evolved to support this concept of class and the corresponding capabilities of Object-oriented Programming.

The following data-structure, a stack, is a simple example of a class using Python. In modern computing, elementary data-structures, including trees, lists, queues, maps, etc., are commonly designed using a class.

The word class is used in place of the word type. Even so, a class is simply a special kind of type. It includes all the properties we listed for a type, but has some additional properties.

The word class was chosen to signify that each class is part of a larger classification scheme that supports both generalization and specialization of that class. Generalization is how we raise the level of abstraction for a class. Specialization is sometimes called inheritance.

Python is a language that supports object-oriented programming. Other languages include (not a complete list): C++, Java, Ada, C#, Ruby, Perl, Scala, and Eiffel.

Page 60: Python by Example - 9 June 2014

Python Programming by Example Page 60 of 82 Richard Riehle

A Stack Class Example

The earlier stack example was an Abstract Data Type (ADT). It did support generalization or specialization. The following example defines a class for Stack that is a specialization of a parent class named “object”.

class Stack(object): ## extend Python object to create class, Stackdef __init__(self): ## define constructor method

self.stack = [ ] ## implement constructor methodself.count = 0 ## initialize the count of the stack

def push(self, object): ## define a stack modifier named Pushself.count += 1 ## increment the count of the stackself.stack.append(object) ## implement the Push operation

def pop(self): ## define a stack modifier named Popself.count -= 1 ## decrement the count of the stackreturn self.stack.pop() ## implement the Pop operation

def length (self): ## define a stack query given length of stackreturn (self.count) ## report on the length of the stack

>>>the_stack = Stack() ## create an instance of the stack

Page 61: Python by Example - 9 June 2014

Python Programming by Example Page 61 of 82 Richard Riehle

A Bounded Stack Class

This example shows how we define a container (in this case, a stack) that has an upper bound. We declare that upper bound at the time we declare an instance of the class. In this example, we include a default size of 12. That default can be change when any other instance is declared.

class Bounded_Stack (object):def __init__(self, maximum_size = 12):

self.stack = [ ]self.index = -1self.max_size = maximum_size - 1self.reversedStack = self.stack

def incrementIndex(self):self.index = self.index + 1

def decrementIndex(self):self.index = self.index - 1

def displayStack(self):self.reversedStack = self.stackself.reversedStack.reverse()for i in self.reversedStack:

print(i)def displayTop(self):

print(self.stack[self.index])def push(self,value):

if self.index >= self.max_size:return ("Stack Overflow!" + str(value) + " not pushed")

else:self.stack.append(value)self.incrementIndex()

def pop(self):if self.index >= 0:

localvalue = self.stack[self.index]self.decrementIndex()return localvalue

else:return (str(self.index) + " is out of valid index range. ")

def stackSize(self):return (self.index + 1)

Note an important property of a class (and an ADT) is that the class knows about itself. We sometimes say an instance of a class (a class object) is self-aware. Python even uses the word,self, to make clear its self-awareness. In this case, the word “self” allows the class to reference any of its own properties prefixed with that word.

Page 62: Python by Example - 9 June 2014

Python Programming by Example Page 62 of 82 Richard Riehle

An Account Class Example

class Account(object): ## extends Python class, objectnum_accounts = 0;def __init__(self, name, balance):

"Initialize a new account Instance"self.name = name;self.balance = balance;Account.num_accounts += 1;

def __del__(self):"Reference counting method"Account.num_accounts -= 1;

def sufficient_funds (self, amount):if self.balance > amount:

return Trueelse:

return Falsedef deposit (self, amount):

self.balance = self.balance + amount;def withdraw (self, amount):

if sufficient_funds(amount):self.balance = self.balance - amount;

else:print "Insufficient Funds for this Transaction";

def inquiry(self):return self.balance;

“Save this to a file”

>>> from Account import *

>>>Sam = Account(“Sam”, 0)

>>>print Sam.Name

Sam

>>>print Sam.Balance

0

>>>

Page 63: Python by Example - 9 June 2014

Python Programming by Example Page 63 of 82 Richard Riehle

Windows Applications (tkinter)

Windows applications are easy to create with Python. There are several libraries available for doing this kind of GUI programming. One of the simplest is called tkinter. Be careful of the spelling. Older versions of Python capitalized the initial ‘t’ as ‘T’ for Tkinter. If your program fails, especially when using existing code from Internet, be sure to correct the spelling so it will work as expected.

It is important for anyone starting to write programs for windows applications, where using Microsoft Windows, MacIntosh, or some other operating system, to know the difference between console-based applications and those using the windows and mouse metaphor. In console applications, the programmer gets a prompt and enters commands that are then executed by the system. This model extends even to the input from files and other sources. That is, the program receives information (data) and then operates on that data. Windows programming is a little different.

A windows program is event-driven. The most common event is characterized by the mouse-click on a hot-spot on one of the windows currently active on the video-display terminal. The operating system detects that mouse-click, and then sends notification about it to the program for the active window. That program includes an event-loop that is waiting for such notifications. When it receives the notification, it determines whether it has some operations that correspond to the event, and then takes action based on that code. Often, the code is in the form of a call-back. The call-back is a message to the operating system with instructions of what to do next. This event/call-back model is unique to windows-style programming, and understanding that uniqueness is fundamental to your being successful in writing effective windows programs. In Python, you will often see references to “mainloop” in the code. When you learn how to use the mainloop approach, you will be well on your way to being competent at windows programming. By the way, if you do not have a mainloop, your program may execute and end before you have a chance to take any kind of action.

simple frame example

from tkinter import *root = Tk()root.title('Lazy Button')app = Frame(root)app.grid()root.mainloop()

Notice that this example does nothing more than create a frame. It uses the library named tkinter. Then, it creates a root window using the feature of tkinter. Then we create an application (abbreviated, app) with a Frame. Don’t worry too much about the grid right now. You can enter this and execute it. The window is not quite big enough to display everything. Use your mouse to make it larger. This window is completely empty except for the basic items in the Frame. Close the window using the X in the upper-right corner (Microsoft).

Sometimes your windows program will behave in seemingly abnormal ways. This is usually because of your use of some parameter or command that is counter-intuitive. Don’t be discouraged. You may have to experiment with some of your programs to get little things such as spacing, window size, colors, widget positioning, and more just the way you want them.

Page 64: Python by Example - 9 June 2014

Python Programming by Example Page 64 of 82 Richard Riehle

Finally, be careful of examples you find on-line. Some of them are for earlier versions of Python. Some do not work at all. Others require additional libraries to achieve their effect. simple message that requires no response

from tkinter import *root = Tk()root.title('Message')Message(root, text=" .........."

"................... ""This is some text for a message without a response "

"................................................................."

"................................................................."

".................................................", bg='royalblue',

fg='ivory', relief=GROOVE).pack(padx=80, pady=80)root.mainloop()

You should see a screen that looks like this:

Page 65: Python by Example - 9 June 2014

Python Programming by Example Page 65 of 82 Richard Riehle

Another message that requires no response

from tkinter import *msg = Message(text="Text Text Text Text Text?")msg.config(bg='pink', font=('times', 16, 'italic'))msg.pack()mainloop()

Now your screen will be a simple frame containing the absurd message, “Text” written many times over and over. You can easily experiment with this example and make it more interesting.

Here is a little program that you can have fun with as you experiment with Python windows programming.

from tkinter import *import os, sysmaster = Tk()def callback():

print ("Ran OK Routine")def QUIT():

sys.exit("normal end of program")b1 = Button(master, text="OK", command=callback)b1.pack()b2 = Button(master, text="QUIT", command=QUIT)b2.pack()mainloop()

There is one little problem with this program in some Python installs under version3.0 and higher. The sys.exit() command does not always work properly. You may have to manually close the window when the program exits. This is something of a nuisance, and we hope it will be fixed in later version of the Python releases.

Page 66: Python by Example - 9 June 2014

Python Programming by Example Page 66 of 82 Richard Riehle

A GUI application using a class definition

from tkinter import *

class Application(Frame):def say_hi(self):

print ("hi there, everyone!")

def createWidgets(self):self.QUIT = Button(self)self.QUIT["text"] = "QUIT"self.QUIT["fg"] = "red"self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)self.hi_there["text"] = "Hello",self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):Frame.__init__(self, master)self.pack()self.createWidgets()

root = Tk()app = Application(master=root)app.mainloop()root.destroy()

Another GUI Example

root = tk.Tk()# use width x height + x_offset + y_offset (no spaces!)root.geometry("%dx%d+%d+%d" % (300, 200, 100, 50))root.title('test the ttk.Notebook')

nb = ttk.Notebook(root)nb.pack(fill='both', expand='yes')

# create a child frame for each pagechild_1 = tk.Frame(bg='red')child_2 = tk.Frame(bg='blue')child_3 = tk.Frame(bg='green')

# create the pagesnb.add(child_1, text='Red Page')nb.add(child_2, text='Blue Page')nb.add(child_3, text='Green Page')

# put a button widget on child_1

Page 67: Python by Example - 9 June 2014

Python Programming by Example Page 67 of 82 Richard Riehle

btn1 = tk.Button(child_1, text='Red Button')btn1.pack(side='left', anchor='nw', padx=3, pady=5)

# put a button widget on child_2btn2 = tk.Button(child_2, text='Blue Button')btn2.pack(side='right', anchor='nw', padx=3, pady=5)

# put a button widget on child_3btn2 = tk.Button(child_3, text='Green Button')btn2.pack(side='right', anchor='nw', padx=3, pady=5)

root.mainloop()

A simple Message Box Dialogue; Python 3.1 or higher

import tkinterimport tkinter.messagebox as tkMessageBox ## Python 3.1top = tkinter.Tk()def hello():

tkMessageBox.showinfo("Say Hello", "Hello World")B1 = tkinter.Button(top, text = "Say Hello", command = hello)B1.pack()top.mainloop()

Another Message Box Dialogue

import tkinterimport tkinter.messagebox as tkMessageBox top = tkinter.Tk()def message():

tkMessageBox.askyesno(title = "lunch time", message = "Do you want to have lunch?")

## In addition to askyesno, you could have the following messages:## showinfo(), showwarning(), showerror(), askquestion(),## askocancel(), askretrycancel()## Experiment with these other options

B1 = tkinter.Button(top, text = " Raise a question. ", command = message)B1.pack()top.mainloop()

Page 68: Python by Example - 9 June 2014

Python Programming by Example Page 68 of 82 Richard Riehle

More sample Programs

Sample Programs - 1

#============================================# Module Name: Statistics.py# Author: John Jacob Jingleheimer Schmit# Creation Date:# Last Update:# ===========================================

def theSum(theList):result = 0for value in theList:

result = result + valuereturn result

def buildList():theList = []temp = 0.0while True:

inputData = input("Enter a number: ")# if inputData.isalphaif inputData.isalpha():

breakelse:

temp = float(inputData)print (temp)theList.append(temp)print (theList)

return theList

def theMean(theList):Len = len(theList)Sum = theSum(theList)return Sum / Len

## implement other functions such as min, max, standard deviation, variance

This class is designed so you can add more functions such as those listed in red, above. If you would rather experiment with inheritance, you can simply create an extension of the class

Page 69: Python by Example - 9 June 2014

Python Programming by Example Page 69 of 82 Richard Riehle

Sample Programs – 2

## ===================== listbox_2.pyw ==========================## Title: listbox_2.pyw## Author: R.Riehle## Date: 14 April 2011#### This program creates a simple, but useful Listbox in Python## using tkinter. #### Designed to work with Python 3.0 and higher using older code from## earlier versions of Python (prior to 3.0)## ================================================================

import tkinter as tk # gives tk namespaceroot = tk.Tk()root.title("Listbox Operations")

def add_item():listbox1.insert(tk.END, enter1.get())

def delete_item():try:

# get selected line indexindex = listbox1.curselection()[0]listbox1.delete(index)

except IndexError:pass

def get_item():# get selected line indexindex = listbox1.curselection()[0]seltext = listbox1.get(index)tk.messagebox.showinfo(title='Selected Item', message='You selected ' + seltext)return listbox1.curselection()[0]

def get_list(event):# get selected line indexindex = listbox1.curselection()[0]# get the line's textseltext = listbox1.get(index)# delete previous text in enter1enter1.delete(0, 50)# now display the selected textenter1.insert(0, seltext)

def set_list(event):try:

Page 70: Python by Example - 9 June 2014

Python Programming by Example Page 70 of 82 Richard Riehle

index = listbox1.curselection()[0]# delete old listbox linelistbox1.delete(index)

except IndexError:index = tk.END

# insert edited item back into listbox1 at indexlistbox1.insert(index, enter1.get())

def sort_list():temp_list = list(listbox1.get(0, tk.END))temp_list.sort(key=str.lower)# delete contents of present listboxlistbox1.delete(0, tk.END)# load listbox with sorted datafor item in temp_list:

listbox1.insert(tk.END, item)

def save_list():# get a list of listbox linestemp_list = list(listbox1.get(0, tk.END))# add a trailing newline char to each linetemp_list = [chem + '\n' for chem in temp_list]# give the file a different namefout = open("chem_data2.txt", "w")fout.writelines(temp_list)fout.close()

# create the sample data filestr1 = """chateau Lafite ## Note the use of triple quotes for line breaksBeringerMondaviGrgich-HillChateau BeychevelleChateau Neuf de PapeChampaignebourbonrumschnaps"""fout = open("chem_data.txt", "w")fout.write(str1)fout.close()

# read the data file into a listfin = open("chem_data.txt", "r")chem_list = fin.readlines()fin.close()# strip the trailing newline charchem_list = [chem.rstrip() for chem in chem_list]

#root = tk.Tk()#root.title("Listbox Operations")# create the listbox (note that size is in characters)listbox1 = tk.Listbox(root, width=70, height=20)listbox1.grid(row=0, column=0)

Page 71: Python by Example - 9 June 2014

Python Programming by Example Page 71 of 82 Richard Riehle

# create a vertical scrollbar to the right of the listboxyscroll = tk.Scrollbar(command=listbox1.yview, orient=tk.VERTICAL)yscroll.grid(row=0, column=1, sticky=tk.N+tk.S)listbox1.configure(yscrollcommand=yscroll.set)

# use entry widget to display/edit selectionenter1 = tk.Entry(root, width=50, bg='yellow')enter1.insert(0, 'Click on an item in the listbox')enter1.grid(row=1, column=0)# pressing the return key will update edited lineenter1.bind('<Return>', set_list)# or double click left mouse button to update lineenter1.bind('<Double-1>', set_list)

# button to sort listboxbutton1 = tk.Button(root, text='Sort the listbox ', command=sort_list)button1.grid(row=2, column=0, sticky=tk.W)

# button to save the listbox's data lines to a filebutton2 = tk.Button(root, text='Save lines to file', command=save_list)button2.grid(row=3, column=0, sticky=tk.W)

# button to delete a line from listboxbutton5 = tk.Button(root, text='Get selected line ', command=get_item)button5.grid(row=4, column=0, sticky=tk.E)

# button to add a line to the listboxbutton3 = tk.Button(root, text='Add entry text to listbox', command=add_item)button3.grid(row=2, column=0, sticky=tk.E)

# button to delete a line from listboxbutton4 = tk.Button(root, text='Delete selected line ', command=delete_item)button4.grid(row=3, column=0, sticky=tk.E)

# button to delete a line from listboxbutton6 = tk.Button(root, text='Quit the Listbox ', command=get_item)button6.grid(row=5, column=0, sticky=tk.E)

# load the listbox with datafor item in chem_list:

listbox1.insert(tk.END, item)

# left mouse click on a list item to display selectionlistbox1.bind('<ButtonRelease-1>', get_list)

root.mainloop()

Page 72: Python by Example - 9 June 2014

Python Programming by Example Page 72 of 82 Richard Riehle

Documentation in Python

Python programs are usually collected in a module. A module is a file with a .py (sometimes .pyw) extension. It is accessed using the import command. For each module, it is appropriate to have header information to document the author, title, date, time, and other information for the module. You should also include a revision history for the module as well as a revision history for each program within the module. Here is one example of what this might look like.

## ====================== Module Documentation =========================## Module Title: gtsthssl.py## Author: Wee Willie Winkle## Orignal Date: 6 June 1896## Purpose:## This module is designed to ...## ...###### ================== End Module Documentation ========================#### ================== Revision History =========================## Revised by: Abhinav Jones## Revision Date: 12 June 2010## Revision Purpose:#### ...## ================= End Revision History =====================#### ================== Programs Start Here =====================#### ====================== Program One Documentation =========================## Program/Function Title: xxxxxxxxxxxxxxx## Author: Beanstalk Jack## Orignal Date: 6 June 1900## Purpose:## This program is designed to ...## ...###### ================== End Program One Documentation ========================#### ================== Program One Revision History =========================## Revised by: Tornilke Shakashvili#### ...## ================= End Program One Revision History =====================

If you are a student in a course where you are studying Python, your instructor may require that every programming assignment has this kind of documentation. In a production environment, you will certainly be required to include this information for each module and program.

Page 73: Python by Example - 9 June 2014

Python Programming by Example Page 73 of 82 Richard Riehle

Software Engineering with Python

In this section, we discuss how Python supports some of the more important concepts of software engineering: reliability, reuse, adaptability, design by contract, constraints, exception handling, type-safety, and other capabilities usually associated with type-based languages.

Introduction

Python is not a language such as Ada, C, C++, Eiffel, or Java where the programmer is required to identify the data types before they are used. For that reason, many designers regard it as less safe than those kind of languages. This does not mean, however, that Python is “weakly” typed. On the contrary, many of the constructs in Python are strict once a data type has been associated with a variable. Python typing is implicit in the way data is defined. To see examples of this, revisit the rules for strings, dictionary, set, and list types discussed earlier in this book.

In addition, Python includes some unique features which, if used intelligently, will provide powerful software engineering capabilities that rival those of the languages commonly considered more engineering-oriented such as Eiffel, Ada, and C++ . However, no one should be seduced into thinking that Python is an appropriate language for safety-critical software.

We have already seen one of the more important features using the import statement. Borrowing from the Ada language, Python distinguishes between the scope of a module and the visibility of the elements in that module. The import statement puts all the elements of a module in-scope, but makes none of them visible. This is an important distinction. The requirement to make each element of a module explicitly visible prevents name clashes and also provides good documentation for whoever will read a given piece of source code later. Visibility of each element is achieved by dot-notation or a universal visibility clause with an asterisk.

While the classical software engineering principle of information-hiding is not commonly used in Python programs, it is possible to make this a part of a good Python design. At this level of the student’s Python education, we leave the capabilities for information-hiding and encapsulation toa later edition of this book; or we leave it as an exercise for the student.

The benefits of reuse through the abstract data type (ADT) and the Python [inheritable] class mechanism have also been introduced. However, inheritance is only one kind of reuse, and we will show how Python supports other kinds of reuse such as a close mode of generic, type independent, modules such as those one might find in Java, Ada, Eiffel, or C++.

Python also provides assertion support for pre-conditions, post-conditions, and invariants. This support can be enabled either directly or through a contract model based on the Python decorator. In fact, once the software engineer in a Python environment understands the full range of capabilities for Python decorators, s/he will be well on the way to creating Python applications that are as well-engineered as in almost any other language.

Page 74: Python by Example - 9 June 2014

Python Programming by Example Page 74 of 82 Richard Riehle

Assertions

We have already introduced this topic earlier in an elementary form. Assertions are of three general kinds:

1. Pre-conditions Required before an operation may begin2. Post-conditions Ensures that the algorithm has completed correctly3. Invariants A set of or range of states that constrains types or variables

These three elements were originally introduced by C.A.R. Hoare and Edsger Dijkstra. They were later refined into a general concept called Design By Contract (DbC) by Bertrand Meyer. Dr. Meyer incorporated them into the Eiffel language. Eiffel probably represents the best reasoning of DbC for object-oriented programming,, and Python cannot achieve the level of sophistication found in Eiffel. However, with an understanding DbC we can use some of the Python features to benefit from that understanding.

Bertrand Meyer, designer of Eiffel, provides us with some useful synonyms for the common assertions:

Requires ynonym for pre-condition Ensure synonym for post-condition Invariant same name; no synonym

Using these synonyms in your own code will make it more readable. Examples might be (best coded in the form of decorator assertions):

require_all_parameters_greater_than_zero require_container_not_full require_threshold_value (where threshold value is defined elsewhere) ensure_return_result_not_zero ensure_no_empty_container ensure_ending_x_greaterthan_beginning_x invariant_x_GTE_zero_LTE_2000 ## GTE greater than or equal; LTE less than invariant_threshold_value_GTE_200 invariant_never_zero

These kinds of assertions, and many others, can go a long way toward improving the quality of your completed programs.

Page 75: Python by Example - 9 June 2014

Python Programming by Example Page 75 of 82 Richard Riehle

Assertion Examples:

>>> def DbC1(a): ## a precondition and invariant on parameter named aassert a > 0 and a < 120, "Parameter out of range"print (a)

Calling this function with a value out-of-range will produce a run-time AssertionError. In general, it is a bad idea to simply let the program fail once it is in the domain of the user. A better approach might be to use the assert in a try … except block.

>>> def DbC1(a):try:

assert a > 0 and a < 120, "Parameter out of range"except:

print("AssertionError")else:

print(a)

Consider the following example:

>>> def BuildIntegerList():theList = []inputValue = 0while inputValue < 999999: ## arbitrary maximum value for an element

try:inputValue = int(input("Enter an Integer; 999999 to Exit "))

except:print ("Bad input type")

else:if inputValue >= 999999:

return theListtheList.append(inputValue)

In the above examples, the assert statement is enclosed in the try-block. Although we have simply printed information about the error, when used in a system of functions (or in a class), the error result in a recovery process appropriate to the kind of error being detected.

Note: An exception handling routine is analogous to a circuit-breaker in an electrical system. When there is bad data, or a value falls outside an acceptable range, the exception handler takes over. No one would design the electrical system of a house without a circuit-breaker, The same should be true of a software system.

Page 76: Python by Example - 9 June 2014

Python Programming by Example Page 76 of 82 Richard Riehle

We can even make sure that all the elements of a collection type are of the same type using the assertion mechanism. If we want to make sure all the elements are integers we might code:

>>> def testForInteger(data): ## can be used on a collection of elements or single elementanInteger = 0 ## or whatever type you want to testtheType = type(anInteger) ## create a class item for the type to be validatedtry:

d = theType(data) ## is the data of the valid type?except:

print ("data is not an integer")else:

print ("found an integer")

>>> testForInteger("45")found an integer

Note: It is possible to code the this with a fewer statements, but the code is more difficult to read and debug for the novice Python programmer

Page 77: Python by Example - 9 June 2014

Python Programming by Example Page 77 of 82 Richard Riehle

Decorators – For Python Power Programming

One of the more powerful, more sophisticated additions to Python is the decorator. It is a difficult concept for newcomers to Python, but it is a major step toward generalization of functionality and reuse of source code.

Note: Since their introduction into the Python language, decorators have been found to have a large number of uses. We cannot treat all of those uses in this book, nor do we need to. The Internet mavens continually post their newly found uses, some of which are remarkable in their cleverness and creativity.

In short, a decorator is a function than can modify another function with specialized behavior. For those who know about the “Decorator Patterns” from pattern-based development, we need to clarify that the decorator in Python is not the same thing. Rather, the Python decorator is closer in behavior to, but not identical to, a Delegation pattern. In the Python literature, this is sometimes called a “wrapped function”.

A decorator has particular value when creating a set of reusable assertions (pre-, post-, and invariant conditions) to support DbC. Here is a rather elaborate example of a post-condition, using the decorator in Python. We annotate it line-by-line to make it easier to follow. Recall from our discussion of functions, the example where an outer function can call the inner function. The wrapper function, in this example, is invoked by the outer function named ensurePositivePostCondition.

First, we define a generalized function that will become our decorator. This function takes as its own argument, a single (client) function we call targetFunction. We remind the reader that this is possible in Python because functions are first-class objects that can be passed as arguments to other functions.

We nest another function called “wrapper” inside the client function. The wrapper is designed to take an unspecified number of arguments and an unspecified number of key-word-arguments. This allows it to be reused as a decorator (and as a post-condition function) for a larger number of other targetFunctions.

An assert statement looks at the result of computation in the client we have called targetFunction in the formal parameter and ensures (the post-condition) that it is not negative; it is greater than or equal to zero. If the assertion fails, the program simply aborts with a long string of messages. Otherwise, the program will return the intermediate result for the wrapper function.

Note 1: Wrappers are not unique to Python. They are commonly used in many other programming languages. One of their uses has been to wrap code from one language with an interface of another. Python enables this capability too. But our examples of wrappers serve a different purpose.

Note 2: Do not confuse the term decorator as used in Python with that same term used in other languages or in the literature of software engineering pattern design.

Page 78: Python by Example - 9 June 2014

Python Programming by Example Page 78 of 82 Richard Riehle

Having taken the result of the targetFunction, the name and the doc parts, the main function, ensurePositivePostCondition(), allows the result of the client function to be returned as a valid value.

>>> def ensurePositivePostCondition(targetFunction):def wrapper(*args, **keyWordArgs):

result = targetFunction(*args, ## variable positional arguments**keyWordArgs) ## variable keyword arguments

assert result >= 0, targetFunction.__name__ + "() result is not >= 0"return result

wrapper.__name__ = targetFunction.__name__ ## assign target name to wrapperwrapper.__doc__ = targetFunction.__doc__ ## assign target docstrings to wrapperreturn wrapper ## wrapper-modified target

In the following code, we apply the decorator and its wrapper to a client function. The client function immediately follows the decorator and is specified using an @functionName. The example client, in this case, is a simple function named adder, which does nothing more than add two values. The decorator ensures the addition returns only a positive resulting value.

>>> @ensurePositivePostCondition ## an assertion decoratordef adder(a, b):

return a + b

>>> adder(12, 34)46>>> adder(-19, 5)Traceback (most recent call last):File "<pyshell#280>", line 1, in <module>adder(-19, 5)

File "<pyshell#276>", line 5, in wrapperassert result >= 0, targetFunction.__name__ + "() result is not >= 0"

AssertionError: adder() result is not >= 0>>>

To illustrate how this decorator is reusable, here is the same set-up for another function we call multiplier.

@ensurePositivePostConditiondef multiplier(a, b):

return a * b

Once again, if the post condition is negative, the program will fail. We could apply this decorator to any kind of function where a positive result would be necessary. Of course, we do not want programs to fail when deployed to the user, so the designer needs to add code to guarantee that the use can continue without trying to debug the code.

A better approach is to use an exception handler so the user of the program will not be getting obscure messages such as the one shown.

Page 79: Python by Example - 9 June 2014

Python Programming by Example Page 79 of 82 Richard Riehle

There is now a new module in Python called functools that makes using wrappers much easier, and also more intuitive. Here is an example of the previous decorator function with a nested wrapper using this new style of coding.

def ensurePositive(targetFunction):def wrapper(*args, **keyWordArgs):

result = targetFunction(*args, ## variable positional arguments**keyWordArgs) ## variable keyword arguments

assert result >= 0, targetFunction.__name__ + "() result is not >= 0"return result

return functools.update_wrapper(wrapper = wrapper,wrapped = targetFunction)

In the preceding example, the assignment of the wrapper name, documentation strings, and other information is automatically handled by the statement, functools.update_wrapper. Of course, you must import the module named functools for this to work.

Page 80: Python by Example - 9 June 2014

Python Programming by Example Page 80 of 82 Richard Riehle

Appendix A -- Fundamental Algorithm Structures

Each rectangle box represents a process or program step. A diamond represents a condition to be tested. An oval represents the scope terminator for the structure. Every structure should have a clear exit point designated by a scope terminator.

Python, unlike many other languages, uses indentation for scope designation. This is sometimes a problem for programmers accustomed to other languages such as Java, Ada, and C++ which are more explicit in the use of scope designation.

Sequence

Iteration (Test Before) Selection (If… else)

Iteration (Test After) Selection (multi-way)

Page 81: Python by Example - 9 June 2014

Python Programming by Example Page 81 of 82 Richard Riehle

There are five fundamental levels of data type in computer programming.

Primitive Pointer (indirection mechanism) User-defined Abstract Data Type Class

Not all languages support all of the above named types. Sometimes a type name is a reserved word; sometimes it is a more of a specialized name, but not reserved.

A primitive data type is typically a scalar such as an integer (int), float, character, byte, word, etc. The usual values and operations for a primitive data type are defined within the language being used. They may be spelled differently from one language to another. In C, C++, Java, and C#, an integer is abbreviated as “int’. In Ada, it is spelled out as “integer”.

The set of values for primitive data types can sometimes be further constrained by a language. For example, Ada allows one to limit the range of an integer with a new name with several kinds of syntax:

type Channel is new Integer range 2..132; -- Derived from Integer; can be castedtype Channel is range 2..132; -- A new kind of Integer; its own typesubtype Channel is Integer range 2..132; -- Can be used with Integer easily

Each of the above definitions has subtle and significant differences that the Ada programmer can use to good effect. The first two are distinct data types. The subtype can be used with its parent type without type conversion (type casting).

A primitive type will often correspond closely to the intrinsic data types for a given machine. However, sometimes a type might be a software type rather than a machine type. An example would be a machine which does not directly support floating-point, but for which there is a software version of floating point in the programming language.

A pointer type is often a primitive type, but not always. In C and C++, the programmer can perform arithmetic operations on pointers. Computation capability on pointers provides great

Reminder: Data type

A data type, in computer programming, is characterized by four properties, which may vary a bit from one programming language/environment to another. These are:

1. The name of the data type2. The set of values (or states) allowed for instances of that type3. The set of behaviors (operations /methods/functions/etc) allowed for that type4. The set of rules for instances of a type with instances of other types

Python has a rich set of data types, some of which are quite unique, when compared to earlier languages. For example, Python collection types give the programmer easy power often not found in more traditional languages.

Page 82: Python by Example - 9 June 2014

Python Programming by Example Page 82 of 82 Richard Riehle

power, but it can also be fraught with danger. Other languages such as Java, allow for indirection through references, but do not allow primitive pointers with arithmetic properties.

The Ada language supports indirection through a special type: access type. By default, there is no computational capability on access types. However, the language does provide a specialized library that enables safe computation on some access types.

Most functional languages (e.g., Lisp, Scheme, Haskell) are built on top of the indirection capabilities that are inherent in modern computer architectures. The programmer need not be aware of how the indirection mechanism actually works. In addition, some interactive languages such as Perl, Python, and Ruby also use a reference model for every data item in the language.

User-defined types are not possible in every language. However, most modern languages provide some capability for this. The student needs to be aware that mechanisms such as typedef do not define a new data type. They simply provide an alias for an existing type.

Data abstraction is an important idea in modern software development. This idea reaches its ideal in the concept of an Abstract Data Type (ADT). An ADT is a user-defined type where the user defines (at minimum) the first three properties of the type (see, properties of a data type, above). In some cases, the user can also define some of the set of rules from the fourth property.

An ADT is typically not extensible. That is, you cannot apply inheritance and specialization to it. Some languages, such as Java, provide a keyword for ensuring that the ADT (under the name, class) cannot be extended, inherited, or specialized. An extensible type is often, but not always, called a “class”. The word, class, is a shortened form of the word “classification” which implies that a class-based design allows for a classification structure for specializations of the class.