Preface This book is designed to be used as the primary textbook in a college-level first course in computing.It takes a fairly traditional approach,emphasizing prob- lem solving,design,and programming as the core skills of computer science. However,these ideas are illustrated using a non-traditional language,namely Python. In my teaching experience,I have found that many students have difficulty mastering the basic concepts of computer science and programming.Part of this difficulty can be blamed on the complexity of the languages and tools that are most often used in introductory courses.Consequently,this textbook was writ- ten with a single overarching goal:to introduce fundamental computer science concepts as simply as possible without being simplistic.Using Python is central to this goal. Traditional systems languages such as C++,Ada,and Java evolved to solve problems that arise in large-scale programming,where the primary emphasis is on structure and discipline.They were not designed to make writing small-or medium-scale programs easy.The recent rise in popularity (in industry,if not necessarily in academia)of scripting(sometimes called"agile")languages,such as Python,suggests an alternative approach.Python is very flexible and makes experimentation easy.Solutions to simple problems are simply and elegantly expressed.Python provides a great laboratory for the neophyte programmer. Python has a number of features that make it a near-perfect choice as a first programming language.The basic structures are simple,clean,and well de- signed,which allows students to focus on the primary skills of algorithmic thinking
Preface This book is designed to be used as the primary textbook in a college-level first course in computing. It takes a fairly traditional approach, emphasizing problem solving, design, and programming as the core skills of computer science. However, these ideas are illustrated using a non-traditional language, namely Python. In my teaching experience, I have found that many students have difficulty mastering the basic concepts of computer science and programming. Part of this difficulty can be blamed on the complexity of the languages and tools that are most often used in introductory courses. Consequently this textbook v^as written with a single overarching goal: to introduce fundamental computer science concepts as simply as possible without being simplistic. Using Python is central to this goal. Traditional systems languages such as C+ -h, Ada, and Java evolved to solve problems that arise in large-scale programming, where the primary emphasis is on structure and discipline. They were not designed to make writing small- or medium-scale programs easy The recent rise in popularity (in industry if not necessarily in academia) of scripting (sometimes called "agile") languages, such as Python, suggests an alternative approach. Python is very flexible and makes experimentation easy. Solutions to simple problems are simply and elegantly expressed. Python provides a great laboratory for the neophyte programmer. Python has a number of features that make it a near-perfect choice as a first programming language. The basic structures are simple, clean, and well designed, which allows students to focus on the primary skills of algorithmic thinking
Preface xi and program design without getting bogged down in arcane language details. Concepts learned in Python carry over directly to subsequent study of systems languages such as Java and C++.But Python is not a"toy language."It is a real- world,production language that is freely available for virtually every program- ming platform and comes standard with its own easy-to-use integrated program- ming environment.The best part is that Python makes learning to program fun again. Although I use Python as the language,teaching Python is not the main point of this book.Rather,Python is used to illustrate fundamental principles of design and programming that apply in any language or computing environment. In some places,I have purposely avoided certain Python features and idioms that are not generally found in other languages.There are already many good books about Python on the market;this book is intended as an introduction to computing. Similarly,there are places in the book where the "Pythonic"way of doing things has been eschewed for pedagogical reasons.For example,the chapter on the string data type uses functions from the string library before introducing string methods,which are generally preferable.This is done purposely in an effort to meet students"where they're at.Most beginning students know noth- ing about strings or objects,but they are already quite familiar with the basic concepts of computation in the context of mathematics(numbers).Thus,it is a natural progression to start with numeric algorithms and functions and extend those ideas to a less familiar data type,namely strings and string functions. From there,the introduction of string methods makes a perfect bridge to the use of objects. Besides using Python,there are other features of this book designed to make it a gentler introduction to computer science.Some of these features include the following: Extensive use of computer graphics.Students love working on programs that include graphics.This book presents a simple-to-use graphics pack- age(provided as a Python module)that allows students both to learn the principles of computer graphics and to practice object-oriented concepts without the complexity inherent in a full-blown graphics library and event- driven programming. Interesting examples.The book is packed with complete programming ex- amples to solve real problems
Preface xi and program design without getting bogged down in arcane language details. Concepts learned in P3^hon carry over directly to subsequent study of systems languages such as Java and C+ + . But P3^hon is not a "toy language." It is a real- world, production language that is freely available for virtually every programming platform and comes standard with its own easy-to-use integrated programming environment. The best part is that Python makes learning to program fun again. Although I use Python as the language, teaching Python is not the main point of this book. Rather, Python is used to illustrate fundamental principles of design and programming that apply in any language or computing environment. In some places, I have purposely avoided certain Python features and idioms that are not generally found in other languages. There are already many good books about Python on the market; this book is intended as an introduction to computing. Similarly there are places in the book where the "Pythonic" way of doing things has been eschewed for pedagogical reasons. For example, the chapter on the string data type uses functions from the string library before introducing string methods, which are generally preferable. This is done purposely in an effort to meet students "where they're at." Most beginning students know nothing about strings or objects, but they are already quite familiar with the basic concepts of computation in the context of mathematics (numbers) . Thus, it is a natural progression to start with numeric algorithms and functions and extend those ideas to a less familiar data type, namely strings and string functions. From there, the introduction of string methods makes a perfect bridge to the use of objects. Besides using Python, there are other features of this book designed to make it a gentler introduction to computer science. Some of these features include the following: • Extensive use of computer graphics. Students love working on programs that include graphics. This book presents a simple-to-use graphics package (provided as a Python module) that allows students both to learn the principles of computer graphics and to practice object-oriented concepts without the complexity inherent in a full-blown graphics library and eventdriven programming. • Interesting examples. The book is packed with complete programming ex- amples to solve real problems
xii Preface Readable prose.The narrative style of this book introduces key computer science concepts in a natural way as an outgrowth of a developing discus- sion.I have tried to avoid random facts or tangentially related sidebars. Flexible spiral coverage.Since the goal of the book is to present concepts simply,each chapter is organized so that students are introduced to new ideas in a gradual way,giving them time to assimilate an increasing level of detail as they progress.Ideas that take more time to master are intro- duced in early chapters and reinforced in later chapters. Just-in-time object coverage.The proper place for the introduction of ob- ject-oriented techniques is an ongoing controversy in computer science education.This book is neither strictly "objects early"nor "objects late," but gradually introduces object concepts after a brief initial grounding in the basics of imperative programming.Students learn multiple design tech- niques,including top-down (functional decomposition),spiral (prototyping),and object-oriented methods.Additionally,the textbook material is flexible enough to accommodate other approaches. Extensive end-of-chapter problems.Exercises at the end of every chapter provide ample opportunity for students to both reinforce chapter material and practice new programming skills. Coverage Options In keeping with the goal of simplicity,I have tried to limit inclusion of material that would not be covered in a first course.Still,there is probably more material here than can be covered in a typical one-semester introduction.My classes cover virtually all of the material in the first twelve chapters in order,though not necessarily covering every section in depth.One or two topics from Chapter 13 (Algorithm Design and Recursion)are generally interspersed at appropriate places during the term. Recognizing that different instructors prefer to approach topics in other or- ders,I have tried to keep the material relatively flexible.Chapters 1-3(Comput- ers and Programs,Writing Simple Programs,and Computing with Numbers)are essential introduction and should probably be covered in order.The first several sections of Chapter 4(Computing with Strings)are also fundamental to mate-
XII Preface Readable prose. The narrative style of this book introduces key computer science concepts in a natural way as an outgrowth of a developing discussion. I have tried to avoid random facts or tangentially related sidebars. Flexible spiral coverage. Since the goal of the book is to present concepts simply each chapter is organized so that students are introduced to new ideas in a gradual way, giving them time to assimilate an increasing level of detail as they progress. Ideas that take more time to master are introduced in early chapters and reinforced in later chapters. Just-in-time object coverage. The proper place for the introduction of object-oriented techniques is an ongoing controversy in computer science education. This book is neither strictly "objects early" nor "objects late," but gradually introduces object concepts after a brief initial grounding in the basics of imperative programming. Students learn multiple design techniques, including top-down (functional decomposition), spiral (prototyping), and object-oriented methods. Additionally the textbook material is flexible enough to accommodate other approaches. Extensive end-of-chapter problems. Exercises at the end of every chapter provide ample opportunity for students to both reinforce chapter material and practice new programming skills. Coverage Options In keeping with the goal of simplicity, I have tried to limit inclusion of material that would not be covered in a first course. Still, there is probably more material here than can be covered in a typical one-semester introduction. My classes cover virtually all of the material in the first twelve chapters in order, though not necessarily covering every section in depth. One or two topics from Chapter 13 (Algorithm Design and Recursion) are generally interspersed at appropriate places during the term. Recognizing that different instructors prefer to approach topics in other or- ders, I have tried to keep the material relatively flexible. Chapters 1-3 (Computers and Programs, Writing Simple Programs, and Computing with Numbers) are essential introduction and should probably be covered in order. The first several sections of Chapter 4 (Computing with Strings) are also fundamental to mate-
Preface xiii rial throughout the book,but the later sections(String formatting and File Pro- cessing)can be delayed until later in the course,if desired.Chapters 5-8(Graphics and Objects,Defining Functions,Decision Structures,and Loop Structures and Booleans)are designed to stand independently and can be taken in virtually any order.Chapters 9-12 are written to be taken in order,but the material in Chap- ter 11 could easily be moved earlier,should the instructor want to cover lists (arrays)before covering design.Instructors wishing to emphasize object-ori- ented design need not spend much time on Chapter 9.Chapter 13 contains more advanced material that may be covered at the end or interspersed at vari- ous places throughout the course. Acknowledgments My approach to CS1 has been influenced over the years by the many fine text- books that I have read and used for classes.Much that I have learned from those books has undoubtedly found its way into these pages.There are a few specific authors whose approaches have been so important that I feel they deserve spe- cial mention.A.K.Dewdney has always had a knack for finding simple examples that illustrate complex issues;I have borrowed a few of those and given them new legs in Python.I also owe a debt to wonderful textbooks from both Owen Astrachan and Cay Horstmann.The graphics library I introduce in Chapter 5 was directly inspired by my experience teaching with a similar library designed by Horstmann.I also learned much about teaching computer science from Nell Dale,for whom I was fortunate enough to serve as a TA when I was a graduate student at the University of Texas. Many people have contributed either directly or indirectly to the production of this book.I am grateful to Dave Reed at Capital University who used early versions and offered numerous insightful suggestions.I have also received much help and encouragement from my colleagues at Wartburg College:Lynn Olson, who offered unflagging support;and Josef Breutzmann,who supplied many project ideas.I also want to acknowledge the fine folks at Franklin,Beedle,and Associates,including Christine Collier and Krista Brown,and especially Jim Leisy Tom Sumner and Dean Lake,who turned my pet project into a real textbook. Thanks also goes to all of my students,who have taught me so much about teaching,and to Wartburg College for giving me sabbatical support to work on the book.Last,but most importantly,I acknowledge my wife,Lib Bingham,who
Preface rial throughout the book, but the later sections (String formatting and File Processing) can be delayed until later in the course, if desired. Chapters 5-8 (Graphics and Objects, Defining Functions, Decision Structures, and Loop Structures and Booleans) are designed to stand independently and can be taken in virtually any order. Chapters 9-12 are written to be taken in order, but the material in Chapter 1 1 could easily be moved earlier, should the instructor want to cover lists (arrays) before covering design. Instructors wishing to emphasize object-oriented design need not spend much time on Chapter 9. Chapter 13 contains more advanced material that may be covered at the end or interspersed at vari- ous places throughout the course. Acknowledgments My approach to CSl has been influenced over the years by the many fine text- books that I have read and used for classes. Much that I have learned from those books has undoubtedly found its way into these pages. There are a few specific authors whose approaches have been so important that I feel they deserve special mention. A.K. Dewdney has always had a knack for finding simple examples that illustrate complex issues; I have borrowed a few of those and given them new legs in Python. I also owe a debt to wonderful textbooks from both Owen Astrachan and Cay Horstmann. The graphics library I introduce in Chapter 5 was directly inspired by my experience teaching with a similar library designed by Horstmann. I also learned much about teaching computer science from Nell Dale, for whom I was fortunate enough to serve as a TA when I was a graduate student at the University of Texas. Many people have contributed either directly or indirectly to the production of this book. I am grateful to Dave Reed at Capital University who used early versions and offered numerous insightful suggestions. I have also received much help and encouragement from my colleagues at Wartburg College: Lynn Olson, who offered unflagging support; and Josef Breutzmann, who supplied many project ideas. I also want to acknowledge the fine folks at Franklin, Beedle, and Associates, including Christine Collier and Krista Brown, and especially Jim Leisy Tom Sumnei; and Dean Lake, who turned my pet project into a real textbook. Thanks also goes to all of my students, who have taught me so much about teaching, and to Wartburg College for giving me sabbatical support to work on the book. Last, but most importantly, I acknowledge my wife. Lib Bingham, who XIII
xiv Preface has served as editor,advisor and morale booster while putting up with me throughout this project. I also thank the following individuals who read and commented on parts or all of the manuscript: Rus May Morehead State University Carolyn Miller North Carolina State University Guido van Rossum Elemental Security Jim Sager California State University,Chico Christine Shannon Centre College Paul Tymann Rochester Institute of Technology Suzanne Westbrook University of Arizona -JMZ
XIV Preface has served as editor, advisoi; and morale booster while putting up with me throughout this project. I also thank the following individuals who read and commented on parts or all of the manuscript: Rus May Carolyn Miller Guido van Rossum Jim Sager Christine Shannon Paul Tymann Suzanne Westbrook Morehead State University North Carolina State University Elemental Security California State University Chico Centre College Rochester Institute of Technology University of Arizona —JMZ