Python Scripting Language 1.3 PDF
Python Scripting Language 1.3 PDF
Collection Editor:
Ken Ninh
Python Scripting Language
Collection Editor:
Ken Ninh
Author:
R.G. (Dick) Baldwin
Online:
< http://cnx.org/content/col11986/1.1/ >
OpenStax-CNX
This selection and arrangement of content as a collection is copyrighted by Ken Ninh. It is licensed under the Creative
Commons Attribution License 4.0 (http://creativecommons.org/licenses/by/4.0/).
Collection structure revised: April 1, 2016
PDF generated: June 23, 2017
For copyright and attribution information for the modules contained in this collection, see p. 312.
Table of Contents
1 Itse1359-1000-Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Itse1359-1010-Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Itse1359-1015-Program Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . 15
4 Itse1359-1020r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5 Itse1359-1030r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6 Itse1359-1050-Introduction to Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
7 Itse1359-1060-Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
8 Itse1359-1065-Visualizing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
9 Itse1359-1080r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
10 Itse1359-1090-Lists Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
11 Itse1359-1110-Nested Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
12 Itse1359-1110r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
13 Itse1359-1120-Empty and Single-Item Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14 Itse1359-1120r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
15 Itse1359-1130-Unpacking Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
16 Itse1359-1130r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
17 Itse1359-1210r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
18 Itse1359-1220-Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
19 Itse1359-1220r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
20 Itse1359-1230r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
21 Itse1359-1240r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
22 Itse1359-1250-Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
23 Itse1359-1250r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
24 Itse1359-1260-Loop Modiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 177
25 Itse1359-1280-Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
26 Itse1359-1410-Overview of Python classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 207
27 Itse1359-1410r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
28 Itse1359-1450-Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
29 Itse1359-1610-GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
30 Itse1359-1720-Doctest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
31 Itse1359-1720r-Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
32 Itse1359-1900-Preface to Networking and Databases . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 287
33 Itse1359-1930-Dbm and Shelve Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . 291
34 Itse1359-2110-Preface to Putting Python to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
35 Itse1359-2510-Getting Started with the Online Python Tutor Code Vi-
sualizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
iv
Itse1359-1000-Preface 1
1.2 Welcome
Welcome to the course material for ITSE 1359 Introduction to Scripting Languages: Python at Austin
Community College in Austin, TX.
(Note to blind and visually impaired students: most of the material for this course is presented in plain
text format and should be accessible using an audio screen reader or a braille display. While there are a few
bitmapped images that aren't accessible, that material will not be needed for assignments or included on
tests.)
Introduction to scripting languages including basic data types, control structures, regular expres-
sions, input/output, and textual analysis.
1
2 CHAPTER 1. ITSE1359-1000-PREFACE
1.3.2 Prerequisites
One semester of programming or departmental approval.
1. Language Fundamentals - Demonstrate the basic techniques used to create scripts for automating
system administrative tasks.
2. Testing - Design, code, and test Python applications using Python doctests and unit testing tools.
3. Text processing - Demonstrate the use of string operations and regular expressions in processing text.
4. Networking and Databases - Demonstrate the use of Python in developing applications using networking
and databases.
2 http://cnx.org/
3 http://interactivepython.org/courselib/static/thinkcspy/toc.html#t-o-c
4 http://openbookproject.net/thinkcs/python/english3e/
5 http://www.skulpt.org/
6 http://pythontutor.com/
7 http://www.learnpython.org/
8 http://www.codecademy.com/en/tracks/python
9 http://programarcadegames.com/
10 http://www.tutorialspoint.com/python/
11 https://docs.python.org/3/tutorial/index.html
12 http://www.sthurlow.com/python/
13 http://en.wikibooks.org/wiki/A_Beginner's_Python_Tutorial
14 http://www.diveintopython.net/toc/index.html
15 https://docs.python.org/3/c-api/index.html#c-api-index
16 https://docs.python.org/3/reference/index.html
17 https://docs.python.org/3/library/index.html#library-index
18 http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/
19 http://www.learnpython.org/
20 http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3
21 http://www.python-course.eu/python3_course.php
22 http://docs.python-guide.org/en/latest/writing/tests/
23 http://www.tkdocs.com/index.html
24 http://ebot.org/tkinterbook/tkinter-hello-tkinter.htm
25 http://ebot.org/tkinterbook/grid.htm
26 http://www.python-course.eu/index.php
27 http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
28 http://inventwithpython.com/bookshelf/
29 http://scipy-lectures.github.io/index.html
30 https://docs.python.org/3/library/turtle.html?highlight=turtle#module-turtle
The rst website listed above (p. 5) provides an interactive online Python tutorial that will help you learn
the fundamentals of Python programming and test your progress along the way.
The second website listed above (p. 5) will not only help you learn about Python programming, but
may also provide you with some enjoyment along the way. This website provides both text and video
instruction, along with quizzes and programming projects to teach you how to use the Python language to
write interactive arcade games involving sound, graphics, etc. While much of that material is beyond the
scope of this course, you may nd that learning that material will make the course more enjoyable.
The third website listed above (p. 5) is an excellent free online interactive Python textbook designed
specically for use in courses commonly referred by computer science academics as CS-1 courses. The
non-interactive version of the book is located here 45 .
1.8 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1000-Preface
• File: Itse1359-1000.htm
• Published: 10/13/2014
• Revised: 01/21/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
41 http://cnx.org/contents/228ac3a7-ed79-4a10-8d52-890dfa1fd20d
42 http://www.codecademy.com/en/tracks/python
43 http://programarcadegames.com/
44 http://interactivepython.org/courselib/static/thinkcspy/toc.html#t-o-c
45 http://openbookproject.net/thinkcs/python/english3e/
-end-
Itse1359-1010-Getting Started 1
2.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
2.2.1.1 Beginners
The ocial prerequisite for this course is "One semester of programming or departmental approval." How-
ever, if you don't know anything about programming, these modules will help you to overcome that deciency
by taking you from ground zero to object-oriented computer programming using Python.
2.2.1.2 Programmers
If you already know how to program in some other language, these modules will teach you how to program
using the Python scripting language.
2.2.4.1 Figures
• Figure 1 (p. 11) . Python 3.4.1 Shell
• Figure 2 (p. 12) . Python (command line) window.
• Figure 3 (p. 13) . Python Hello World.
What is Python ? Python is an easy to learn, powerful programming language. It has ecient
high-level data structures and a simple but eective approach to object-oriented programming.
Python's elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal
language for scripting and rapid application development in many areas on most platforms.
Note: If you are working in an ACC computer lab on the Northridge campus, you don't need
to download and install the Python software. It has already been installed. If you are working
in an ACC computer lab on a dierent campus and you don't nd the Python software, ask the
lab manager to download and install it. If you are working on your computer at home, at some
point you will need to download and install the Python software.
4 https://docs.python.org/3/library/index.html
5 http://www.python.org/
Note: In addition to IDLE, many Python development environments are available on the web -
some free and some not free. My personal favorite is the free Wing IDE 7 . I have asked that it
also be installed in the computer labs on the Northridge campus.
2.4.2.3 Let's do it
Type the following text to the right of the Python prompt and press the Enter key:
print("Hello World")
The print function : Prior to the release of version 3.0, print was a statement instead of a
function and was executed without the parentheses.
If all goes well, your interactive Python screen should then look something like Figure 3 (p. 13) .
Figure 3 . Python Hello World.
Pay particular attention to the line that reads Hello World following your entry. That is the output
from your program.
2.4.2.4 Congratulations
You have just written (and executed) your rst (interactive) Python program, and possibly your rst
computer program as well. Not only that, you only had to type one line of code to write and execute your
program.
Note that your entire program, the output from your program, and a new prompt are all shown in Figure
3 (p. 13) .
2.5 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1010-Getting Started
• File: Itse1359-1010.htm
• Published: 10/13/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1015-Program Organization 1
3.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
You are probably reading this document online at cnx.org 2 , otherwise known as openstax cnx . As
of October 2014, two views of the document are available: an openstax view and a legacy view. In the
legacy view, the document is commonly referred to as a module , which is contained in a collection of
modules. I mention this here to distinguish the word module from the usage of the same word that you
will nd below (p. 15) .
This document provides a brief introduction to program organization using scripts, modules, and pack-
ages.
3.3 Discussion
Although you won't need this information initially, once your Python programs become larger than a few
lines of code, you may want to organize them into scripts , modules , and packages . There is
no shortage of online material on that topic. Here is a list of online resources that explain how to organize
your programs:
15
16 CHAPTER 3. ITSE1359-1015-PROGRAM ORGANIZATION
Also, as you write more complicated programs, you will need to use modules that are contained in the
standard Python distribution. Here is a link to an index of those modules in Python 3.4.2:
This information is being provided at this early stage in the course so that you will know where to nd it
when you need it.
3.4 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1015-Program Organization
• File: Itse1359-1015.htm
• Published: 10/14/14
• Revised: 12/28/14
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
7 http://www.learnpython.org/en/Modules_and_Packages
8 http://en.wikibooks.org/wiki/A_Beginner's_Python_Tutorial/Importing_Modules
9 http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3/Using_Modules
10 https://docs.python.org/3/py-modindex.html
Itse1359-1020r-Review 1
4.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1020-Numbers
.
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.)
4.3 Questions
4.3.1 Question 1
True or False? Python programming comments are ignored by the computer.
Go to answer 1 (p. 21)
4.3.2 Question 2
True or False? Just like in C, C++, and Java, a Python comment begins with two slash characters (//) and
continues to the end of the line.
Go to answer 2 (p. 21)
1 This content is available online at <http://cnx.org/content/m51317/1.2/>.
Available for free at Connexions <http://cnx.org/content/col11986/1.1>
17
18 CHAPTER 4. ITSE1359-1020R-REVIEW
4.3.3 Question 3
True or False? The only prompt used by the Python interactive system is the one consisting of three
right-angle brackets (>).
Go to answer 3 (p. 21)
4.3.4 Question 4
True or False? The output produced by the Python interactive system appears on a line without either of
the prompts mentioned above.
Go to answer 4 (p. 21)
4.3.5 Question 5
True or False? If you enter an expression at the prompt and press the Enter key, the result of evaluating the
expression will be displayed on the next line without a prompt.
Go to answer 5 (p. 21)
4.3.6 Question 6
True or False? Computer programs are composed of expressions, which are made up of statements.
Go to answer 6 (p. 21)
4.3.7 Question 7
In the following expression, 2+5, what is the common jargon for the plus sign, and what is the common
jargon for the 2 and the 5?
Go to answer 7 (p. 21)
4.3.8 Question 8
List the operators discussed in the module titled Itse1359-1020-Numbers , and describe the purpose of
each.
Go to answer 8 (p. 21)
4.3.9 Question 9
True or False? Integer division produces a decimal result with non-zero digits to the right of the decimal
point.
Go to answer 9 (p. 20)
4.3.10 Question 10
True or False? The modulus operator is used to produce the quotient in division.
Go to answer 10 (p. 20)
4.3.11 Question 11
Describe the use of parentheses in expressions.
Go to answer 11 (p. 20)
4.3.12 Question 12
Describe how Python evaluates an expression containing parentheses.
Go to answer 12 (p. 20)
What is the meaning of the following two images?
These images were inserted here simply to insert some space between the questions and the answers to
keep them from being visible on the screen at the same time.
This image was also inserted for the purpose of inserting space between the questions and the answers.
4.4 Answers
4.4.1 Answer 12
A Python expression is evaluated by rst evaluating each of the sub-expressions in the parentheses, and then
using those values to complete the evaluation. If the expression contains nested parentheses, the evaluation
is performed by evaluating the innermost parentheses rst and working outwards from there.
Go back to Question 12 (p. 19)
4.4.2 Answer 11
Parentheses can be used to group terms in an expression in order to provide control over the order in which
the operations are performed.
Go back to Question 11 (p. 18)
4.4.3 Answer 10
False, the modulus operator is used to produce the remainder.
Go back to Question 10 (p. 18)
4.4.4 Answer 9
False. Integer division produces an integer or whole number result with no non-zero digits to the right of
the decimal point even if a decimal point is displayed.
4.4.5 Answer 8
The operators and their purposes are:
4.4.6 Answer 7
The plus sign is commonly called the operator. The 2 and the 5 are commonly called operands. More
specically, the 2 is the left operand and the 5 is the right operand.
Go back to Question 7 (p. 18)
4.4.7 Answer 6
False. Just the reverse is true. Programs are made up of statements, which are composed of expressions.
Go back to Question 6 (p. 18)
4.4.8 Answer 5
True, unless the expression can't be evaluated, in which case an error message will appear.
Go back to Question 5 (p. 18)
4.4.9 Answer 4
True.
Go back to Question 4 (p. 18)
4.4.10 Answer 3
False. The Python interactive system also uses a secondary prompt consisting of three periods (...).
Go back to Question 3 (p. 18)
4.4.11 Answer 2
False. In Python, a comment starts with the hash character (#) and ends at the end of the line.
Go back to Question 2 (p. 17)
4.4.12 Answer 1
True. Programming comments are used for program documentation and are intended for human consump-
tion.
Go back to Question 1 (p. 17)
4.5 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1020r-Review
• File: Itse1359-1020r.htm
• Published: 10/14/14
• Revised: 12/28/14
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1030r-Review 1
5.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1030-Variables
and Identiers .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.)
5.3 Questions
5.3.1 Question 1
True or False? A variable is the same as a constant.
Go to answer 1 (p. 28)
5.3.2 Question 2
True or False? Python is a strongly typed language.
Go to answer 2 (p. 28)
1 This content is available online at <http://cnx.org/content/m51319/1.3/>.
Available for free at Connexions <http://cnx.org/content/col11986/1.1>
23
24 CHAPTER 5. ITSE1359-1030R-REVIEW
5.3.3 Question 3
True or False? Python programmers must declare all variables.
Go to answer 3 (p. 28)
5.3.4 Question 4
Explain the dangers of using a language that does not require variables to be declared.
Go to answer 4 (p. 28)
5.3.5 Question 5
What is the best defense against spelling errors in variables names?
Go to answer 5 (p. 28)
5.3.6 Question 6
True or False? Variable names can begin with numeric or digit characters.
Go to answer 6 (p. 28)
5.3.7 Question 7
Write a simple program that illustrates case sensitivity in the names of variables.
Go to answer 7 (p. 27)
5.3.8 Question 8
Explain the use of the assignment operator.
Go to answer 8 (p. 27)
5.3.9 Question 9
Which type usually provides the greater range for storage of numeric values, integer or oating point?
Go to answer 9 (p. 27)
5.3.10 Question 10
Should you just always use oating point instead of integer to be safe?
Go to answer 10 (p. 27)
5.3.11 Question 11
Write a simple program that illustrates the approximation nature of oating point arithmetic.
Go to answer 11 (p. 26)
5.3.12 Question 12
Explain the purpose of the automatic continuation variable whose name is simply the underscore character.
Go to answer 12 (p. 26)
This image was also inserted for the purpose of inserting space between the questions and the answers.
5.5 Answers
5.5.1 Answer 12
The primary purpose of the automatic variable named _ is to make it easier for you to string calculations
together in interactive mode and to display the intermediate results as you go.
Go back to Question 12 (p. 24)
5.5.2 Answer 11
See the sample program in Figure 3 (p. 26) . We know that the true result of this expression is an unending
string of nines, as in 9.999999999999999
Table 5.1
However, in this case, Python returned an answer of 10.0, indicating that the answer is accurate to three
signicant gures. This is not really the correct answer, but as a practical matter, it may be the best
answer. I will leave that for you to decide.
Go back to Question 11 (p. 24)
5.5.3 Answer 10
Probably not. Floating point arithmetic often suers from speed penalties. In addition, integer arithmetic
produces exact results while oating point arithmetic usually produces approximate results (although the
approximations may be very close to being exact in many cases) .
Go back to Question 10 (p. 24)
5.5.4 Answer 9
Floating point usually provides the greater range for storage of numeric values.
Go back to Question 9 (p. 24)
5.5.5 Answer 8
The assignment operator causes the value of its right operand to be stored in the memory location identied
by its left operand.
Go back to Question 8 (p. 24)
5.5.6 Answer 7
An example of such a program is shown in Figure 1 (p. 27) . Note that the names of the two variables have
the same letters, but dierent case. The fact that the two variables are dierent variables is illustrated by
the fact that each is assigned a dierent value. The sum of the two variables demonstrates that the two
variables contain dierent, and correct, values.
Table 5.2
Contrast the above result with the program in Figure 2 (p. 28) where a variable whose name contains
the same letters and the same case is used twice.
> ax=10
> ax=20
> ax+ax
40
>
Table 5.3
All the program in Figure 2 (p. 28) accomplishes is the assignment of two dierent values to the same
variable. The second assignment overwrites the value previously assigned to the variable. The program then
adds the variable to itself using its current value of 20 producing a result of 40 (instead of 30 as in the
previous example) .
Go back to Question 7 (p. 24)
5.5.7 Answer 6
False. Variable names must begin with a letter or underscore character.
Go back to Question 6 (p. 24)
5.5.8 Answer 5
Use meaningful variable names for which the spelling is obvious, such as theOverheadRate .
Go back to Question 5 (p. 24)
5.5.9 Answer 4
Several dierent kinds of problems can result from making typing errors that result in misspelling the names
of variables. These errors usually result in programs that produce incorrect results without warning.
Go back to Question 4 (p. 24)
5.5.10 Answer 3
False. Variable declarations are not required in Python. All that is required to cause a variable to come
into existence is to invent a new name for a variable and assign a value to it.
Go back to Question 3 (p. 24)
5.5.11 Answer 2
False. Python is not a strongly typed language. From a pure technical viewpoint, the Python programmer
rarely needs to be concerned about type.
Go back to Question 2 (p. 23)
5.5.12 Answer 1
False. The value of a variable is intended to change during the execution of the program. The value of a
constant (which I haven't discussed so far in this collection) is not intended to change.
Go back to Question 1 (p. 23)
5.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1030r-Review
• File: Itse1359-1030r.htm
• Published: 10/14/14
• Revised: 20/20/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1050-Introduction to Scripts 1
31
32 CHAPTER 6. ITSE1359-1050-INTRODUCTION TO SCRIPTS
6.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX. It provides an introduction to
the use of scripts, and of necessity will depart from the interactive mode used in previous modules.
6.2.1.1 Figures
• Figure 1 (p. 35) . Screen output from running the script named 1359-1050-01.py.
• Figure 2 (p. 37) . Online Python Tutor visualizer screen output.
• Figure 3 (p. 37) . Visualizing a program from a future module.
6.2.1.2 Listings
• Listing 1 (p. 34) . Setting the path temporarily with a batch le.
• Listing 2 (p. 35) . Contents of the le named 1359-1050-01.py.
6.3 Introduction
This module provides an introduction to the use of scripts, and of necessity will depart from the interactive
mode used in previous modules.
It will be necessary for me to get into a small amount of system stu that really has nothing in particular
to do with Python programming. Rather, it will involve getting your computer set up for using scripts with
Python.
Up to this point in this collection, I have concentrated on the interactive programming capability of
Python. This is a very useful capability that allows you to type in a program and to have it executed
immediately in an interactive mode. But, interactive can be burdensome. By now you may have realized
that you sometimes nd yourself typing the same thing over and over. That is where scripts are useful.
Batch les also make it possible to execute commands on the command line in the "current directory"
without the requirement to go through the steps that are normally required to open a command-line window
and navigate to the current directory.
echo off
python 1359-1050-01.py
pause
Table 6.1
The second command in Listing 1 (p. 34) is the important command for this section of this module.
It will temporarily set the path to the indicated folder. If you elect to use this approach, you will need to
replace the text inside the double quotes with the path to the folder containing the le named python.exe
on your computer.
This approach appends the new path onto the environment variable named path on a temporary basis.
It goes away as soon as you respond to the pause statement at the end or otherwise close the command-line
window that appears when you execute the batch le.
a=2
b=3
a=2*a
b=3*b
c=a+b
print(c)
Table 6.2
You should recognize all of the statements in Listing 2 (p. 35) . Note, however, that in a script, they are
not preceded by an interactive prompt (>).
Store this le in any folder on your hard drive. You may want to create a new directory for the sole
purpose of storing Python script les.
Then create the corresponding batch le with the contents shown in Listing 1 (p. 34) and save it in the
same folder as the script le. (If you have permanently set your path to support Python, you can omit the
path command in Listing 1 (p. 34) .)
M:\Baldwin\AA-School\...>echo off
13
Press any key to continue . . .
Table 6.3
• Skulpt 2
• Online code visualizer 3
• How To Think Like a Computer Scientist - Interactive 4
All three of these approaches run in a browser and can even be used with a tablet or a smart phone.
6.4.3.1 Skulpt
Skulpt 5 is an entirely in-browser implementation of Python. No local installation, plugins, or server-side
support is required.
Just go to www.skulpt.org 6 in your computer, tablet, or smart phone, scroll down to the Demo pane,
type in your Python code and click the Run button below the Demo pane. The program output will
appear in the Output pane below the Run button.
You can learn more about using Skulpt, including how to embed Skulpt in your HTML pages in the
module titled Itse1359-2410-Getting Started with Skulpt 7 in this book 8 .
6.4.4 Visualize your script le with the Online Python Tutor
When learning to program, it is often useful to be able to follow the execution of a program step-by-step. A
free online tool called the Online Python Tutor 10 makes this possible. (You have seen some output from
this tool in earlier modules. I will provide a more detailed explanation of how to use the tool in the module
titled Itse1359-1065-Visualizing Python 11 in this book 12 . )
The following quotation was copied from pythontutor.com 13 .
" Online Python Tutor 14 is a free educational tool created by Philip Guo 15 that
helps students overcome a fundamental barrier to learning programming: understanding what
2 http://www.skulpt.org/
3 http://pythontutor.com/
4 http://interactivepython.org/courselib/static/thinkcspy/toc.html
5 http://www.skulpt.org/
6 http://www.skulpt.org
7 http://cnx.org/contents/d28b8c4d-dfa6-4c49-a071-56fd68d7cb31
8 http://cnx.org/contents/96b34405-9562-406b-bafb-90c5fb70f39e
9 http://interactivepython.org/courselib/static/thinkcspy/toc.html#
10 http://pythontutor.com/visualize.html#mode=edit
11 http://cnx.org/contents/71aca310-6390-40c4-9001-561cde902a0c
12 http://cnx.org/contents/96b34405-9562-406b-bafb-90c5fb70f39e
13 http://pythontutor.com/
14 http://pythontutor.com/visualize.html#mode=edit
15 http://www.pgbovine.net/
happens as the computer executes each line of a program's source code. Using this tool, a teacher
or student can write Python , Java , and JavaScript programs in the Web browser
and visualize what the computer is doing step-by-step as it executes those programs."
Beginning with this module and continuing with future modules, I will recommend that you
The screen will then change to include what you see in Figure 2 (p. 37) . Use the Forward and Back
buttons to step forward and backward through your code, one instruction at a time, while observing the
information that appears on the screen to the right of the code window.
For example, Figure 2 (p. 37) shows the Online Python Tutor 18 visualizer screen after the rst three
statements from Listing 2 (p. 35) have been executed. The information on the right shows the values stored
in the variables named a and b at that point in the execution of the program.
Figure 2 . Online Python Tutor visualizer screen output.
As you might expect, more complicated code produces more complicated output from the visualizer. For
example, Figure 3 (p. 37) shows the output from the Online Python Tutor 19 when visualizing a program
from a future module titled Itse1359-1440-Class Variables 20 in this book 21 .
Figure 3 . Visualizing a program from a future module.
16 http://pythontutor.com/visualize.html#mode=edit
17 http://pythontutor.com/visualize.html#mode=edit
18 http://pythontutor.com/visualize.html#mode=edit
19 http://pythontutor.com/visualize.html#mode=edit
20 http://cnx.org/contents/88f5bdd0-474b-4f69-806d-a383346db53a
21 http://cnx.org/contents/96b34405-9562-406b-bafb-90c5fb70f39e
6.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1050-Introduction to Scripts
• File: Itse1359-1050.htm
• Published: 10/14/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1060-Syntax 1
7.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction
to Scripting Languages: Python at Austin Community College in Austin, TX. It explains some of the
mechanics of putting together a Python program.
7.2.1.1 Figures
• Figure 1 (p. 43) . Two physical lines.
• Figure 2 (p. 47) . Output from the script in Listing 4.
7.2.1.2 Listings
• Listing 1 (p. 43) . Explicit line joining.
• Listing 2 (p. 44) . Implicit line joining.
• Listing 3 (p. 45) . Blank lines and whitespace.
• Listing 4 (p. 46) . An example of correct indentation.
• Listing 5 (p. 48) . Another sample script.
7.3 Introduction
You have learned how to write some simple programs and how to execute them interactively.
You have also learned how to capture simple programs in script les and how to execute those script les.
Now it is time to learn a little more about the mechanics of putting together a Python program.
We refer to this in programming jargon as the newline character (even though it is actually two
characters on Windows systems) .
Basically, a physical line is what you get when you type some characters into your text editor and press
the Enter key, the Return key, or whatever you call that key on your keyboard. (Actually you don't
even need to type characters before pressing the Enter key, in which case you get a blank line.)
This is the key that causes the cursor to go down to the next line and return to the left side of the editing
window.
For example, when creating the text shown in Figure 1 (p. 43) using a text editor on my Windows
computer, I pressed the Enter key immediately following the > for each line. This produced a newline in
each case.
Table 7.1
7.4.2.2 Comments
We learned about comments in an earlier module. To summarize, a comment starts with a hash character
(#) that is not part of a string literal (we also learned about string literals in an earlier module) . The
comment ends at the end of the physical line.
a = 3
b = 4
# construct c=a+b
c \
= \
a \
+ \
b
print(c)
# the output is 7
Table 7.2
a = 3
b = 4
c = (a # continue on next line
+ b)
print(c)
# the output is 7
Table 7.3
You can also place a comment on a line that is being continued implicitly as shown in the third line of
Listing 2 (p. 44) .
Also, you can indent the continuation line however you want. This is very useful for making the code
more readable.
a = 3
# spaces and comment
b = 4
# tab and comment
c = (a # continue on next line
+ b)
# a blank line follows
print(c)
# the output is 7
Table 7.4
7.5 Indentation
In every other programming environment that I have worked with in the past, indentation is used strictly
for cosmetic or readability purposes.
However, indentation is not used strictly for cosmetic or readability purposes in Python. It is such an
important topic in Python that I have separated it into a major section in this module.
A = 3
B = 4
if B > A:
print(A) # begin group
print(B)
print(A + B) # end group
A = 6 # not part of above group
print(A)
#============================================================
#The output, which is not part of the script, is shown below.
3
4
7
6
Table 7.5
C:\jnk\6>echo off
3
4
7
6
Press any key to continue . . .
Table 7.6
Then the value 6 is printed by the last statement in the script, which is not part of the group. (Note
that the last statement is not indented.)
A = 4
B = 3
if B > A:
print(A) # begin group
print(B)
print(A + B) # end group
A = 6 # not part of above group
print(A)
#============================================================
#The output, which is not part of the script, is shown below.
6
Table 7.7
In this case, only one value (6) is printed because the three print statements in the group were bypassed
as a group.
7.5.2.6 An opinion
I personally don't like the idea of using indentation to create grouping. Although it sounds nice in theory, it
can be very labor intensive in practice. Once you have written a script, one simple change can often require
you to go back and modify the indentation level of almost every statement in the script.
I guess the good news is that this will encourage you to write your script as a series of short, concise
independent modules rather than as a single long rambling script.
I am also concerned about the accessibility or lack thereof that grouping based on indentation level
provides for blind and visually impaired students.
7.5.2.7.2 Tabs
My advice is to avoid the use of tabs altogether. Use spaces instead, and use the same number of spaces for
each statement in the group.
However, if you must use tabs, you should go to the Python Language Reference 2.1.8. Indentation 2
and make certain that you understand how Python deals with tabs.
2 https://docs.python.org/3/reference/lexical_analysis.html#indentation
7.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1060-Syntax
• File: Itse1359-1060.htm
• Published: 10/14/14
• Revised: 03/26/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
3 http://cnx.org/contents/71aca310-6390-40c4-9001-561cde902a0c
4 http://cnx.org/contents/96b34405-9562-406b-bafb-90c5fb70f39e
Itse1359-1065-Visualizing Python 1
8.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX. This module concentrates on
the use of the Online Python Tutor 2 , which includes a free educational code visualizer tool created by
Philip Guo 3 .
This code visualizer tool 4 helps students overcome a fundamental barrier to learning programming:
understanding what happens as the computer executes each line of a program's source code. Using this code
visualizer tool, a teacher or student can write Python, Java, and JavaScript programs in the Web browser
and visualize what the computer is doing step-by-step as it executes those programs.
1 This content is available online at <http://cnx.org/content/m53203/1.3/>.
2 http://pythontutor.com/
3 http://www.pgbovine.net/
4 http://pythontutor.com/visualize.html#mode=edit
51
52 CHAPTER 8. ITSE1359-1065-VISUALIZING PYTHON
8.2.1.1 Figures
• Figure 1. (p. 54) The online text editor.
• Figure 2. (p. 55) Beginning the visualization process.
• Figure 3. (p. 55) Output following three presses of the Forward button.
• Figure 4. (p. 56) Same situation, dierent code.
• Figure 5. (p. 57) The student torture program.
8.2.1.2 Listings
• Listing 1. (p. 53) A sample program with proper indentation.
• Listing 2. (p. 53) Another sample program with proper indentation.
" Online Python Tutor 10 is a free educational tool created by Philip Guo 11 that
helps students overcome a fundamental barrier to learning programming: understanding what
happens as the computer executes each line of a program's source code. Using this tool, a teacher
or student can write Python , Java , and JavaScript programs in the Web browser
and visualize what the computer is doing step-by-step as it executes those programs."
8.4 Recommendation
I recommend that from this point forward in this course, you copy and paste every sample program into the
code visualizer and observe the behavior of that code as you step through the program, one instruction at a
time. I believe that doing that will help you to better understand Python programming.
The procedure for doing that is as follows:
A = 3
B = 4
if B > A:
print(A) # begin group
print(B)
print(A + B) # end group
A = 6 # not part of above group
print(A)
Table 8.1
A = 4
B = 3
if B > A:
print(A) # begin group
print(B)
print(A + B) # end group
A = 6 # not part of above group
print(A)
Table 8.2
14 http://cnx.org/contents/f62bf8c2-00f8-44ed-a24a-1fd30ef3d7fc
15 http://cnx.org/contents/96b34405-9562-406b-bafb-90c5fb70f39e
The blank portion at the top of Figure 1 (p. 54) is the text editor portion of the screen.
step through the program. In some cases, you might want to step through the same program more than
once with dierent sets of options selected.
We see from the green arrow on the left that the relational test in the if statement was the most recently
executed instruction.
We see on the right that the variables named A and B contain references (pointers) to objects of
type int containing values of 3 and 4 respectively.
Because B is greater than A , the relational test in the if statement (pointed to by the green
arrow) returned true when that statement was executed. This means that the three statements in the
indented block following the if statement will be executed. The red arrow points to the rst statement in
the indented block meaning that it will be executed the next time the user presses the Forward button.
Note that the values stored in the variables named A and B were reversed relative to the code in
Figure 3 (p. 55) . As a result, the if statement pointed to by the green arrow returned false instead of
true when that statement was executed. Because of that, execution of the indented code block following the
if statement will be skipped entirely and the red arrow now points to the statement immediately following
that code block. That is the next statement that will be executed.
8.7 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1065-Visualizing Python
• File: Itse1359-1065.htm
• Published: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1080r-Review 1
9.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1080-Lists Part
1 .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.)
9.3 Questions
9.3.1 Question 1
True or False? A subscription in Python is how you go about getting magazines delivered to your mailbox.
Go to answer 1 (p. 65)
1 This content is available online at <http://cnx.org/content/m51352/1.5/>.
59
60 CHAPTER 9. ITSE1359-1080R-REVIEW
9.3.2 Question 2
Name three types of sequence objects.
Go to answer 2 (p. 65)
9.3.3 Question 3
Given the following nomenclature
primary "[" expression_list "]"
what is the requirement for the primary ?
Go to answer 3 (p. 65)
9.3.4 Question 4
If the primary is a sequence , what must be the type of expression_list?
Go to answer 4 (p. 65)
9.3.5 Question 5
True or False? If the primary is a sequence , the expression_list must evaluate to a positive integer or to
a slice containing positive integers.
Go to answer 5 (p. 65)
9.3.6 Question 6
Which item in the sequence is selected for an index value of -1?
Go to answer 6 (p. 65)
9.3.7 Question 7
True or False? Just like C, C++, and Java, Python supports a character type.
Go to answer 7 (p. 65)
9.3.8 Question 8
What is another name for a mapping ?
Go to answer 8 (p. 65)
9.3.9 Question 9
What is a mutable sequence?
Go to answer 9 (p. 64)
9.3.10 Question 10
True or False? A string is a sequence.
Go to answer 10 (p. 64)
9.3.11 Question 11
True or False? A string is a mutable sequence.
Go to answer 11 (p. 64)
9.3.12 Question 12
What kinds of items can be placed in a list ?
Go to answer 12 (p. 64)
9.3.13 Question 13
How are lists formed from a syntax viewpoint?
Go to answer 13 (p. 64)
9.3.14 Question 14
Show how to create a simple list using program code.
Go to answer 14 (p. 64)
9.3.15 Question 15
Show how to access an item in a list using a subscription.
Go to answer 15 (p. 63)
9.3.16 Question 16
Show how to access a slice from a list.
Go to answer 16 (p. 63)
9.3.17 Question 17
Show how to concatenate two lists.
Go to answer 17 (p. 63)
9.3.18 Question 18
Show how to modify an item in a list using a subscription.
Go to answer 18 (p. 63)
This image was also inserted for the purpose of inserting space between the questions and the answers.
9.5 Answers
9.5.1 Answer 18
Listing 5 (p. 63) shows how to modify an item in a list using a subscription.
Table 9.1
9.5.2 Answer 17
Listing 4 (p. 63) shows how to concatenate two lists.
Table 9.2
9.5.3 Answer 16
Listing 3 (p. 63) shows how to access a slice from a list.
print(theList[0:3])
Table 9.3
9.5.4 Answer 15
Listing 2 (p. 64) shows how to access an item in a list using a subscription.
print(theList[2])
Table 9.4
9.5.5 Answer 14
Listing 1 (p. 64) shows a simple list.
Table 9.5
9.5.6 Answer 13
Lists are formed by placing a comma-separated sequence of expressions in square brackets.
Go back to Question 13 (p. 61)
9.5.7 Answer 12
The items of a list are arbitrary Python objects.
Go back to Question 12 (p. 61)
9.5.8 Answer 11
False. The characters in a string cannot be modied after the string is created.
Go back to Question 11 (p. 60)
9.5.9 Answer 10
True. A string is a sequence.
Go back to Question 10 (p. 60)
9.5.10 Answer 9
Mutable sequences can be changed after they are created.
Go back to Question 9 (p. 60)
9.5.11 Answer 8
A dictionary.
Go back to Question 8 (p. 60)
9.5.12 Answer 7
False. There is no character type in Python. Rather, a string's items are characters. A character is not a
separate data type but a string containing exactly one character.
Go back to Question 7 (p. 60)
9.5.13 Answer 6
The last item in the sequence is selected for an index value of -1.
Go back to Question 6 (p. 60)
9.5.14 Answer 5
False. The integers may be positive or negative.
Go back to Question 5 (p. 60)
9.5.15 Answer 4
If the primary is a sequence , the expression_list must evaluate to an integer or a slice.
Go back to Question 4 (p. 60)
9.5.16 Answer 3
The primary must evaluate to an object of a sequence or mapping type.
Go back to Question 3 (p. 60)
9.5.17 Answer 2
string, tuple and list and possibly range object
Go back to Question 2 (p. 60)
9.5.18 Answer 1
False in the Python context. In Python, A subscription selects an item of a sequence object.
Go back to Question 1 (p. 59)
9.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1080r-Review
• File: Itse1359-1080r.htm
• Published: 10/15/14
• Revised: 02/21/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1090-Lists Part 2 1
10.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction
to Scripting Languages: Python at Austin Community College in Austin, TX. It expands on the previous
module titled Itse1359-1080-Lists Part 1 2 by teaching you some other ways to manipulate lists.
10.2.1.1 Figures
• Figure 1 (p. 70) . Program output.
• Figure 2 (p. 71) . Program output from replacing an element with a list.
• Figure 3 (p. 72) . Visualization
• Figure 4 (p. 74) . Program output from extracting elements from a nested list.
• Figure 5 (p. 75) . Output from more nested elements.
• Figure 6 (p. 77) . Visualization of a three-dimensional array.
• Figure 7 (p. 79) . Output from a three-dimensional array program.
10.2.1.2 Listings
• Listing 1 (p. 69) . Replace a slice in a list.
• Listing 2 (p. 71) . Replacing an element with a list.
• Listing 3 (p. 73) . Extracting elements from a nested list.
• Listing 4 (p. 75) . More nested elements.
• Listing 5 (p. 77) . A three-dimensional array program.
10.3 Introduction
A previous module titled Itse1359-1080-Lists Part 1 3 introduced you to lists and some other structures as
well.
2 http://cnx.org/contents/81942bae-453e-4cce-ac50-6e9ed9f2c54e
3 http://cnx.org/contents/81942bae-453e-4cce-ac50-6e9ed9f2c54e
• subscriptions
• sequences
• mutable sequences
• mappings
• slicing, and
• tuples.
Table 10.1
Table 10.2
As you can see in Figure 1 (p. 70) , the six new elements replaced the three original elements to increase
the length of the list from 5 to 8 elements.
# Illustrates replacing an
# element with a slice
#
#-------------------------------
print("Create and print a list")
listA = [100,200,300,400,500]
print(listA)
print("Original length is:")
print(len(listA))
print("Replace an element")
listA[2] = [2,4,8,16,32,64]
print("Print the modified list")
print(listA)
print("Modified length is:")
print(len(listA))
Table 10.3
Table 10.4
10.4.3 A visualization
The code visualization shown in Figure 3 (p. 72) summarizes the concepts that were explained in the previous
two sections. This image was produced by entering the ve statements shown in the code visualizer's 4 code
window and stepping through all ve instructions.
Figure 3. Visualization.
I recommend that you use the code visualizer 5 to step through this code and observe the state of the
program in memory as each instruction is executed. Try to correlate the behavior of this program with what
you learned in the sections above titled Replacing a slice (p. 69) and Replacing an element with a list (p.
70) .
4 http://pythontutor.com/visualize.html#mode=display
5 http://pythontutor.com/visualize.html#mode=display
# Illustrates extracting a
# list element and extracting
# elements from a nested list
#
#-------------------------------
print("Create and print a list")
listA = [100,200,300,400,500]
print(listA)
print("Original length is:")
print(len(listA))
print("Replace an element")
listA[2] = [2,4,8,16,32,64]
print("Print the modified list")
print(listA)
print("Modified length is:")
print(len(listA))
print("Extract and display each")
print(" element in the list")
print(listA[0])
print(listA[1])
print(listA[2])
print(listA[3])
print(listA[4])
Table 10.5
Table 10.6
Table 10.7
Table 10.8
Table 10.9
Table 10.10
10.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1090-Lists Part 2
• File: Itse1359-1090.htm
• Published: 10/15/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1110-Nested Tuples 1
11.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
11.2.1.1 Figures
• Figure 1 (p. 83) . Output from the code in Listing 1.
• Figure 2 (p. 84) . Output from the code in Listing 2.
1 This content is available online at <http://cnx.org/content/m51387/1.5/>.
81
82 CHAPTER 11. ITSE1359-1110-NESTED TUPLES
11.2.1.2 Listings
• Listing 1 (p. 83) . Beginning of the program.
• Listing 2 (p. 83) . Nesting the tuples.
• Listing 3 (p. 84) . Get the length of the new tuple.
• Listing 4 (p. 85) . Complete program listing.
11.3 Introduction
Previous modules have introduced you to lists, subscriptions, sequences, mutable sequences, mappings,
slicings, and tuples.
The earlier module titled Itse1359-1100-Indexing and Slicing Tuples 2 showed you:
• How to create a tuple.
• How to access a tuple item using indexing.
• How to slice a tuple.
This module will expand your knowledge of tuples by teaching you about nesting tuples within other tuples.
2 http://cnx.org/contents/59b92f58-fa74-4624-a37a-37ab2794536a
3 http://pythontutor.com/visualize.html#mode=display
Table 11.1
The code in Listing 1 (p. 83) creates and displays the two tuples.
Figure 1 (p. 83) shows the output produced by the code fragment in Listing 1 (p. 83) .
Table 11.2
The remaining parts of this program will be presented and discussed as code fragments in subsequent
Listings. As mentioned above, a complete listing of the program is shown in Listing 4 (p. 85) .
A consolidated view of the output from the program is shown in Figure 4 (p. 85) .
Table 11.3
All that is required to nest the existing tuples in a new tuple is to list the variables that reference the two
existing tuples in a comma-separated list of items for creation of the new tuple. (Note that the optional
parentheses were omitted in Listing 2 (p. 83) .)
Figure 2 (p. 84) shows the output for the new tuple containing two nested tuples.
Table 11.4
Note that the two nested tuples retain their identity as tuples, as indicated by the fact that the parentheses
surrounding the items in the two nested tuples are preserved in the new tuple. This is also indicated by the
visualization 4 in Figure 5 (p. 86) .
Table 11.5
The length is a measure of the number of items in the tuple, and is obtained using the method named
len .
Figure 3 (p. 84) shows the output produced by the code in Listing 3 (p. 84) , including the length of the
new tuple containing the two nested tuples.
Table 11.6
4 http://pythontutor.com/visualize.html#mode=display
It is important to note that even though the tuple shown in Figure 2 (p. 84) actually consists of six
individual items (ignoring parentheses) , each of the nested tuples is treated as a single item, giving a
length of only four items for the tuple that contains the two nested tuples.
This would be true regardless of the length of the nested tuples.
You will learn in a future module that a double square-bracket indexing notation can be used to gain
access to the individual items in tuples that are nested inside of other tuples.
Table 11.7
Figure 4 (p. 85) shows a consolidated view of the output produced by this program.
Table 11.8
11.9 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1110-Nested Tuples
• File: Itse1359-1110.htm
• Published: 10/19/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
5 http://pythontutor.com/visualize.html#mode=display
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1110r-Review 1
12.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1110-Nested
Tuples .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.)
12.3 Questions
12.3.1 Question 1
True or False? The program code in Listing 1 (p. 90) produces the output shown in Figure 1 (p. 90) .
89
90 CHAPTER 12. ITSE1359-1110R-REVIEW
t1 = 1,2
print(t1)
t2 = "a","b"
print(t2)
t3 = "A",t1,"B",t2
print(t3)
Table 12.1
(1, 2)
('a', 'b')
('A', (1, 2), 'B', ('a', 'b'))
Table 12.2
12.3.2 Question 2
True or False? The program code in Listing 2 (p. 90) produces the output shown in Figure 2 (p. 91) .
t1 = 1,2
print(t1)
t2 = "a","b"
print(t2)
t3 = "A",t1,"B",t2
print(t3)
print(len(t3))
Table 12.3
(1, 2)
('a', 'b')
('A', (1, 2), 'B', ('a', 'b'))
6
Table 12.4
This image was also inserted for the purpose of inserting space between the questions and the answers.
12.6 Answers
12.6.1 Answer 2
False. The actual output is shown in Figure 3 (p. 92) . The code in Listing 2 (p. 90) produces a tuple
containing two other nested tuples. A nested tuple counts as only one element in the length of a tuple
containing nested tuples.
(1, 2)
('a', 'b')
('A', (1, 2), 'B', ('a', 'b'))
4
Table 12.5
12.6.2 Answer 1
True.
Go back to Question 1 (p. 89)
12.7 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1110r-Review
• File: Itse1359-1110r.htm
• Published: 10/19/14
• Revised: 02/23/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
13.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
95
96 CHAPTER 13. ITSE1359-1120-EMPTY AND SINGLE-ITEM TUPLES
13.2.1.1 Figures
• Figure 1 (p. 97) . Output from the code in Listing 1.
• Figure 2 (p. 98) . Output from the code in Listing 2.
• Figure 3 (p. 99) . Output from the code in Listing 3.
• Figure 4 (p. 100) . Consolidated output from the program in Listing 4.
• Figure 5 (p. 101) . Visualization of the tuples.
13.2.1.2 Listings
• Listing 1 (p. 97) . Beginning of the program.
• Listing 2 (p. 97) . A tuple with only one element.
• Listing 3 (p. 98) . Create and print nested tuples.
• Listing 4 (p. 99) . Create and print nested tuples.
13.3 Introduction
This module is part of a series of modules designed to teach you about tuples in Python.
Previous modules have illustrated
• How to create a tuple.
• How to access a tuple item using indexing.
• How to slice a tuple.
• How to nest tuples.
This module will teach you how to create empty tuples and tuples containing only one item.
Table 13.1
As you might have guessed from the name, an empty tuple is just a pair of empty parentheses as shown
by the second statement in Listing 1 (p. 97) . (The parentheses aren't optional for an empty tuple.)
Figure 1 (p. 97) shows the output produced by the code in Listing 1 (p. 97) . The empty tuple is
displayed simply as a pair of empty parentheses, and the length of the empty tuple is shown to be zero.
Table 13.2
Table 13.3
The syntax for creating a tuple with only one element is rather ugly, but is required to avoid ambiguity.
In particular, it is necessary to follow the single tuple item with a comma as shown in the third line of text
in Listing 2 (p. 97) .
Had I written that line simply as follows without the extra comma,
t2 = "a"
the result would have been to create a new variable named t2 whose contents would be the string "a".
(The parentheses are optional here but the comma is required with or without the parentheses.)
This would not indicate a tuple at all. The extra comma is required to make a single-item tuple unique
and to distinguish it from other possibilities.
Figure 2 (p. 98) shows the output produced by the code in Listing 2 (p. 97) . The single-item tuple
is shown in the third line of text in Listing 2 (p. 97) . As is always the case, the tuple is displayed in
parentheses.
Table 13.4
The length of the tuple as shown in Figure 2 (p. 98) is one (1) item.
Table 13.5
Table 13.6
The length of the tuple is also shown in Figure 3 (p. 99) . Even though the tuple named t3 contains
two nested tuples (one of which is doubly-nested) , its overall length is only ve (5) items.
One of the tuples nested inside of t3 has a length of zero but it still counts as one item when the length
of t3 is determined.
3 http://pythontutor.com/visualize.html#mode=display
Table 13.7
Table 13.8
13.9 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1120-Empty and Single-Item Tuples
• File: Itse1359-1120.htm
• Published: 10/19/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
4 http://pythontutor.com/visualize.html#mode=display
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1120r-Review 1
14.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1120-Empty and
Single-Item Tuples .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.)
14.3 Questions
14.3.1 Question 1
True or False? The code in Figure 1 (p. 104) creates and prints an empty tuple producing the output shown
in Figure 2 (p. 104) .
103
104 CHAPTER 14. ITSE1359-1120R-REVIEW
t1 =
print(t1)
print(len(t1))
Table 14.1
()
0
Table 14.2
14.3.2 Question 2
True or False? The code in Figure 4 (p. 104) creates and prints an empty tuple producing the output shown
in Figure 5 (p. 104) .
t1 = ()
print(t1)
print(len(t1))
Table 14.3
()
0
Table 14.4
14.3.3 Question 3
True or False? The code in Figure 6 (p. 105) produces the output shown in Figure 7 (p. 105) .
t2 = ("a")
print(t2)
Table 14.5
('a')
Table 14.6
14.3.4 Question 4
True or False? The code in Figure 9 (p. 105) produces the output shown in Figure 10 (p. 105) .
t2 = ("a",)
print(t2)
Table 14.7
('a',)
Table 14.8
14.3.5 Question 5
True or False? The code in Figure 11 (p. 106) produces the output shown in Figure 12 (p. 106) .
t1 = ()
print(t1)
print(len(t1))
t2 = ("a",)
print(t2)
print(len(t2))
t3 = "A",t1,"B",(t2,"Z"),"C"
print(t3)
print(t3[3][0])
print(len(t3))
Table 14.9
()
0
('a',)
1
('A', (), 'B', (('a',), 'Z'), 'C')
('a',)
5
Table 14.10
This image was also inserted for the purpose of inserting space between the questions and the answers.
14.5 Answers
14.5.1 Answer 5
True. Note the indexing using double square bracket notation.
Go back to Question 5 (p. 105)
14.5.2 Answer 4
True.
Go back to Question 4 (p. 105)
14.5.3 Answer 3
False. The output is shown in Figure 8 (p. 109) . The code in Figure 6 (p. 105) doesn't recognize the item
in parentheses as a tuple. It is being treated as an ordinary variable. It needs a comma to be recognized as
a tuple.
Table 14.11
14.5.4 Answer 2
True.
Go back to Question 2 (p. 104)
14.5.5 Answer 1
False. The actual output is shown in Figure 3 (p. 109) . While the parentheses are optional when creating
and populating a non-empty tuple, they are not optional when creating an empty tuple.
Table 14.12
14.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1120r-Review
• File: Itse1359-1120r.htm
• Published: 10/19/14
• Revised: 02/23/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1130-Unpacking Tuples 1
15.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
111
112 CHAPTER 15. ITSE1359-1130-UNPACKING TUPLES
15.2.1.1 Figures
• Figure 1 (p. 113) . Output from the code in Listing 1.
• Figure 2 (p. 114) . Output from the code in Listing 2.
• Figure 3 (p. 115) . Output from the code in Listing 3.
• Figure 4 (p. 116) . Output from the code in Listing 4.
• Figure 5 (p. 117) . Visualization of the tuples in the program.
• Figure 6 (p. 119) . Output from the modied program.
15.2.1.2 Listings
• Listing 1 (p. 113) . Beginning of the program.
• Listing 2 (p. 114) . Unpack the tuple and print individual elements.
• Listing 3 (p. 115) . Unpack the tuple into the mutable list.
• Listing 4 (p. 115) . Complete program listing.
15.3 Introduction
This module is part of a series of modules designed to teach you about tuples.
Earlier modules have illustrated
Figure 4 (p. 116) shows the output produced by the program in Listing 4 (p. 115) .
Figure 5 (p. 117) shows a visualization of the tuples in the program after the rst ve statements in the
code block have been executed. This is a case where you need to step through the program and observe
changes in the diagram on the right to appreciate the behavior of the program.
I will explain this program in fragments. Listing 1 (p. 113) shows the beginning of the program.
Table 15.1
Table 15.2
Table 15.3
Figure 2 (p. 114) shows the output produced by the code in Listing 2 (p. 114) .
1
2
A
B
Table 15.4
If you compare this output with the original tuple in Listing 1 (p. 113) , or with the previous output in
Figure 1 (p. 113) , you will see that each of the individual items in the tuple (in left-to-right order) were
assigned respectively to the variables named w , x , y , and z .
Thus, the lines of output produced by printing these four variables in Figure 2 (p. 114) match the items
in the original tuple that was created in Listing 1 (p. 113) and displayed in Figure 1 (p. 113) .
Table 15.5
Then the list is displayed, as shown by the rst line of text in Figure 3 (p. 115) . The rst line of output
in Figure 3 (p. 115) shows the contents of the list just after it is created and populated.
Table 15.6
Then the code in Listing 3 (p. 115) unpacks the four-element tuple referred to by t3 into the rst four
elements in the list. (Remember, a list is mutable, so the values of its items can be changed.)
Then the contents of the list are displayed again producing the second line of text in Figure 3 (p. 115) .
As you can see from Figure 3 (p. 115) , the rst four items in the list were replaced by the four items from
the tuple. The fth item in the list was not modied.
Table 15.7
Figure 4 (p. 116) shows the output from the code in Listing 4 (p. 115) .
Table 15.8
Table 15.9
Everything should work well until the attempt is made to unpack the tuple named t3 and to assign the
items from that tuple into the individual items of the new tuple named L1 .
The items in a tuple are immutable, meaning that they cannot be changed. Therefore, the program
crashes at this point with the error shown in Figure 6 (p. 119) .
15.8 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1130-Unpacking Tuples
• File: Itse1359-1130.htm
• Published: 10/19/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1130r-Review 1
16.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1230-The if
Statement .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
16.3 Questions
16.3.1 Question 1
True or False? The code in Figure 1 (p. 122) produces the output shown in Figure 2 (p. 122) .
121
122 CHAPTER 16. ITSE1359-1130R-REVIEW
if len(dogs) != 2:
print(dogs[0])
print(dogs[1])
print(dogs[2])
print("Length of dogs list = " + str(len(dogs)))
if not(len(dogs) == 3):
print(dogs[0])
print(dogs[1])
print(dogs[2])
print(dogs)
Table 16.1
Affenpinscher
Afgan Hound
Akita
Length of dogs list = 3
['Affenpinscher', 'Afgan Hound', 'Akita']
Table 16.2
16.3.2 Question 2
True or False? The code in Figure 3 (p. 123) produces the output shown in Figure 4 (p. 123) .
weather = ["sunshine","rain"]
weatherToday = weather[1]
if weatherToday == "rain":
print("It's raining, visit the museum.")
else:
print("Sunshine, go to the beach.");
Table 16.3
Table 16.4
16.3.3 Question 3
True or False? The code in Figure 6 (p. 123) produces the output shown in Figure 7 (p. 125) .
weather = ["sunshine","rain","snow"]
weatherToday = weather[2]
if weatherToday == "rain":
print("It's raining, visit the museum.")
elif weatherToday == "sunshine":
print("Sunshine, go to the beach.")
else:
print("It's snowing, go skiing.")
Table 16.5
Table 16.6
This image was also inserted for the purpose of inserting space between the questions and the answers.
16.5 Answers
16.5.1 Answer 3
True.
Go back to Question 3 (p. 123)
16.5.2 Answer 2
False. The actual output is shown in Figure 5 (p. 126) .
Table 16.7
16.5.3 Answer 1
True.
Go back to Question 1 (p. 121)
16.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1230r-Review
• File: Itse1359-1230r.htm
• Published: 10/21/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1210r-Review 1
17.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1210-The While
Loop .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
17.3 Questions
17.3.1 Question 1
True or False? The code in Figure 1 (p. 130) produces the output shown in Figure 2 (p. 130) .
129
130 CHAPTER 17. ITSE1359-1210R-REVIEW
aList = ["a","b","c"]
print(aList)
count = 6
while count <= 5:
count = count + 1
print(count)
aList.append(count)
print(aList)
Table 17.1
Table 17.2
17.3.2 Question 2
True or False? The code in Figure 4 (p. 131) produces the output shown in Figure 5 (p. 131) .
aList = ["a","b","c"]
print(aList)
count = 0
while count <= 5:
count = count + 1
print(count)
aList.append(count)
print(aList)
Table 17.3
Table 17.4
17.3.3 Question 3
True or False? The code in Figure 7 (p. 132) produces the output shown in Figure 8 (p. 132) .
aList = ["a","b","c"]
print(aList)
count = 0
while count <= 5:
count = count + 1
print(count)
aList.append(count)
print(aList)
Table 17.5
Table 17.6
17.3.4 Question 4
True or False? The two statements shown in Figure 9 (p. 132) produce the same result.
Figure 9 . Question 9.
count = count + 1
count += 1
Table 17.7
This image was also inserted for the purpose of inserting space between the questions and the answers.
17.5 Answers
17.5.1 Answer 4
True.
Go back to Question 4 (p. 132)
17.5.2 Answer 3
True.
Go back to Question 3 (p. 131)
17.5.3 Answer 2
False. The output is shown in Figure 6 (p. 134) . The indentation in Figure 4 (p. 131) is not correct for the
output shown in Figure 5 (p. 131) .
Table 17.8
17.5.4 Answer 1
False. The output is shown in Figure 3 (p. 135) . The code in the body of the while loop is never executed
because the conditional clause returns false the rst time it is tested.
Table 17.9
17.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1210r-Review
• File: Itse1359-1210r.htm
• Published: 10/26/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1220-Operators 1
18.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
• numbers
• variables
• identiers
• strings
• scripts
• lists
1 This content is available online at <http://cnx.org/content/m51405/1.3/>.
137
138 CHAPTER 18. ITSE1359-1220-OPERATORS
This instruction has prepared you to use Python as a very fancy desk calculator.
A more recent module provided instruction on the concepts of repetition and decision logic using the
while loop as an example.
• addition
• subtraction
• multiplication
• division
• assignment
However, when examining the while loop, we were confronted with an operator that was less self explanatory
the l ess than or equal operator. This suggests that this is the point in this collection where we need to
describe all, or at least most of the operators used in Python. That is the purpose of this module.
18.2.3.1 Figures
• Figure 1 (p. 139) . Arithmetic operators.
• Figure 2 (p. 139) . Relational operators.
• Figure 3 (p. 140) . Logical operators.
• Figure 4 (p. 141) . Bit shift operators.
• Figure 5 (p. 141) . Miscellaneous operators.
18.2.3.2 Listings
• Listing 1 (p. 140) . Logical operators "and", "or", and "not".
18.3 Operators
The material on operators contained in this module was mainly extracted from 10.3.1. Mapping Operators
to Functions 2 in The Python Standard Library 3 .
2 https://docs.python.org/3/library/operator.html#mapping-operators-to-functions
3 https://docs.python.org/3/library/index.html
+ Addition a + b
+ Positive + a (does essentially nothing)
- Subtraction a - b
- Negation - a (change sign of operand)
* Multiplication a * b
** Exponentiation a ** b
/ Division a / b (floating point division)
// Division a // b (integer division)
% Modulo a % b
Table 18.1
With the exception of (//) and (%), the behavior of all of the operators in Figure 1 (p. 139) should be
self-explanatory. The behavior of those two operators was explained in the earlier module titled Itse1359-
1020-Numbers .
Table 18.2
Table 18.3
The operators in Figure 3 (p. 140) are often used to embellish the relational operators from Figure 2 (p.
139) in decision logic.
The rst operator ( and ) returns true if both operands are true.
The second operator ( or ) returns true if either operand is true.
The third operator ( not ) has only one operand and it is on the right side of the operator. It switches
the operand from true to false or from false to true.
The script in Listing 1 (p. 140) shows examples of the use of these three operators in conjunction with
the equality relational operator.
a=5
print(a==5 and a==5)
print(a==5 and a==4)
print(a==5 and not a==5)
print(a==5 and not a==4)
print(a==5 or a==5)
print(a==5 or a==4)
print(a==4 or a==4)
Table 18.4
The last four operators in Figure 3 (p. 140) are used to perform logical operations at the bit level. An
explanation of these operators is beyond the scope of this module.
Left Shift a b
Right Shift a b
Table 18.5
Table 18.6
18.5 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1220-Operators
• File: Itse1359-1220.htm
• Published: 10/26/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1220r-Review 1
19.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1220-Operators.
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
19.3 Questions
19.3.1 Question 1
True or False? The code in Figure 1 (p. 143) produces the output shown in Figure 2 (p. 144) .
143
144 CHAPTER 19. ITSE1359-1220R-REVIEW
count = 0
while count <= 5 and 3*count//2 <=5:
count = count + 1
print(count)
print(count)
Table 19.1
1
2
3
4
4
Table 19.2
19.3.2 Question 2
True or False? The code in Figure 3 (p. 144) produces the output shown in Figure 4 (p. 145) .
count = 0
while count < 5 or 2*count+1 < 15:
count = count + 1
print(count)
print(count)
Table 19.3
1
2
3
4
5
6
7
7
Table 19.4
19.3.3 Question 3
True or False? The code in Figure 5 (p. 145) produces the output shown in Figure 6 (p. 145) .
count = 0
while not(count >= 5 and 2*count+1 >= 15):
count = count + 1
print(count)
print(count)
Table 19.5
1
2
3
4
5
6
7
8
8
Table 19.6
This image was also inserted for the purpose of inserting space between the questions and the answers.
19.5 Answers
19.5.1 Answer 3
False. The actual output is shown in Figure 7 (p. 147) .
1
2
3
4
5
6
7
7
Table 19.7
19.5.2 Answer 2
True.
Go back to Question 2 (p. 144)
19.5.3 Answer 1
True.
Go back to Question 1 (p. 143)
19.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1220r-Review
• File: Itse1359-1220r.htm
• Published: 10/26/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1230r-Review 1
20.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1230-The if
Statement .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
20.3 Questions
20.3.1 Question 1
True or False? The code in Image 1 (p. 150) produces the output shown in Image 2 (p. 150) .
149
150 CHAPTER 20. ITSE1359-1230R-REVIEW
if len(dogs) != 2:
print(dogs[0])
print(dogs[1])
print(dogs[2])
print("Length of dogs list = " + str(len(dogs)))
if not(len(dogs) == 3):
print(dogs[0])
print(dogs[1])
print(dogs[2])
print(dogs)
Table 20.1
Affenpinscher
Afgan Hound
Akita
Length of dogs list = 3
['Affenpinscher', 'Afgan Hound', 'Akita']
Table 20.2
20.3.2 Question 2
True or False? The code in Image 3 (p. 151) produces the output shown in Image 4 (p. 151) .
weather = ["sunshine","rain"]
weatherToday = weather[1]
if weatherToday == "rain":
print("It's raining, visit the museum.")
else:
print("Sunshine, go to the beach.");
Table 20.3
Table 20.4
20.3.3 Question 3
True or False? The code in Image 6 (p. 151) produces the output shown in Image 7 (p. 153) .
weather = ["sunshine","rain","snow"]
weatherToday = weather[2]
if weatherToday == "rain":
print("It's raining, visit the museum.")
elif weatherToday == "sunshine":
print("Sunshine, go to the beach.")
else:
print("It's snowing, go skiing.")
Table 20.5
Table 20.6
This image was also inserted for the purpose of inserting space between the questions and the answers.
20.5 Answers
20.5.1 Answer 3
True.
Go back to Question 3 (p. 151)
20.5.2 Answer 2
False. The actual output is shown in Image 5 (p. 154) .
Table 20.7
20.5.3 Answer 1
True.
Go back to Question 1 (p. 149)
20.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1230r-Review
• File: Itse1359-1230r.htm
• Published: 10/21/14
• Revised: 12/28/14
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1240r-Review 1
21.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1240-The for
Loop .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
21.3 Questions
21.3.1 Question 1
True or False? The code in Figure 1 (p. 158) produces the output shown in Figure 2 (p. 158) .
157
158 CHAPTER 21. ITSE1359-1240R-REVIEW
Table 21.1
Akita
Afgan Hound
Affenpinscher
Length of dogs list = 3
A
k
i
t
a
Akita
Table 21.2
21.3.2 Question 2
True or False? The code in Figure 4 (p. 159) produces the output shown in Figure 5 (p. 159) .
breed = "Dachshund"
for index in range(2,len(breed),3):
print("Letter at index " + str(index) + " is " + breed[index])
print(breed)
sum = 0
for cnt in range(5):
sum += 2*cnt
print("cnt = " + str(cnt) + ", sum = " + str(sum))
Table 21.3
Letter at index 2 is c
Letter at index 5 is h
Letter at index 8 is d
Dachshund
cnt = 0, sum = 0
cnt = 1, sum = 2
cnt = 2, sum = 6
cnt = 3, sum = 12
cnt = 4, sum = 20
Table 21.4
This image was also inserted for the purpose of inserting space between the questions and the answers.
21.5 Answers
21.5.1 Answer 2
True.
Go back to Question 2 (p. 158)
21.5.2 Answer 1
False. The actual output is shown in Figure 3 (p. 161) .
Affenpinscher
Afgan Hound
Akita
Length of dogs list = 3
A
k
i
t
a
Akita
Table 21.5
21.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1240r-Review
• File: Itse1359-1240r.htm
• Published: 10/21/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1250-Nested Loops 1
22.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
163
164 CHAPTER 22. ITSE1359-1250-NESTED LOOPS
(Note to blind and visually impaired students: most of the Figures and all of the Listings in this module
are presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
22.2.3.1 Figures
• Figure 1 (p. 164) . Syntax for nested while loops.
• Figure 2 (p. 164) . Syntax for nested for loops
• Figure 3 (p. 166) . Output from the code in Listing 1.
22.2.3.2 Listings
• Listing 1 (p. 165) . An example of nested while and for loops.
while expression:
while expression:
while expression:
statement(s)
statement(s)
statement(s)
Table 22.1
Figure 2 (p. 164) shows the general syntax of nested for loops.
Table 22.2
The syntax can become more complicated than that shown in either Figure 1 (p. 164) or Figure 2 (p.
164) . This is particularly true when you mix while loops and for loops in a nested construct.
leftDigit = 0
rightDigit = 0
leftDigit += 1
#end outer while loop
Table 22.3
Figure 3 (p. 166) shows the output from the code in Listing 1 (p. 165) . Note that the blank lines were
inserted to make the material in Figure 3 (p. 166) easier to read.
0-0-0
0-0-1
0-0-2
0-1-0
0-1-1
0-1-2
0-2-0
0-2-1
0-2-2
1-0-0
1-0-1
1-0-2
1-1-0
1-1-1
1-1-2
1-2-0
1-2-1
1-2-2
2-0-0
2-0-1
2-0-2
2-1-0
2-1-1
2-1-2
2-2-0
2-2-1
2-2-2
Table 22.4
This program simulates a scaled-down version of the odometer in your car. This odometer, however,
only has three digits and each digit is limited to the values of 0, 1, and 2. In other words, Each digit of the
odometer rolls over to 0 when its value exceeds 2.
The program begins by creating two working variables named leftDigit and rightDigit and initializing
their values to zero. These are essentially counter variables for the outermost while loop and the innermost
while loop in Listing 1 (p. 165) . They are also the left-most and right-most digits shown in the columns
of digits in Figure 3 (p. 166) .
It isn't necessary to create a separate working variable for the middle digit. That variable, named
middleDigit is created as part of the for loop statement in Listing 1 (p. 165) .
If you compare the code in Listing 1 (p. 165) with the output in Figure 3 (p. 166) , you will see that the
for loop goes through its full range of iterations (3) for each iteration of the outermost while loop.
You will also see that the innermost while loop goes through its full range of iterations (3) for every
iteration of the for loop. As a result, the innermost while loop goes through nine iterations for every
iteration of the outermost while loop.
When you think about the behavior of nested loops, think of an odometer. That may help you to
remember how they behave.
22.7 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1250-Nested Loops
• File: Itse1359-1250.htm
• Published: 10/26/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
2 http://pythontutor.com/visualize.html#mode=display
Itse1359-1250r-Review 1
23.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1250-Nested
Loops .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
23.3 Questions
23.3.1 Question 1
True or False? The code in Figure 1 (p. 170) produces the output shown in Figure 2 (p. 171) .
169
170 CHAPTER 23. ITSE1359-1250R-REVIEW
leftDigit = 0
rightDigit = 0
leftDigit += 1
#end outer while loop
Table 23.1
0-0-0
0-0-1
0-0-2
0-1-0
0-1-1
0-1-2
0-2-0
0-2-1
0-2-2
1-0-0
1-0-1
1-0-2
1-1-0
1-1-1
1-1-2
1-2-0
1-2-1
1-2-2
2-0-0
2-0-1
2-0-2
2-1-0
2-1-1
2-1-2
2-2-0
2-2-1
2-2-2
Table 23.2
These images were inserted here simply to insert some space between the questions and the answers to
keep them from being visible on the screen at the same time.
This image was also inserted for the purpose of inserting space between the questions and the answers.
23.5 Answers
23.5.1 Answer 1
False. The actual output is shown in Figure 3 (p. 174) .
0-3-0
0-3-1
0-3-2
0-2-0
0-2-1
0-2-2
0-1-0
0-1-1
0-1-2
1-3-0
1-3-1
1-3-2
1-2-0
1-2-1
1-2-2
1-1-0
1-1-1
1-1-2
2-3-0
2-3-1
2-3-2
2-2-0
2-2-1
2-2-2
2-1-0
2-1-1
2-1-2
Table 23.3
23.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1250r-Review
• File: Itse1359-1250r-Review.htm
• Published: 10/26/14
• Revised: 02/25/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1260-Loop Modiers 1
24.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
177
178 CHAPTER 24. ITSE1359-1260-LOOP MODIFIERS
Finally, this module will make a brief mention of the pass statement, which does nothing. If you need
to write code that does nothing, the pass statement is for you.
24.2.3.1 Figures
• Figure 1 (p. 180) . Output from the code in Listing 1.
• Figure 2 (p. 183) . Output from the code in Listing 2.
• Figure 3 (p. 186) . Output from the code in Listing 3.
24.2.3.2 Listings
• Listing 1 (p. 179) . Loops with else clauses.
• Listing 2 (p. 182) . Nested loops with a continue statement.
• Listing 3 (p. 185) . Nested loops with a break statement.
2 http://cnx.org/contents/34d000fd-1e35-482b-b8d5-96b32a552b48
leftDigit = 0
rightDigit = 0
else:
print("In else clause on for loop")
#end for loop with else clause
else:
print("In else clause on outer while loop")
#end outer while loop with else clause
print("Goodbye")
Table 24.1
The output from the code in Listing 1 (p. 179) is shown in Figure 1 (p. 180) .
1-1-1
1-1-2
1-1-3
In else clause on inner while loop
1-2-1
1-2-2
1-2-3
In else clause on inner while loop
In else clause on for loop
2-1-1
2-1-2
2-1-3
In else clause on inner while loop
2-2-1
2-2-2
2-2-3
In else clause on inner while loop
In else clause on for loop
In else clause on outer while loop
Goodbye
Table 24.2
You should already be familiar with most of the code in Listing 1 (p. 179) . Some of the statements have
been rearranged relative to the odometer code in the earlier module and some of the limits in the conditions
have been reduced. However, that was done simply to shorten and to improve the clarity of the printed
output and is of no technical consequence.
The only thing that is new in Listing 1 (p. 179) is the addition of an else clause to each of the loops.
Note that the indentation level of the word else in each case is the same as the indentation level for the
corresponding words while and for . For example, the last else clause in Listing 1 (p. 179) belongs to
the outermost while loop that begins with the word while near the top of Listing 1 (p. 179) .
In this program, each of the else clauses simply prints a message indicating that it is being executed.
Obviously, more substantive code could be placed in the else clause in a more signicant program.
The innermost while loop in this program is executed twice for each iteration of its enclosing for loop.
Similarly, the for loop is executed twice for each iteration of its enclosing while loop. The outermost
while loop is executed once, the for loop is executed twice, and the innermost while loop is executed
four times.
The else clause for a loop is executed each time the loop terminates (with the exception of break (p.
183) as mentioned earlier) . As you can see in Figure 1 (p. 180) , the else clause on the innermost while
loop was executed four times, the else clause on the for loop was executed two times, and the else
clause on the outermost while loop was executed once.
the left, the diagram on the right, and the printed material at the bottom. That should help you to better
understand the behavior of loops with else clauses.
(Be careful that the code that updates the condition in a while loop is not bypassed by a continue
statement. If you do, you will end up with an innite loop.)
The use of a continue statement is illustrated in Listing 2 (p. 182) , which shows an if statement (with
a continue statement in the body of the if statement) inserted into the innermost while loop of
the program from Listing 1 (p. 179) .
leftDigit = 0
rightDigit = 0
else:
print("In else clause on for loop")
#end for loop with else clause
else:
print("In else clause on outer while loop")
#end outer while loop with else clause
print("Goodbye")
Table 24.3
Figure 2 (p. 183) shows the output produced by the code in Listing 2 (p. 182) . You should compare
this output with the output shown in Figure 1 (p. 180) .
1-1-1
continue
1-1-3
In else clause on inner while loop
1-2-1
continue
1-2-3
In else clause on inner while loop
In else clause on for loop
2-1-1
continue
2-1-3
In else clause on inner while loop
2-2-1
continue
2-2-3
In else clause on inner while loop
In else clause on for loop
In else clause on outer while loop
Goodbye
Table 24.4
Once during each iteration of the while loop in Listing 2 (p. 182) , a test is made to determine if
the condition variable, rightDigit , is equal to 2. If so, the word continue is printed and a continue
statement is executed.
The execution of the continue statement causes the current iteration of the loop to terminate and the
next iteration to begin. This, in turn causes the print statement following the if statement to be skipped
and the word continue to be printed in its place. You can easily see where this happens by comparing the
output in Figure 2 (p. 183) with the output in Figure 1 (p. 180) .
leftDigit = 0
rightDigit = 0
else:
print("In else clause on for loop")
#end for loop with else clause
else:
print("In else clause on outer while loop")
#end outer while loop with else clause
print("Goodbye")
Table 24.5
The output from the code in Listing 3 (p. 185) is shown in Figure 3 (p. 186) . You should compare this
with the output shown in Figure 2 (p. 183) .
1-1-1
break
1-2-1
break
In else clause on for loop
2-1-1
break
2-2-1
break
In else clause on for loop
In else clause on outer while loop
Goodbye
Table 24.6
The output for the continue statement in Figure 2 (p. 183) shows that only one iteration of the
innermost while loop was impacted by the if statement and the continue statement in its body.
The output shown in Figure 3 (p. 186) shows that once the break statement was executed, no further
iterations of the innermost while loop were executed. In other words, execution of the break statement
terminated the loop in its entirety.
Figure 3 (p. 186) also shows that termination caused by a break statement also prevented the execution
of the code in the else clause for the innermost while loop as indicated earlier (p. 178) ..
The innermost while loop is still executed twice for each iteration of its enclosing for loop. However,
the while loop terminates part of the way through the second iteration and the printed output that would
be produced by the second and third iterations (see Listing 3 (p. 185) ) is missing from the output shown
in Figure 3 (p. 186) .
I also recommend that you create and experiment with visualizations 6 for the code in Listing 1 (p. 179)
, Listing 2 (p. 182) , and Listing 3 (p. 185) .
24.5 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1260-Loop Modiers
• File: Itse1359-1260.htm
• Published: 10/26/14
• Revised: 09/05/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
6 http://pythontutor.com/visualize.html#mode=edit
Itse1359-1280-Function Arguments 1
25.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
189
190 CHAPTER 25. ITSE1359-1280-FUNCTION ARGUMENTS
• required arguments,
• default arguments,
• keyword arguments, and
• variable-length arguments.
25.2.3.1 Figures
• Figure 1 (p. 193) . Output from the code in Listing 1.
• Figure 2 (p. 194) . Visualization of the code in Listing 1.
• Figure 3 (p. 196) . Output from the code in Listing 2.
• Figure 4 (p. 198) . Output produced by the code in Listing 3.
• Figure 5 (p. 201) . Output produced by the code in Listing 4.
• Figure 6 (p. 203) . Output produced by the code in Listing 5.
• Figure 7 (p. 204) . Visualization of variable-length arguments.
25.2.3.2 Listings
• Listing 1 (p. 192) . A list-modier function.
• Listing 2 (p. 195) . Another list-modier function.
• Listing 3 (p. 197) . Illustration of default arguments.
• Listing 4 (p. 200) . Illustration of keyword arguments.
• Listing 5 (p. 202) . Illustration of variable-length arguments.
According to tutorialspoint Python Functions 5 , all parameters in Python are passed by reference .
On the other hand, according to The Python Tutorial Dening Functions 6 ,
"thus, arguments are passed using call by value (where the value is always an object reference,
not the value of the object)."
In this case, I come down on the side of call by value . I think it is more correct to say that parameters
are passed by value and as a result, the function receives copies of references to objects. Even though I can
use a copy of an object's reference to modify the object, I cannot use a copy of that reference to cause the
original reference to point to a dierent object. I can modify the object to which the reference points, but I
cannot modify the reference itself.
I will illustrate what I mean by this in conjunction with the program in Listing 1 (p. 192) .
• Required arguments
• Default arguments
• Keyword arguments
• Variable-length arguments
5 http://www.tutorialspoint.com/python/python_functions.htm
6 https://docs.python.org/3/tutorial/controlow.html#dening-functions
def listModifier(listA,listB):
"""Illustrates pass by value or reference"""
print("In listModifier")
return
#End function definition
print("Call listModifier")
listModifier(aList,bList)
print("Back from listModifier")
print("aList = " + str(aList))
print("bList = " + str(bList))
Table 25.1
Figure 1 (p. 193) shows the output produced by the code in Listing 1 (p. 192) .
Table 25.2
I recommend that you create a visualization 8 for the code in Listing 1 (p. 192) and step through the
program one instruction at a time. As you do that, pay attention to the movements of the red and green
arrows on the left, the diagram on the right, and the printed material at the bottom. That should help you
to better understand the concept of "passing by value."
7 http://pythontutor.com/visualize.html#mode=display
8 http://pythontutor.com/visualize.html#mode=display
def listModifier(listA,listB):
"""Illustrates pass by value or reference"""
print("In listModifier")
return listB
#End function definition
print("Call listModifier")
bList = listModifier(aList,bList)
print("Back from listModifier")
print("aList = " + str(aList))
print("bList = " + str(bList))
Table 25.3
Figure 3 (p. 196) shows the output from the code in Listing 2 (p. 195) .
Table 25.4
Listing 2 (p. 195) has two major modications relative to Listing 1 (p. 192) . First, the function returns
a copy of the reference to the new list instead of returning nothing. Second, the return value is assigned to
the variable named bList . This does cause the variable named bList to point to a dierent list object.
This is conrmed by the last line of text in Figure 3 (p. 196) .
Conclusion function parameters in Python are passed by value, but the copies that are passed can often
be used to access and modify the objects pointed to by the original reference variables of which they are
copies.
In this program, the function's second argument is of no consequence. The function and the program
would behave the same if the second argument were eliminated entirely. I left it in for illustration purposes
only.
Hopefully you can study these two programs along with the program output and the above discussion to
understand the basic behavior of Python function arguments.
When the calling program ignores default arguments, the dened default values for those arguments are
used by the code in the body of the function.
The program shown in Listing 3 (p. 197) illustrates a function with one required argument ( listA )
and three default arguments ( listB , listC , and listD ).
def listModifier(listA,listB=["B"],listC=["C"],listD=["D"]):
"""Illustrates default arguments"""
print("In listModifier")
listA.append(1.00001)
print("listA = " + str(listA))
listB.append(2.00002)
print("listB = " + str(listB))
listC.append(3.00003)
print("listC = " + str(listC))
listD.append(4.00004)
print("listD = " + str(listD))
return
#End function definition
aList = ["ab","cd","ef"]
bList = ["The","old","list"]
cList = ["This old house"]
dList = ["is falling down"]
print("Call listModifier")
listModifier(aList,bList)
print("Back from listModifier")
Table 25.5
Figure 4 (p. 198) shows the output produced by the code in Listing 3 (p. 197) .
Table 25.6
The rst thing that you should pay attention to is the syntax for dening default arguments in the
function named listModier . (You will see later that this same syntax is used for dening
keyword arguments .)
The second thing you should pay attention to is the syntax used to call the function named listModier
about six lines up from the bottom of Listing 3 (p. 197) . The function call passes only two parameters to
the function: aList and bList .
The function treats the rst parameter as satisfying the required argument named listA . The function
treats the second parameter as satisfying the default argument named ListB , replacing the default value
with the parameter that is actually received. Not receiving any more incoming parameters, the function
processes the default values for listC and listD .
Hopefully this explanation along with the code in Listing 3 (p. 197) and the output in Figure 4 (p. 198)
will tell you what you need to know about using default function arguments.
the syntax shown in Listing 4 (p. 200) . (I told you earlier that the syntax for dening default arguments
is the same as the syntax for dening keyword arguments.)
The calling program can ignore none, some, or all of the keyword arguments when calling the function.
By this I mean that the calling program can simply not pass parameters for keyword arguments. Parameters
that are passed for keyword arguments can be passed in any order. In other words, unlike default arguments,
the calling program can ignore a keyword argument in the middle of a group of keyword arguments.
As with default arguments, when the calling program ignores keyword arguments, the dened default
values for those arguments are used by the code in the body of the function.
The program shown in Listing 4 (p. 200) illustrates a function with one required argument ( listA )
and three keyword arguments ( listB , listC , and listD ).
def listModifier(listA,listB=["B"],listC=["C"],listD=["D"]):
"""Illustrates keyword arguments"""
print("In listModifier")
listA.append(1.00001)
print("listA = " + str(listA))
listB.append(2.00002)
print("listB = " + str(listB))
listC.append(3.00003)
print("listC = " + str(listC))
listD.append(4.00004)
print("listD = " + str(listD))
return
#End function definition
aList = ["ab","cd","ef"]
bList = ["The","old","list"]
cList = ["This old house"]
dList = ["is falling down"]
print("Call listModifier")
listModifier(aList,listD=dList,listB=bList)
print("Back from listModifier")
Table 25.7
The code in Listing 4 (p. 200) produces the output shown in Figure 5 (p. 201) .
Table 25.8
As before, the rst thing that you should pay attention to is the syntax for dening keyword arguments
in the function named listModier in Listing 4 (p. 200) . If you compare it with the argument list for
the function with the same name in Listing 3 (p. 197) , you will see that there are exactly the same
. Therefore, the manner in which the arguments are dened does not distinguish default arguments from
keyword arguments. The distinguishing factor is the syntax with which the function is called.
The second thing you should pay attention to is the syntax used to call the function named listModier
about six lines up from the bottom of Listing 4 (p. 200) . The function call passes three parameters: aList
, dList , and bList .
The reference variable named aList is passed as the rst parameter to satisfy the required argument
named listA . Note that it is passed using the same syntax as in Listing 1 (p. 192) , Listing 2 (p. 195)
, and Listing 3 (p. 197) . Because it is a required argument, it must be passed in the correct order in a
positional sense.
The syntax for passing dList and bList however is signicantly dierent from the previous examples.
What you see is something closely akin to an assignment statement. In other words, the parameter named
dList is assigned to the argument named listD . Also, the parameter named bList is assigned to the
argument named listB . Nothing is passed and assigned to the argument named listC . Furthermore,
the two parameters that are passed through assignment to the named arguments are passed in reverse order
relative to the denition of those arguments in the function denition.
Hopefully this explanation along with the code in Listing 4 (p. 200) and the output in Figure 5 (p. 201)
will tell you what you need to know about using keyword function arguments.
I also recommend that you create a visualization 9 for the code in Listing 4 (p. 200) and step through
the program one instruction at a time. As you do that, pay attention to the movements of the red and green
arrows on the left, the diagram on the right, and the printed material at the bottom. That should help you
to better understand the concept of keyword arguments.
def listModifier(listA,*wxyz):
"""Illustrates variable-length arguments"""
print("In listModifier")
aList = ["ab","cd","ef"]
bList = ["The","old","list"]
cList = ["This old house"]
dList = ["is falling down"]
print("Call listModifier")
listModifier(aList,bList,cList,dList)
print("Back from listModifier")
Table 25.9
The code in Listing 5 (p. 202) produces the output shown in Figure 6 (p. 203) .
Table 25.10
The rst thing that you should pay attention to is the syntax for dening variable-length arguments in
the function named listModier in Listing 5 (p. 202) . The syntax consists of an asterisk (*) followed by
an arbitrary argument name. As mentioned earlier, the function denition in Listing 5 (p. 202) consists of a
required argument ( listA ) followed by the syntax for a variable-length group of arguments ( *wxyz ).
In this program, the calling program must pass a parameter for the required argument and can pass an
arbitrary number of parameters following the required argument.
The second thing you should pay attention to is the syntax used to call the function named listModier
about six lines up from the bottom of Listing 5 (p. 202) . The function call passes four parameters: aList
, bList, cList , and dList , each of which is a reference to a list object.
The reference variable named aList is passed as the rst parameter to satisfy the required argument
named listA . Note that it is passed using the same syntax as in Listing 1 (p. 192) , Listing 2 (p. 195)
, Listing 3 (p. 197) and Listing 4 (p. 200) . Because it is a required argument, it must be passed in the
correct order in a positional sense.
The remaining three parameters are passed as variable-length arguments. The interpreter wraps them
in a tuple and presents the tuple to the code in the body of the function. (You learned about tuples in the
earlier module titled Itse1359-1100-Indexing and Slicing Tuples 10 and several modules following that one.)
The code in the body of the function uses the parameter passed as the required argument to append a
numeric value to the list referred to by that parameter.
Although a tuple is immutable, the list objects referred to by the elements in the tuple are mutable. A
for loop in the functions iterates from the beginning to the end of the tuple, extracting the references to
the lists and appending an increasing numeric value to the end of each list. This is shown in the last four
lines of text in Figure 6 (p. 203) .
Note that if the order in which the function processes the arguments is important, the order in which the
calling program passes the parameters must match that order because that is the order in which they will
be wrapped in the tuple.
Hopefully this explanation along with the code in Listing 5 (p. 202) and the output in Figure 6 (p.
203) will tell you what you need to know about using variable-length function arguments. I do want to
caution again, however, that this module does not provide a complete treatment of function arguments. For
a more compete treatment, I will refer you to The Python Tutorial More on Dening Functions 11 and
tutorialspoint Python Functions 12 .
10 http://cnx.org/contents/59b92f58-fa74-4624-a37a-37ab2794536a
11 https://docs.python.org/3/tutorial/controlow.html#more-on-dening-functions
12 http://www.tutorialspoint.com/python/python_functions.htm
I recommend that you create a visualization 15 for the code in Listing 5 (p. 202) and step through the
program one instruction at a time. As you do that, pay attention to the movements of the red and green
arrows on the left, the diagram on the right, and the printed material at the bottom. That should help you
to better understand the concept of variable-length arguments.
I also recommend that you create visualizations 16 for the code in Listing 1 (p. 192) through Listing
5 (p. 202) . Step through the programs one instruction at a time. As you do that, pay attention to the
movements of the red and green arrows on the left, the diagram on the right, and the printed material at
the bottom. That should help you to better understand the concepts embodied in those sample programs.
25.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1280-Function Arguments
• File: Itse1359-1280.htm
• Published: 10/26/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
16 http://pythontutor.com/visualize.html#mode=display
Itse1359-1410-Overview of Python
classes 1
26.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
26.2.3.1 Figures
• Figure 1 (p. 216) . Output from the code in Listing 2.
• Figure 2 (p. 216) . Output from the code in Listing 3.
• Figure 3 (p. 217) . Output from the code in Listing 4.
• Figure 4 (p. 218) . Output from the code in Listing 5.
• Figure 5 (p. 221) . Visualizing a class denition.
• Figure 6 (p. 223) . Output from the program in Listing 9.
26.2.3.2 Listings
• Listing 1 (p. 215) . Manufacture a three-button radio.
• Listing 2 (p. 215) . Scan for available stations.
• Listing 3 (p. 216) . Program the buttons on radio01.
• Listing 4 (p. 217) . Play the three programmed stations.
• Listing 5 (p. 218) . Manufacture another 3-button radio.
26.3 Preview
This module will concentrate primarily on a discussion of the Python class and objects created using Python
classes.
In order to relate object-oriented programming to the real world, a car radio will be used to illustrate and
discuss several aspects of software objects. For example, you will learn that car radios, as well as software
objects, have the ability to store data, along with the ability to modify or manipulate that data. You will
learn that car radios, as well as software objects, have the ability to accept messages and to perform an
action, modify their state, return a value, or some combination of the above.
A simple Python program will be presented and explained to illustrate the denition and use of a Python
class. This class simulates the manufacture, programming, and use of a car radio.
You will see the denition of a class named Radio . You will see how to write code that simulates
pressing the scan button on the radio to learn about available radio stations in the area. You will see how
to write code that simulates the association of a radio button with a particular radio station. You will see
how to write code that simulates the pressing of a radio button to play the radio station associated with
that button.
You will see how to write code to create new Radio objects. You will also see how to save these object's
reference in reference variables and how to use those variables to exercise the Radio objects.
You will learn some of the jargon used in OOP, including persistence, state, messages, methods, and
behaviors. You will learn where objects come from, and you will learn that a class is a set of plans that can
be used to construct objects.
You will learn that a Python object is an instance of a class. You will see Python code, used to create two
objects from the same class, and then to send messages to each of those objects (call methods on objects) .
Note that unlike Java and C++, once a Python object is instantiated, it can be modied in ways
that no longer follow the blueprint of the class through the addition of new data members.
An object's behavior is controlled by methods dened within the class. In Python, methods are functions
that are dened within a class denition.
An object is said to have state and behavior . At any instant in time, the state of an object is
determined by the values stored in its variables and its behavior is determined by its methods.
Most modern car radios are much more complex than the one that we will use as an analogy in
this module.
When the radio beeps, I know that the new frequency value has been stored in a storage location that
corresponds to that particular button.
What I have done in this process is to send a message to the radio object asking it to change its state .
The beep that I hear could be interpreted as the radio object returning a value back to me indicating that
the mission has been accomplished. (Alternately, we might say that the radio object sent a message back
to me.)
• perform an action,
• change its state,
• return a value, or
After following this procedure to program a button, when I press that button (send a message) , the radio
object will automatically tune itself to that frequency.
I live in Austin, TX. If I drive to Dallas and press a button that I have associated with a particular
radio station in Austin, I will probably hear static. In that case, I may want to change the frequency value
associated with that button. I can follow the same procedure described earlier to set the frequency value
associated with that button to correspond to one of the radio stations in Dallas. (Again, I would be sending
a message to the radio object asking it to change its state.)
26.5.1.5 Jargon
As you can see from the above discussion, the world of OOP is awash with jargon, and the ability to
translate the jargon is essential to an understanding of the published material on OOP. Therefore, as we
progress through this series of modules, I will introduce you to some of that jargon and try to help you
understand the meaning of the jargon.
26.5.1.5.1 Persistence
The ability of your car radio to remember your list of favorite stations is often referred to as persistence. An
object that has the ability to store and remember values is often said to have persistence.
26.5.1.5.2 State
It is often said that the state of an object at a particular point in time is determined by the values stored
in the object. In our analogy, even if we own identical radios, unless the two of us have the same list of
favorite radio stations, associated with the same combination of buttons, the state of your radio object at
any particular point in time will be dierent from the state of my radio object.
It is perfectly OK for the two of us to own identical radios and to cause the two radio objects to contain
the same list of frequencies. Even if two objects have the same state at the same time, they are still separate
and distinct objects. While this is obvious in the real world of car radios, it may not be quite as obvious in
the virtual world of computer programming.
radio01 = Radio()
Table 26.1
As a practical matter, the code required to instantiate a Python object looks just like the code required
to call a function having the same name as the class from which the object is being instantiated. In my
opinion, this causes Python to be a little less readable than Java, which uses a special syntax to create an
object.
Table 26.2
As mentioned above, Figure 1 (p. 216) shows the output produced by the code in Listing 2 (p. 215) .
Table 26.3
Table 26.4
The output produced by the code in Listing 3 (p. 216) is shown in Figure 2 (p. 216) .
Table 26.5
Table 26.6
Figure 3 (p. 217) shows the output produced by the code in Listing 4 (p. 217) .
Table 26.7
Table 26.8
Figure 4 (p. 218) shows the output produced by the code in Listing 5 (p. 218) .
Table 26.9
class Radio(object):
#This class provides the plans from which the radio objects are built.
stations = {"Austin":{91.7:"KVRX",95.5:"KKMJ",98.1:"KVET",93.7:"KLBJ"},
"Dallas":{98.3:"KNON",91.7:"KKXT",97.9:"KBFB",98.7:"KLUV"}
}
Table 26.10
def __init__(self):
self.stationNumber = [0,0,0]
Table 26.11
According to The Python Tutorial Class and Instance Variables 5 , variables that are created
in this manner (inside class methods) are called instance variables and are "for data unique
to each instance (object)."
Therefore, the variable named stations from Listing 6 (p. 219) is shared among radio01 and radio02
in Listing 9 (p. 222) . However, radio01 and radio02 each has its own copy of the variable named
stationNumber from Listing 7 (p. 219) and those variables are not shared among objects.
def scan(self,city):
return self.stations[city]
def setStationNumber(self,index,station):
self.stationNumber[index-1] = station
def playStation(self,index):
print("Playing " + self.stationNumber[index-1])
Table 26.12
The analogy to a physical car radio breaks down with respect to the class variable named
stations . Physical car radios don't share any data. You could say that data stored in physical
car radios is stored in instance variables only.
5 https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables
I recommend that you create a visualization 7 for the code in Listing 9 (p. 222) and step through the
program one instruction at a time. As you do that, pay attention to the movements of the red and green
arrows on the left, the diagram on the right, and the printed material at the bottom. That should help you
to better understand the concept of classes and objects in Python.
observing the results of your changes. Make certain that you can explain why your changes behave as they
do.
def __init__(self):
self.stationNumber = [0,0,0]
def scan(self,city):
return self.stations[city]
def setStationNumber(self,index,station):
self.stationNumber[index-1] = station
def playStation(self,index):
print("Playing " + self.stationNumber[index-1])
Table 26.13
26.9 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1410-Overview of Python classes
• File: Itse1359-1410.htm
• Published: 10/27/14
• Revised: 02/08/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1410r-Review 1
27.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1410-Overview
of Python classes .
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
27.3 Questions
27.3.1 Question 1
True or False? The code in Figure 1 (p. 226) produces the output shown in Figure 2 (p. 227) except that
the order of the items in the dictionaries may be dierent from one run to the next.
225
226 CHAPTER 27. ITSE1359-1410R-REVIEW
class MyNewClass(object):
myDict = {"A":{100:"M",200:"N",300:"P",400:"Q"},
"B":{500:"S",600:"R",700:"U",800:"T"}
}
def __init__(self):
self.itemNumber = [0,0,0]
def scan(self,group):
return self.myDict[group]
def setItemNumber(self,index,val):
self.itemNumber[index-1] = val
def displayItem(self,index):
print("Showing " + self.itemNumber[index-1])
group01 = MyNewClass()
group01Items = group01.scan("A")
print("Available items in 'A'")
print(group01Items)
group01.setItemNumber(1,group01Items[200])
group01.setItemNumber(2,group01Items[100])
group01.setItemNumber(3,group01Items[300])
group01.displayItem(3)
group01.displayItem(2)
group01.displayItem(1)
group02 = MyNewClass()
group02Items = group02.scan("B")
print("Available items in 'B'")
print(group02Items)
group02.setItemNumber(1,group02Items[600])
group02.setItemNumber(2,group02Items[700])
group02.setItemNumber(3,group02Items[500])
group02.displayItem(3)
group02.displayItem(2)
group02.displayItem(1)
Table 27.1
Table 27.2
This image was also inserted for the purpose of inserting space between the questions and the answers.
27.5 Answers
27.5.1 Answer 1
True.
Go back to Question 1 (p. 225)
27.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1410r-Review
• File: Itse1359-1410r-Review.htm
• Published: 10/27/14
• Revised: 03/02/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1450-Inheritance 1
28.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
231
232 CHAPTER 28. ITSE1359-1450-INHERITANCE
28.2.3.1 Figures
• Figure 1 (p. 235) . Output from the code in Listing 3.
• Figure 2 (p. 236) . Output from the code in Listing 4.
• Figure 3 (p. 236) . Output from the code in Listing 5.
• Figure 4 (p. 238) . Output from code in Listing 6.
28.2.3.2 Listings
• Listing 1 (p. 234) . Dene the superclass.
• Listing 2 (p. 234) . Dene a subclass that extends or inherits from Superclass.
• Listing 3 (p. 235) . Display the value of the class variable.
• Listing 4 (p. 235) . Instantiate a Subclass object.
• Listing 5 (p. 236) . Modify and then print the value in superclassVar.
• Listing 6 (p. 237) . Complete program listing.
I have discussed encapsulation in some detail in previous modules. As near as I can tell, unlike C++ and
Java, Python does not support polymorphism, at least not in any signicant way.
28.3.1 Inheritance
C++ and Java support two forms of polymorphism:
• Compile-time polymorphism
• Runtime polymorphism
Both of these depend on the "strongly-typed" nature of C++ and Java. Runtime polymorphism using virtual
functions and base-class pointers also depends on inheritance.
Because Python is a "weakly-typed" (if typed at all) programming language, I don't know how to
implement either form of polymorphism using Python. (However, if I am wrong on this, I will be happy to
learn how to implement polymorphism in Python.)
Inheritance is extremely important in C++ and Java primarily because of the use of runtime polymor-
phism in program design. However, due to the lack of polymorphism, inheritance in Python is not nearly as
important as in those other two languages. Although it may have some other uses, inheritance in Python
appears mainly to be a convenient way to reduce the amount of typing required to write a program.
According to tutorialspointPython Objected Oriented 2 , Python inheritance is
"The transfer of the characteristics of a class to other classes that are derived from it."
While that can sometimes be a useful capability, it is simply one of many useful capabilities that exist in
Python
Depending on your preferred jargon, Superclass will be the base class or the superclass . Subclass
will be the derived class or the subclass .
def __init__(self,name):
Superclass.superclassVar = name
self.friend = "Tom"
Table 28.1
The code in Listing 1 (p. 234) creates a class variable named superclassVar and initializes its value
to True .
The code in Listing 1 (p. 234) also denes an initializer or constructor method named __init__ .
When this method is executed, it changes the value stored in superclassVar from True to the value of
an incoming parameter named name .
The __init__ method also creates an instance variable named friend in the object referred to by
self and initializes that variable to contain "Tom" .
Table 28.2
The method named __init__ in Subclass also prints the value of the variable named friend
when it is executed.
When you instantiate a new object of a class that contains a method named __init__, that
method is automatically called and a reference to the object is automatically passed as the rst
parameter to the method. However, if you write code that explicitly calls __init__, you must
explicitly pass that parameter along with any other required parameters as shown in Listing 2.
Table 28.3
As you can see, at this point in the program, the class variable contains the value True that was placed
there when the class variable was created by the code in Listing 1 (p. 234) .
True
Table 28.4
ref = Subclass("Joe")
print(ref.superclassVar)
Table 28.5
As you saw in Listing 2 (p. 234) , the __init__ method in the class named Subclass calls the
method named __init__ in the class named Superclass passing both of its incoming parameters as
parameters to the __init__ method in the class named Superclass . As you saw in Listing 1 (p. 234)
, that method replaces the value True in superclassVar with one of its incoming parameters, which in
this case is "Joe" .
The code in the method named __init__ in the class named Subclass in Listing 2 (p. 234)
also prints the value of the variable named friend (which is created and initialized by the __init__
method in the class named Superclass in Listing 1 (p. 234) ) . This produces the rst line of output
text (Tom) shown in Figure 2 (p. 236) .
The code in Listing 4 (p. 235) also uses the reference to the Subclass object to get and print the value
of the class variable named superclassVar from the class named Superclass , producing the second
line of output text in Figure 2 (p. 236) (Joe) .
Tom
Joe
Table 28.6
ref.superclassVar = "Sue"
print(ref.superclassVar)
Table 28.7
Figure 3 (p. 236) shows the output produced by executing the code in Listing 5 (p. 236) .
Sue
Table 28.8
# Illustrates inheritance
#----------------------------------------------------------
#Define the superclass
class Superclass(object):
superclassVar = True
def __init__(self,name):
Superclass.superclassVar = name
self.friend = "Tom"
Table 28.9
6 http://pythontutor.com/visualize.html#mode=display
True
Tom
Joe
Sue
Table 28.10
28.8 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1450-Inheritance
• File: Itse1359-1450.htm
• Published: 10/27/14
• Revised: 03/04/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I : I am a professor of Computer Information Technology at Austin Community
College in Austin, TX.
-end-
Itse1359-1610-GUI Programming 1
29.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module is not intended to be a stand-alone tutorial. Instead, it is intended to be an annotated guide
to other freely available online resources on the topic.
29.2.2.1 Figures
• Figure 1 (p. 242) . Image of a tkinter test from the command line.
• Figure 2 (p. 243) . Output from Feet to Meters converter program.
• Figure 3 (p. 243) . Output from Listing 1.
• Figure 4 (p. 248) . A sticky demonstration.
29.2.2.2 Listings
• Listing 1 (p. 243) . The root.
• Listing 2 (p. 245) . Add a Frame widget.
• Listing 3 (p. 247) . StringVar objects.
• Listing 4 (p. 247) . The Entry widget.
• Listing 5 (p. 249) . A Label and a Button.
• Listing 6 (p. 249) . Three more Label widgets.
• Listing 7 (p. 250) . Finishing touches.
• Listing 8 (p. 250) . The calculate function.
• Listing 9 (p. 252) . Complete program listing.
"In computing, a graphical user interface (GUI... sometimes pronounced "gooey" ...) is a type of
interface that allows users to interact with electronic devices through graphical icons and visual
indicators such as secondary notation, as opposed to text-based interfaces, typed command labels
or text navigation. GUIs were introduced in reaction to the perceived steep learning curve of
command-line interfaces ... which require commands to be typed on the keyboard."
You probably interface with most of the programs that you use on a daily basis through a GUI. If you use
Python to create programs that will be used by non programmers, you may need to provide GUIs for those
programs.
"Tcl (Tool Command Language) is a very powerful but easy to learn dynamic programming lan-
guage, suitable for a very wide range of uses, including web and desktop applications, networking,
administration, testing and many more. Open source and business-friendly, Tcl is a mature yet
evolving language that is truly cross platform, easily deployed and highly extensible."
"Tk is a graphical user interface toolkit that takes developing desktop applications to a higher
level than conventional approaches. Tk is the standard GUI not only for Tcl, but for many
other dynamic languages, and can produce rich, native applications that run unchanged across
Windows, Mac OS X, Linux and more."
"Tk/Tcl has long been an integral part of Python. It provides a robust and platform independent
windowing toolkit, that is available to Python programmers using the tkinter package, and its
extension, the tkinter.tix and the tkinter.ttk modules. ... To use tkinter, you don't need to
write Tcl code, but you will need to consult the Tk documentation, and occasionally the Tcl
documentation. tkinter...usually comes bundled with Python."
According to The Python Standard Library 25.1 tkinter Python interface to Tcl/Tk 6 ,
2 http://en.wikipedia.org/wiki/Graphical_user_interface
3 http://www.tcl.tk/
4 http://www.tcl.tk/
5 https://docs.python.org/3/library/tk.html
6 https://docs.python.org/3/library/tkinter.html#module-tkinter
"The tkinter package ("Tk interface") is the standard Python interface to the Tk GUI toolkit.
Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. (Tk
itself is not part of Python; it is maintained at ActiveState.) You can check that tkinter is
properly installed on your system by running python -m tkinter from the command line; this
should open a window demonstrating a simple Tk interface."
Figure 1 (p. 242) shows the result of entering the command given above on the command line on my
computer and then clicking the button labeled Click me several times. Clicking the QUIT button will
cause the GUI to disappear from the screen as expected.
Figure 1 . Image of a tkinter test from the command line.
root = Tk()
root.title("Feet to Meters")
root.mainloop()
Table 29.1
Figure 3 (p. 243) shows the output produced by running the skeleton code in Listing 1 (p. 243) .
Figure 3 . Output from Listing 1.
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
Table 29.2
According to TkDocs ,
"Next, we create a frame widget, which will hold all the content of our user interface, and place
that in our main window. The "columncongure"/"rowcongure" bits just tell Tk that if the
main window is resized, the frame should expand to take up the extra space."
"Like the Tkinter Frame widget, the ttk.Frame widget is a rectangular container for other wid-
gets."
That document goes on to explain that the rst argument to Frame is the parent of the Frame . In
this case, the frame will be a child of root . After that, you can enter any of about nine dierent options,
one of which is padding .
According to Padding 15 at TkDocs,
"Normally, each column or row will be directly adjacent to the next, so that widgets will be right
next to each other. This is sometimes what you want (think of a listbox and its scrollbar), but
often you want some space between widgets. In Tk, this is called padding, and there are several
ways you can choose to add it."
One of the ways to achieve the desired space between widgets is by using the syntax shown in the padding
argument of Listing 2 (p. 245) . This syntax inserts spaces (in units of pixels) on the four sides of the
frame in the order west, north, east, and south or left, top, right, and bottom. (Note that the integers in
Listing 2 (p. 245) are not separated by commas.)
"Grid is one of several geometry managers available in Tk, but it's mix of power, exibility and
ease of use, along with its natural t with today's layouts (that rely on alignment of widgets)
make it the best choice for general use. There are other geometry managers: "pack" is also quite
powerful, but harder to use and understand; "place" gives you complete control of positioning
each element; we'll see even widgets like paned windows, notebooks, canvas and text can act as
geometry managers."
The second statement in Listing 2 (p. 245) shows that the Frame is placed in column 0 and row 0 (the
upper-left corner) of the root .
29.4.2.8 StringVar
To make a long story short, the StringVar objects shown in Listing 3 (p. 247) are used to connect the
string values for feet and meters to the textvariable arguments of an Entry widget and a Label
widget, which you will see later.
(If you connect a widget to a StringVar object using a textvariable argument, a change in
the contents of either will cause a corresponding change in the other. This is discussed in some detail at
25.1.6.4. Coupling Widget Variables 19 .)
17 http://www.tkdocs.com/tutorial/grid.html#colsrows
18 http://www.tkdocs.com/tutorial/grid.html#incell
19 https://docs.python.org/3/library/tkinter.html#coupling-widget-variables
feet = StringVar()
meters = StringVar()
Table 29.3
Table 29.4
As you will see shortly, the numerical result of calculating the conversion from feet to meters is displayed
in column 2 in the row immediately below the Entry widget. When that result requires more width than
is available in the column by default, the width of the column is automatically increased. Because the right
and left ends of the Entry widget stick to the sides of its cell in that column, the width of the Entry
widget increases accordingly.
25 http://www.tkdocs.com/tutorial/rstexample.html
Table 29.5
• References to the widgets are not saved in ordinary Python variables because there will be no need to
refer to them later in program code.
• The dot operator is used to call grid immediately upon the creation of each widget as opposed to
calling grid in a separate statement as was done in Listing 4 (p. 247) .
• The Button widget uses a text argument to place the string "Calculate" on the face of the
button.
• No text is placed in the Label . The empty Label will be used to display the results of converting
feet to meters.
• The left end of the Button widget is stuck to the west side of the cell in which it resides. It is not
stretched to the width of the cell as is the case with the Entry and Label widgets
• Last and perhaps most important, the Button widget uses the command argument to specify
that the function named calculate will be executed whenever the user presses the button.
Table 29.6
There is nothing new in Listing 6 (p. 249) so no explanation of that code should be required.
"The rst line walks through all of the widgets that are children of our content frame, and adds a
little bit of padding around each, so they aren't so scrunched together. We could have added these
options to each "grid" call when we rst put the widgets onscreen, but this is a nice shortcut."
26 http://www.tkdocs.com/tutorial/rstexample.html
feet_entry.focus()
root.bind('<Return>', calculate)
Table 29.7
The author 27 describes the second line of code in Listing 7 (p. 250) as follows:
"The second line tells Tk to put the focus on our entry widget. That way the cursor will start in
that eld, so the user doesn't have to click in it before starting to type."
And nally , the author 28 describes the third line of code in Listing 7 (p. 250) as follows:
"The third line tells Tk that if the user presses the Return key (Enter on Windows) anywhere
within the root window, that it should call our calculate routine, the same as if the user pressed
the Calculate button."
def calculate(*args):
try:
value = float(feet.get())
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
Table 29.8
def calculate(*args):
try:
value = float(feet.get())
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
root = Tk()
root.title("Feet to Meters")
feet = StringVar()
meters = StringVar()
feet_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()
Table 29.9
29.7 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1610-GUI Programming
• File: Itse1359-1610.htm
• Published: 10/28/14
• Revised: 01/31/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
Itse1359-1720-Doctest 1
30.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
1 This content is available online at <http://cnx.org/content/m51550/1.3/>.
Available for free at Connexions <http://cnx.org/content/col11986/1.1>
255
256 CHAPTER 30. ITSE1359-1720-DOCTEST
30.2.3.1 Figures
• Figure 1 (p. 257) . Simple interactive session.
• Figure 2 (p. 259) . Output produced by the test le named Py1359_1720_01.txt.
• Figure 3 (p. 261) . Output produced by the test le named Py1359_1720_02.txt.
• Figure 4 (p. 263) . Output produced by the test embedded in the docstring.
30.2.3.2 Listings
• Listing 1 (p. 257) . Contents of the test le named Py1359_1720_01.txt.
• Listing 2 (p. 258) . Contents of the le named Py1359_1720_01.bat.
• Listing 3 (p. 259) . Contents of the le named Py1359_1720_02.py.
• Listing 4 (p. 260) . Contents of the test le named Py1359_1720_02.txt.
• Listing 5 (p. 260) . Contents of the batch le named Py1359_1720_02.bat.
• Listing 6 (p. 262) . Contents of the le named Py1359_1720_03.py.
• Listing 7 (p. 262) . Contents of the batch le named Py1359_1720_03.bat.
"The doctest and unittest modules contain frameworks for writing unit tests that automatically
exercise code and verify that the expected output is produced."
The material in this module is based heavily on The Python Standard Library 26.2 doctest Test interactive
Python examples 3 . According to that document,
"The doctest module searches for pieces of text that look like interactive Python sessions, and
then executes those sessions to verify that they work exactly as shown."
2 https://docs.python.org/3/library/development.html
3 https://docs.python.org/3/library/doctest.html
Stated dierently, the doctest module can be used to automate the execution of code that you might
otherwise execute in an interactive session. However, instead of having the computer present the result to
you, you specify the required result and the computer tests the required result against the actual result and
noties you when the two fail to match.
There are at least two dierent ways to use doctest :
1. You can write the test code in a separate text le and use that le to perform the testing.
2. You can embed the test code in one or more docstrings in a module and cause the test code to be
executed each time you execute the module as a script.
A simple example of each approach will be provided and explained in this module.
> 2 + 3
5
>
Table 30.1
If you have forgotten what the text in Figure 1 (p. 257) means, see the earlier module titled Itse1359-
1010-Getting Started .
> 2 + 3
6
Table 30.2
Note that the contents of the test le mirror the input and the output of the interactive session shown in
Figure 1 (p. 257) except that the result of performing the computation was purposely specied incorrectly
as 6 instead of 5. (When you create a doctest test le, you must specify the required output as shown
in Listing 1 (p. 257) .)
Note that the doctest process is very picky regarding le names. Some le names that are
valid for the operating system may not work when using doctest . However, if you restrict
your le names to names that would be valid for variables, you should be okay.
Listing 2 (p. 258) shows the contents of a Windows batch le named Py1359_1720_01.bat that I used
to perform the test. Although not a requirement, the use of such a batch le makes it convenient to perform
the test more than once with a minimal typing eort.
echo off
pause
Table 30.3
(Some of the text in Listing 2 (p. 258) is peculiar to my machine. You can ignore the text shown in
Listing 2 (p. 258) down to the command that begins with the word python .)
The third command that begins with the word python in Listing 2 (p. 258) executes the Python doctest
module as a script and passes the name of the test le as a command-line argument to the module.
According to Python v3.1.5 documentation Python Setup and Usage 4 , "When called with -m
module-name, the given module is located on the Python module path and executed as a script."
The syntax shown for the python command in Listing 2 (p. 258) is a command line shortcut for calling
the testmod() function in the doctest module. (You will see more on this later.)
4 https://docs.python.org/3.1/using/cmdline.html
**********************************************************************
File "Py1359_1720_01.txt", line 2, in Py1359_1720_01.txt
Failed example:
2 + 3
Expected:
6
Got:
5
**********************************************************************
1 items had failures:
1 of 1 in Py1359_1720_01.txt
***Test Failed*** 1 failures.
Table 30.4
Figure 2 (p. 259) shows that the test failed because the result of the computation did not match the
specied value of 6. In this case, the output from the computation was correct but the value specied by
the test was incorrect.
When testing using the doctest module, you specify what the result must be for the test to pass. If
the test fails to produce a matching result, the output will show that the test failed. For the case of Listing 1
(p. 257) and Listing 2 (p. 258) , if the correct result had been specied, the test would simply have returned
to the command prompt with no other output.
A verbose output could have been specied in Listing 2 (p. 258) to cause the test to return
positive results even if there were no failures. You will see how to do that in the next example.
def sum(parA,parB):
return parA + parB
Table 30.5
Table 30.6
This le will actually cause two tests to be performed. The rst test is to import the function named
sum from the module named Py1359_1720_02 . However, no visible output is expected from this
test. Therefore, no output is specied in Listing 4 (p. 260) .
The second test is to call the function named sum (shown in Listing 3 (p. 259) ) passing 2 and 3 as
parameters. The test le shows that an incorrect output value of 6 is expected from this test. (As before,
an incorrect value was specied to force the test to fail for illustration purposes only.)
echo off
pause
Table 30.7
This batch le diers from the one shown in Listing 2 (p. 258) in one respect. The -v switch following
doctest in the command causes the output to be more verbose than the output from the previous example.
Trying:
from Py1359_1720_02 import sum
Expecting nothing
ok
Trying:
sum(2,3)
Expecting:
6
**********************************************************************
File "Py1359_1720_02.txt", line 2, in Py1359_1720_02.txt
Failed example:
sum(2,3)
Expected:
6
Got:
5
**********************************************************************
1 items had failures:
1 of 2 in Py1359_1720_02.txt
2 tests in 1 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.
Table 30.8
As mentioned earlier, this output is more verbose than the output shown in Figure 2 (p. 259) . In this
more verbose output, tests that pass are shown in addition to tests that fail. For example, the test of the
import statement is shown as ok .
On the other hand, as expected, the call to the sum function is shown as failed . As before, both
the location and the nature of the failure is explained.
5 http://legacy.python.org/dev/peps/pep-0257/#what-is-a-docstring
"""
> sum(2,3)
6
"""
def sum(parA,parB):
return parA + parB
if __name__ == "__main__":
import doctest
doctest.testmod()
Table 30.9
Listing 6 (p. 262) is an update of the earlier module shown in Listing 3 (p. 259) . Test code was
embedded in the docstring at the beginning of Listing 6 (p. 262) .
In addition, a special if statement was added at the end of Listing 6 (p. 262) that calls the testmod
function of the doctest module. (See the earlier (p. 258) comment regarding the testmod function and
the command line shortcut.)
According to The Python Standard Library 26.2.1. Simple Usage: Checking Examples in Docstrings 6
, if you include this code at the end of a module, running the module as a script will cause tests that
are embedded in docstrings to be executed.
Detailed information on the various options of the testmod function are provided at The Python
Standard Library 26.2.4. Basic API 7 .
echo off
pause
Table 30.10
6 https://docs.python.org/3/library/doctest.html#simple-usage-checking-examples-in-docstrings
7 https://docs.python.org/3/library/doctest.html#basic-api
In this case there was no separate test le. Instead, the python command in Listing 7 (p. 262) executed
the module named Py1359_1720_03.py as a script triggering the test to be run as described above (p.
262) .
Trying:
sum(2,3)
Expecting:
6
**********************************************************************
File "Py1359_1720_03.py", line 4, in __main__
Failed example:
sum(2,3)
Expected:
6
Got:
5
1 items had no tests:
__main__.sum
**********************************************************************
1 items had failures:
1 of 1 in __main__
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
Table 30.11
30.7 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1720-Doctest Introduction
• File: Itse1359-1720.htm
• Published: 11/10/14
• Revised: 03/04/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
8 https://docs.python.org/3/library/doctest.html
9 https://www.packtpub.com/books/content/testing-python-using-doctest
10 https://www.packtpub.com/books/content/python-unit-testing-doctest
11 http://docs.python-guide.org/en/latest/writing/tests/
12 http://agiletesting.blogspot.com/2005/01/python-unit-testing-part-1-unittest.html
13 http://agiletesting.blogspot.com/2005/01/python-unit-testing-part-2-doctest.html
14 http://agiletesting.blogspot.com/2005/01/python-unit-testing-part-3-pytest-tool.html
15 http://www.python-course.eu/python3_tests.php
16 http://ivory.idyll.org/articles/nose-intro.html#running-doctests-in-nose
17 http://nose.readthedocs.org/en/latest/plugins/doctests.html
18 http://www.youtube.com/watch?v=L8EcFqpX-Lk
Itse1359-1720r-Review 1
31.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to the module titled Itse1359-1720-Doctest
. In addition, some of the questions and answers are keyed to the document titled The Python Standard
Library 26.2 doctest Test interactive Python examples 2 .
Once you study that material, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
31.3 Questions
31.3.1 Question 1
True or False? The test le shown in Figure 1 (p. 268) , when executed using the command shown in Figure
2 (p. 268) , produces the output shown in Figure 3 (p. 268) .
265
266 CHAPTER 31. ITSE1359-1720R-REVIEW
Table 31.1
Table 31.2
**********************************************************************
File "Py1359_1720r_01.txt", line 2, in Py1359_1720r_01.txt
Failed example:
2 + 3
Expected:
6
Got:
5
**********************************************************************
File "Py1359_1720r_01.txt", line 7, in Py1359_1720r_01.txt
Failed example:
2 + 3
Expected:
6
Got:
5
**********************************************************************
1 items had failures:
2 of 2 in Py1359_1720r_01.txt
***Test Failed*** 2 failures.
Table 31.3
31.3.2 Question 2
True or False? The test le shown in Figure 5 (p. 269) , when executed using the command shown in Figure
6 (p. 269) , produces the output shown in Figure 7 (p. 269) .
Beginning of test
> 2 + 3
5
End of test
Table 31.4
Table 31.5
Trying:
2 + 3
Expecting:
5
ok
1 items passed all tests:
1 tests in Py1359_1720r_02.txt
1 tests in 1 items.
1 passed and 0 failed.
Test passed.
Table 31.6
31.3.3 Question 3
True or False? Given the Python module shown in Figure 9 (p. 270) , when the test le shown in Figure
10 (p. 270) is executed using the command shown in Figure 11 (p. 270) , it produces the output shown in
Figure 12 (p. 271) .
def sum(parA,parB):
return parA + parB
def product(parC,parD):
return parC * parD
Table 31.7
Table 31.8
Table 31.9
Trying:
from Py1359_1720r_03 import sum
Expecting nothing
ok
Trying:
sum(2,3)
Expecting:
5
ok
Trying:
from Py1359_1720r_03 import product
Expecting nothing
ok
Trying:
product(2,3)
Expecting:
6
ok
1 items passed all tests:
4 tests in Py1359_1720r_03.txt
4 tests in 1 items.
4 passed and 0 failed.
Test passed.
Table 31.10
31.3.4 Question 4
True or False? Given the Python module shown in Figure 13 (p. 272) , execution of the module as a script
using the command shown in Figure 14 (p. 272) produces the output shown in Figure 15 (p. 273) .
"""
Begin first test
> sum(2,3)
5
def product(parC,parD):
return parC * parD
if __name__ == "__main__":
import doctest
doctest.testmod()
Table 31.11
python Py1359_1720r_04.py -v
Table 31.12
Trying:
sum(2,3)
Expecting:
5
ok
Trying:
product(2,3)
Expecting:
6
ok
2 items had no tests:
__main__.product
__main__.sum
1 items passed all tests:
2 tests in __main__
2 tests in 3 items.
2 passed and 0 failed.
Test passed.
Table 31.13
31.3.5 Question 5
True or False? Given the Python module shown in Figure 16 (p. 273) , execution of the module as a script
using the command shown in Figure 17 (p. 274) produces the output shown in Figure 18 (p. 274) .
"""
> divide(2,3)
0
> divide(2,0)
Traceback (most recent call last):
...
ZeroDivisionError: integer division or modulo by zero
"""
def divide(parA,parB):
return parA//parB
if __name__ == "__main__":
import doctest
doctest.testmod()
Table 31.14
python Py1359_1720r_05.py -v
Table 31.15
Trying:
divide(2,3)
Expecting:
0
ok
Trying:
divide(2,0)
Expecting:
Traceback (most recent call last):
...
ZeroDivisionError: integer division or modulo by zero
ok
1 items had no tests:
__main__.divide
1 items passed all tests:
2 tests in __main__
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
Table 31.16
31.3.6 Question 6
True or False? Given the Python module shown in Figure 19 (p. 275) , execution of the module as a script
using the command shown in Figure 20 (p. 275) produces the output shown in Figure 21 (p. 275) .
"""
> function(11)
Traceback (most recent call last):
...
ValueError: par must be <= 10
"""
def function(par):
if par > 10:
raise ValueError("par must be <= 10")
else:
return par
if __name__ == "__main__":
import doctest
doctest.testmod()
Table 31.17
python Py1359_1720r_06.py -v
Table 31.18
Trying:
function(11)
Expecting:
Traceback (most recent call last):
...
ValueError: par must be <= 10
ok
1 items had no tests:
__main__.function
1 items passed all tests:
1 tests in __main__
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
Table 31.19
31.3.7 Question 7
True or False? Given the Python modules shown in Figure 22 (p. 276) and Figure 23 (p. 276) and the test
le shown in Figure 25 (p. 276) , execution of the command shown in Figure 24 (p. 276) , produces the
output shown in Figure 26 (p. 277) .
import doctest
doctest.testfile(Py1359_1720r_07.txt)
Table 31.20
def sum(parA,parB):
return parA + parB
def product(parC,parD):
return parC * parD
Table 31.21
python Py1359_1720r_07_runner.py
Table 31.22
Table 31.23
**********************************************************************
File "Py1359_1720r_07.txt", line 5, in Py1359_1720r_07.txt
Failed example:
product(2,3)
Expected:
5
Got:
6
**********************************************************************
1 items had failures:
1 of 4 in Py1359_1720r_07.txt
***Test Failed*** 1 failures.
Table 31.24
31.3.8 Question 8
True or False? Given the Python module shown in Figure 27 (p. 278) and the test le shown in Figure 28
(p. 278) , execution of the module as a script using the command shown in Figure 29 (p. 278) , produces
the output shown in Figure 30 (p. 279) .
def sum(parA,parB):
return parA + parB
def product(parC,parD):
return parC * parD
if __name__ == "__main__":
import doctest
doctest.testfile("Py1359_1720r_08.txt")
Table 31.25
Table 31.26
python Py1359_1720r_08.py -v
Table 31.27
Trying:
from Py1359_1720r_08 import sum
Expecting nothing
ok
Trying:
sum(2,3)
Expecting:
5
ok
Trying:
from Py1359_1720r_08 import product
Expecting nothing
ok
Trying:
product(2,3)
Expecting:
6
ok
1 items passed all tests:
4 tests in Py1359_1720r_08.txt
4 tests in 1 items.
4 passed and 0 failed.
Test passed.
Table 31.28
31.3.9 Question 9
True or False? Given the Python version 3 module shown in Figure 31 (p. 280) , execution of the module as
a script using the command shown in Figure 32 (p. 280) , produces the output shown in Figure 33 (p. 280)
.
"""
> divide(1,3)
0.333333
"""
def divide(parA,parB):
return parA/parB
if __name__ == "__main__":
import doctest
doctest.testmod()
Table 31.29
python Py1359_1720r_09.py -v
Table 31.30
Trying:
divide(1,3)
Expecting:
0.3333333333333333
ok
1 items had no tests:
__main__.divide
1 items passed all tests:
1 tests in __main__
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
Table 31.31
This image was also inserted for the purpose of inserting space between the questions and the answers.
31.5 Answers
31.5.1 Answer 9
False. The actual output is shown in Figure 34 (p. 283) . This question was included to point out that
testing oating point results can be problematic. Many oating point values are simply estimates. Although
this example was purposely caused to fail, a given oating point computation may not produce exactly the
same results on dierent platforms. Therefore a test that species a given oating point result may pass on
one platform and fail on a dierent platform. The Python Standard Library 26.2 doctest Test interactive
Python examples 3 provides some suggestions for dealing with this issue.
Trying:
divide(1,3)
Expecting:
0.333333
**********************************************************************
File "Py1359_1720r_09.py", line 4, in __main__
Failed example:
divide(1,3)
Expected:
0.333333
Got:
0.3333333333333333
1 items had no tests:
__main__.divide
**********************************************************************
1 items had failures:
1 of 1 in __main__
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
Table 31.32
31.5.2 Answer 8
True. The call to testle at the end of Figure 27 (p. 278) illustrates another way to cause a test le to be
executed.
Go back to Question 8 (p. 277)
31.5.3 Answer 7
False. The output produced by the procedure described in this question includes the following error message:
NameError: name 'Py1359_1720r_07' is not dened
3 https://docs.python.org/3/library/doctest.html
This indicates a syntax error in the specication of the name of the test le. While the concept is correct
and it is possible to use Python code in a module similar to that shown in Figure 22 (p. 276) to execute a
test le such as that shown in Figure 25 (p. 276) , the name of the test le must be enclosed in quotation
marks to be recognized as a test le. Enclosing the name of the test le in Figure 22 (p. 276) in quotes
would cause the procedure to produce the output shown in Figure 26 (p. 277) .
Go back to Question 7 (p. 276)
31.5.4 Answer 6
True.
Go back to Question 6 (p. 274)
31.5.5 Answer 5
True.
Go back to Question 5 (p. 273)
31.5.6 Answer 4
True.
Go back to Question 4 (p. 271)
31.5.7 Answer 3
True.
Go back to Question 3 (p. 270)
31.5.8 Answer 2
False. The actual output is shown in Figure 8 (p. 285) . This is a formatting issue. If you include explanatory
text in the text le, you must leave a blank line between the last line of specied output and the following
line of explanatory text. Otherwise, the explanatory text will be included as part of required output as
shown by the expected output value in Figure 8 (p. 285) .
Trying:
2 + 3
Expecting:
5
End of test
**********************************************************************
File "Py1359_1720r_02.txt", line 2, in Py1359_1720r_02.txt
Failed example:
2 + 3
Expected:
5
End of test
Got:
5
**********************************************************************
1 items had failures:
1 of 1 in Py1359_1720r_02.txt
1 tests in 1 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
Table 31.33
31.5.9 Answer 1
False. The actual output is shown in Figure 4 (p. 286) . The test le contained two tests, one of which
passed and the other of which failed. Verbose output was not specied. Therefore, the report didn't include
information on the test that passed. The test le contained explanatory text, which is okay under certain
formatting conditions, which I will get into in a subsequent question..
**********************************************************************
File "Py1359_1720r_01.txt", line 7, in Py1359_1720r_01.txt
Failed example:
2 + 3
Expected:
6
Got:
5
**********************************************************************
1 items had failures:
1 of 2 in Py1359_1720r_01.txt
***Test Failed*** 1 failures.
Table 31.34
31.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1720r-Review
• File: Itse1359-1720r.htm
• Published: 11/10/14
• Revised: 03/04/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
32.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
The learning resource for the Networking and Databases portion of the course consists of material
from selected websites plus modules containing review questions and answers keyed to the material on those
websites.
The Networking and Databases portion of the course consists of three main areas of study:
A list of selected websites plus review questions keyed to those websites is provided for each area of study.
The combined list of selected websites is:
285
CHAPTER 32. ITSE1359-1900-PREFACE TO NETWORKING AND
286
DATABASES
Take-home programming assignments may be based on any material on these websites as well as material
from any of the other modules in this collection of modules for teaching ITSE 1359. However, online tests
for the Networking and Databases portion of the course will be restricted to topics addressed in the
following three modules:
The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, col-
laborative, hypermedia information systems. This is the foundation for data communication for
the World Wide Web (i.e.. internet) since 1990. HTTP is a generic and stateless protocol which
can be used for other purposes as well using extension of its request methods, error codes and
headers.
In other words, if you are reading this document online, you are probably accessing and reading it from a
website using HTTP.
student's computing systems, this course will concentrate on a standard Portable DBM implementation 17
known as dbm.dumb. 18
According to Portable DBM implementation 19 , the dbm.dumb module is intended as a last resort
fallback for the dbm module when a more robust module is not available. The dbm.dumb module is not
written for speed and is not nearly as heavily used as the other database modules.
The dbm.dumb module provides a persistent dictionary-like interface which is written entirely in
Python. Unlike other modules such as dbm.gnu no external library is required. As with other persistent
mappings, the keys and values are always stored as bytes.
A shelf 20 is a persistent, dictionary-like object. The dierence with "dbm" databases is that the values
(not the keys!) in a shelf can be essentially arbitrary Python objects anything that the pickle 21 module
can handle. This includes most class instances, recursive data types, and objects containing lots of shared
sub-objects. The keys are ordinary strings.
Aside from this brief description, this course will not address shelve 22 .
SQLite is a C library that provides a lightweight disk-based database that doesn't require a
separate server process and allows accessing the database using a nonstandard variant of the
SQL query language.
SQL is also a very broad topic. Considerable time, eort, and experience is required to master SQL. As
evidence of this fact, see the somewhat voluminous W3Schools SQL Tutorial 24 .
Knowledge of SQL is a prerequisite for understanding SQLite. It is unreasonable to expect a student to
learn SQL during a small portion of a one-semester course in Python. Therefore, the module titled Itse1359-
1950-SQLite Database will barely scratch the surface and will provide only a cursory introduction to the
use of SQLite.
32.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1900-Preface to Networking and Databases
• File: Itse1359-1900.htm
• Published: 11/11/14
• Revised: 06/23/17
17 https://docs.python.org/3/library/dbm.html#module-dbm.dumb
18 https://docs.python.org/3/library/dbm.html#module-dbm.dumb
19 https://docs.python.org/3/library/dbm.html#module-dbm.dumb
20 https://docs.python.org/3/library/shelve.html
21 https://docs.python.org/3/library/pickle.html#module-pickle
22 https://docs.python.org/3/library/shelve.html
23 https://docs.python.org/3/library/sqlite3.html
24 http://www.w3schools.com/sql/
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
33.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This module contains review questions and answers keyed to material on the following web sites:
Once you study that module, you should be able to answer the review questions in this module.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to
navigate from the question to the answer and back again.
(Note to blind and visually impaired students: with the exception of two bitmap images that are used
solely as spacers to separate the question section from the answer section, all of the material in this module
is presented in plain text format and should be accessible using an audio screen reader or a braille display.
Note however that the required indentation may not be properly represented by an audio screen reader.)
1 This content is available online at <http://cnx.org/content/m51562/1.5/>.
2 https://docs.python.org/3/library/dbm.html
3 https://docs.python.org/3/library/shelve.html
4 https://docs.python.org/3/library/pickle.html
5 https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
289
290 CHAPTER 33. ITSE1359-1930-DBM AND SHELVE DATABASES
33.3 Questions
33.3.1 Question 1
True or False? The webpage at The Python Standard Library - 12.1. pickle - Python object serialization 6
contains the following warning:
Warning: The pickle module is not intended to be secure against erroneous or maliciously con-
structed data. Never unpickle data received from an untrusted or unauthenticated source.
33.3.2 Question 2
True or False? The webpage at The Python Standard Library - 12.3. shelve - Python object persistence 7
contains the following warning:
Warning: Because the shelve module is backed by pickle, it is insecure to load a shelf from an
untrusted source. Like with pickle, loading a shelf can execute arbitrary code.
33.3.3 Question 3
True or False? The webpage at The Python Standard Library - 12.5. dbm - Interfaces to Unix "databases"
8 contains the following warning:
Warning: The dbm module is not intended to be secure against erroneous or maliciously con-
structed data. Never use dbm data received from an untrusted or unauthenticated source.
33.3.4 Question 4
True or False? dbm is a generic interface to variants of the DBM databases - dbm.gnu or dbm.ndbm
. If neither of these modules is installed, the program will raise a "Module not found" error.
Go to answer 4 (p. 302)
33.3.5 Question 5
True or False? The function named dbm.whichdb( lename ) can be called to determine which
database module should be used to open a given database le.
Go to answer 5 (p. 302)
33.3.6 Question 6
True or False? The function named dbm.getDatabase can be called to open an existing dbm database
le or to create a new dbm database le.
Go to answer 6 (p. 302)
6 https://docs.python.org/3/library/pickle.html
7 https://docs.python.org/3/library/shelve.html
8 https://docs.python.org/3/library/dbm.html
33.3.7 Question 7
True or False? The object returned by dbm.open supports the same basic functionality as tuples. Keys
and their corresponding values can be stored, retrieved, and deleted. The in operator and the k eys
method are available, as well as get and setdefault .
Go to answer 7 (p. 302)
33.3.8 Question 8
True or False? When using the dbm.dumb module, attempting to store a key or value that is not a string
will raise an exception.
Go to answer 8 (p. 302)
33.3.9 Question 9
True or False? The following statement appears on the webpage at The Python Standard Library - 12.5.
dbm - Interfaces to Unix "databases" 9 .
Note: The dbm.dumb module is intended as a last resort fallback for the dbm module when a
more robust module is not available. The dbm.dumb module is not written for speed and is not
nearly as heavily used as the other database modules.
33.3.10 Question 10
True or False? The function named dbm.dumb.open can be called to create or open a dumbdbm
database and return a dumbdbm object.
Go to answer 10 (p. 301)
33.3.11 Question 11
True or False? The code in the top panel of Figure 1 (p. 293) produces the output shown in the bottom
panel of Figure 1 (p. 293) .
import dbm.dumb
dumbDb[b'aNumber'] = 1234
print(dumbDb.get('aNumber'))
dumbDb.close()
===============================================================================
1234
9 https://docs.python.org/3/library/dbm.html
Table 33.1
33.3.12 Question 12
True or False? The code in the top panel of Figure 3 (p. 294) produces the output shown in the bottom
panel of Figure 3 (p. 294) .
import dbm.dumb
dumbDb[b'aNumber'] = b'1234'
print(dumbDb.get('aNumber'))
dumbDb.close()
===============================================================================
b'1234'
Table 33.2
33.3.13 Question 13
True or False? The code in the top panel of Figure 4 (p. 295) produces the output shown in the bottom
panel of Figure 4 (p. 295) .
import dbm.dumb
empName = b'Joe'
dumbDb[b'name'] = empName
dumbDb[b"cubicle"] = b"639"
dumbDb[b"phone"] = b"617.665.2154"
if(empName in dumbDb):
print('name: ', dumbDb.get(b'name'))
print('cubicle: ', dumbDb.get(b'cubicle'))
print('phone: ', dumbDb.get(b'phone'))
else:
print("Not in dumbDb")
dumbDb.close()
===============================================================================
name: b'Joe'
cubicle: b'639'
phone: b'617.665.2154'
Table 33.3
33.3.14 Question 14
True or False? The code in the top panel of Figure 6 (p. 296) produces the output shown in the bottom
panel of Figure 6 (p. 296) .
import dbm.dumb
empName = b'Joe'
dumbDb[b'name'] = empName
dumbDb[b"cubicle"] = b"639"
dumbDb[b"phone"] = b"617.665.2154"
if(b'name' in dumbDb):
print('name: ', dumbDb.get(b'name'))
print('cubicle: ', dumbDb.get(b'cubicle'))
print('phone: ', dumbDb.get(b'phone'))
else:
print("Not in dumbDb")
dumbDb.close()
===============================================================================
name: b'Joe'
cubicle: b'639'
phone: b'617.665.2154'
Table 33.4
33.3.15 Question 15
True or False? The code in the top panel of Figure 7 (p. 296) produces the output shown in the bottom
panel of Figure 7 (p. 296) .
Figure 7 . Question 15.
import dbm.dumb
print()#blank line
print('Get and display keys')
keys = dumbDb.keys()
for key in keys:
print(key)
print()
print('Attempt to pop and display two values')
print(dumbDb.pop(b'phone','no phone'))
print(dumbDb.pop(b'weight','no weight'))
print()
print('Get and display keys and values')
print('Database length',len(dumbDb))
items = dumbDb.items()
for item in items:
print(item)
print()
print('Delete a key/value pair and display again')
del dumbDb[b'cubicle']
print('Database length',len(dumbDb))
items = dumbDb.items()
for item in items:
print(item)
dumbDb.close()
===============================================================================
Get and display values
b'name'
b'age'
b'phone'
b'marital'
b'cubicle'
b'gender'
This image was also inserted for the purpose of inserting space between the questions and the answers.
33.5 Answers
33.5.1 Answer 15
False. The output is shown in Figure 8 (p. 300) . The keys and the values were switched in the bottom
panel of Figure 7 (p. 296) .
Table 33.5
33.5.2 Answer 14
True.
Go back to Question 14 (p. 295)
33.5.3 Answer 13
False. The output is shown in Figure 5 (p. 301) . The in operator searches for a match in the keys and
does not search for a match in the values .
Not in dumbDb
Table 33.6
33.5.4 Answer 12
True.
Go back to Question 12 (p. 294)
33.5.5 Answer 11
False. The code in Figure 1 (p. 293) produces an error by attempting to store a number as a value. The
output (with some text deleted for brevity) is shown in Figure 2 (p. 301) .
Table 33.7
33.5.6 Answer 10
True. See The Python Standard Library - 12.5. dbm - Interfaces to Unix "databases" 10 .
Go back to Question 10 (p. 293)
10 https://docs.python.org/3/library/dbm.html
33.5.7 Answer 9
True as of 11/09/2014.
Go back to Question 9 (p. 293)
33.5.8 Answer 8
True. See comments in sample code at The Python Standard Library - 12.5. dbm - Interfaces to Unix
"databases" 11 .
Go back to Question 8 (p. 293)
33.5.9 Answer 7
False. The object returned by dbm.open supports the same basic functionality as dictionaries .
Keys and their corresponding values can be stored, retrieved, and deleted. The in operator and the keys
method are available, as well as get and setdefault .
Go back to Question 7 (p. 293)
33.5.10 Answer 6
False. The function named dbm.open can be called to open an existing dbm database le or to create
a new dbm database le.
Go back to Question 6 (p. 292)
33.5.11 Answer 5
True.
Go back to Question 5 (p. 292)
33.5.12 Answer 4
False. dbm is a generic interface to variants of the DBM databases - dbm.gnu or dbm.ndbm . If
neither of these modules is installed, the slow-but-simple implementation in module dbm.dumb will be
used.
Go back to Question 4 (p. 292)
33.5.13 Answer 3
False. As of 11/09/2014, no such warning appears on the referenced webpage. Therefore, it might be
concluded that even though shelve has features that are not available in dbm , dbm is the more
secure of the two. As a result, this course will concentrate on dbm and ignore shelve .
Go back to Question 3 (p. 292)
33.5.14 Answer 2
True as of 11/09/2014.
Go back to Question 2 (p. 292)
33.5.15 Answer 1
True as of 11/09/2014.
Go back to Question 1 (p. 292)
11 https://docs.python.org/3/library/dbm.html
33.6 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-1930-Dbm and Shelve Databases
• File: Itse1359-1930.htm
• Published: 11/10/14
• Revised: 07/28/16
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
34.2 Discussion
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX.
This material is provided for the benet of those students who are interested in expanding their learning
experience beyond the minimum requirements of the course titled ITSE 1359 Introduction to Scripting
Languages: Python . Knowledge of the material in this sub-collection titled Putting Python to Work
is not a requirement of the course.
During my 20+ years of teaching computer programming, I have learned that many students are more
likely to succeed in the course if I provide interesting and useful projects for them to work on in addition
to simply computing sales-tax tables, etc. Therefore, I try to provide projects such as image processing,
network programming, event-driven programming, game programming, etc. in addition to the more boring
text-only types of projects in most of the courses that I teach.
Several interesting libraries and Ebooks for doing this sort of programming with Python are freely avail-
able on the web including the following:
303
304 CHAPTER 34. ITSE1359-2110-PREFACE TO PUTTING PYTHON TO WORK
Many of the items in the above list could be used to develop interesting and useful programming projects for
use in this course. There is a problem, however. Many of the items in the list were developed using Python
version 2.x and have not yet been updated for compatibility with Python 3.x or later as of January 2015.
Therefore, some of the items in the above list are not compatible with the version of Python being used for
this course.
As time goes on, I plan to monitor the state of these items and to update the contents of this sub-collection
to explore the use of the items in this portion of the course material. Therefore, you should consider this
portion of the course material to be a work in process.
I will begin with Pygame 14 , which is compatible with Python version 3.4.
34.3 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-2110-Preface to Putting Python to Work
• File: Itse1359-2110.htm
• Published: 01/02/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
8 http://www.pythonware.com/products/pil/
9 https://pillow.readthedocs.org/
10 http://cs.nyu.edu/∼amos/courses/computer_programming/index.php?title=John_Zelle%27s_Graphics_Module
11 https://code.google.com/p/pythonxy/
12 http://www.engr.ucsb.edu/∼shell/che210d/numpy.pdf
13 http://inventwithpython.com/bookshelf/
14 http://pygame.org/news.html
35.2 Preface
This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to
Scripting Languages: Python at Austin Community College in Austin, TX. This module concentrates on
the use of the Online Python Tutor 2 , which includes a free educational code visualizer tool created by
Philip Guo 3 .
This code visualizer tool helps students overcome a fundamental barrier to learning programming: un-
derstanding what happens as the computer executes each line of a program's source code. Using this code
visualizer tool, a teacher or student can write Python, Java, and JavaScript programs in the Web browser
and visualize what the computer is doing step-by-step as it executes those programs.
35.3 Discussion
The primary benet of this code visualizer tool 4 is that it allows a student to step through a program one
instruction at a time, using Python 2.7 or Python 3.3, and to see what the computer is doing as the program
executes. A secondary benet is the ability to share a step-through session with another person online and
to discuss the behavior of the program while stepping through the program.
1 This content is available online at <http://cnx.org/content/m53197/1.1/>.
2 http://pythontutor.com/
3 http://www.pgbovine.net/
4 http://pythontutor.com/visualize.html#mode=edit
305
CHAPTER 35. ITSE1359-2510-GETTING STARTED WITH THE ONLINE
306
PYTHON TUTOR CODE VISUALIZER
That causes four more buttons with the following labels to appear:
• First
• Back
• Forward
• Last
You can click those buttons to navigate forward or backward through the program while observing the
information that is displayed on the screen to the right of the code box.
Links to several Python example programs are provided near the bottom of the page here 8 .
35.5 Miscellaneous
This section contains a variety of miscellaneous information.
Housekeeping material
• Module name: Itse1359-2510-Getting Started with the Online Python Tutor Code Visualizer
• File: Itse1359-2510.htm
• Published: 02/15/15
Disclaimers: Financial : Although the Connexions site makes it possible for you to download
a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed
version of the PDF le, you should be aware that some of the HTML elements in this module may
not translate well into PDF.
I also want you to know that, I receive no nancial compensation from the Connexions website even
if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle
books, and placed them for sale on Amazon.com showing me as the author. I neither receive
compensation for those sales nor do I know who does receive compensation. If you purchase such
a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it
was made and published without my prior knowledge.
Aliation : I am a professor of Computer Information Technology at Austin Community College
in Austin, TX.
-end-
11 http://www.pythontutor.com/visualize.html#mode=edit
12 http://interactivepython.org/courselib/static/thinkcspy/toc.html
Attributions
Collection: Python Scripting Language
Edited by: Ken Ninh
URL: http://cnx.org/content/col11986/1.1/
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1000-Preface"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51307/1.14/
Pages: 1-6
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1010-Getting Started"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51309/1.5/
Pages: 7-14
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1015-Program Organization"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51311/1.2/
Pages: 15-16
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1020r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51317/1.2/
Pages: 17-22
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1030r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51319/1.3/
Pages: 23-29
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1050-Introduction to Scripts"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51331/1.8/
Pages: 31-39
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1060-Syntax"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51348/1.4/
Pages: 41-49
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1065-Visualizing Python"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m53203/1.3/
Pages: 51-58
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1080r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51352/1.5/
Pages: 59-66
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1090-Lists Part 2"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51355/1.6/
Pages: 67-80
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1110-Nested Tuples"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51387/1.5/
Pages: 81-87
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1110r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51393/1.3/
Pages: 89-93
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1120-Empty and Single-Item Tuples"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51383/1.4/
Pages: 95-102
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1120r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51376/1.3/
Pages: 103-110
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1250r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51420/1.3/
Pages: 169-175
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1260-Loop Modiers"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51430/1.4/
Pages: 177-187
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1280-Function Arguments"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51433/1.5/
Pages: 189-205
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1410-Overview of Python classes"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51443/1.6/
Pages: 207-224
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1410r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51442/1.3/
Pages: 225-229
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1450-Inheritance"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51465/1.3/
Pages: 231-238
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1610-GUI Programming"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51474/1.5/
Pages: 239-253
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1720-Doctest"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51550/1.3/
Pages: 255-265
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1720r-Review"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51534/1.3/
Pages: 267-286
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1900-Preface to Networking and Databases"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51585/1.3/
Pages: 287-290
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-1930-Dbm and Shelve Databases"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m51562/1.5/
Pages: 291-303
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-2110-Preface to Putting Python to Work"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m52497/1.4/
Pages: 305-306
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
Module: "Itse1359-2510-Getting Started with the Online Python Tutor Code Visualizer"
By: R.G. (Dick) Baldwin
URL: http://cnx.org/content/m53197/1.1/
Pages: 307-309
Copyright: R.G. (Dick) Baldwin
License: http://creativecommons.org/licenses/by/4.0/
About OpenStax-CNX
Rhaptos is a web-based collaborative publishing system for educational material.