Computer science deals with people who have problems to solve. Algorithms help find the solutions to those problems. To be a computer scientist means first and foremost that you are a problem solver, capable of constructing algorithms either from scratch or by applying patterns from past experience.
The only way to be successful in learning computer science is through deliberate and incremental exposure to the fundamental ideas of the discipline. A beginning student of computer science needs practice to establish a thorough understanding of a concept before continuing on to the more complex parts of the curriculum. In addition, a beginner needs the opportunity to be successful and to gain confidence. As students progress through the introductory computer science sequence, we want them to focus on aspects of problem solving, algorithm development, and algorithm understanding.
In this text, we use Python as the programming language because it has a clean, simple syntax and an intuitive user environment. The basic collections are powerful, yet easy to use. The interactive nature of the language creates a place to test ideas without the need for a lot of coding. Finally, Python provides a textbook-like notation for representing algorithms, alleviating the need for an additional layer of pseudocode. This allows the illustration of many relevant, modern, and interesting problems that make use of algorithms.
This text is designed to be a first course in computer science that focuses on problem solving, with language features being introduced as needed to solve the problem at hand. We have structured the text around problems of general interest, rather than a traditional language-element structure. Thus, you will not see chapter titles in this text like “Loops” and “Conditionals”—but you will see chapter titles such as “Planet Objects” and “Codes and Other Secrets.”
Throughout the text, concepts are introduced using a spiral model. Because the syntax of Python is easy to learn, we can quickly introduce the basics of standard programming constructs. As students progress through the text, more of the details and background for these constructs are added to their toolbox. In this way, students are exposed to important computer science concepts at the point when those concepts are needed to solve a problem.
An illustration of how this spiral approach touches on a specific topic in different ways can be seen in our presentation of functions. Students begin to write functions with parameters in Chapter 1. Chapter 2 then introduces functions with return values. In Chapter 6, students learn about passing functions as parameters to other functions and the details of Python’s scoping rules. In Chapter 8, they encounter Python’s keyword and optional parameters. Chapter 9 covers recursive functions. In Chapter 10, students learn about writing functions that are methods of a class. In Chapter 12, students learn to write abstract methods.
The Third Edition contains many updates and new features found in Python version 8.
While keeping the problem-solving approach, we have also added many pedagogical aids in the third edition to better illustrate and highlight Python programming constructs.
As an example, each newly introduced function or method is summarized in table format:
TABLE 3.5 The |
|
---|---|
Function | Description |
input(prompt) |
Outputs the prompt, then returns a string containing any characters typed by the user when the user presses enter or return. The enter or return key is not part of the returned string. |
In addition, the following elements are all new to this edition:
▶ Recap boxes summarize important points.
▶ Heads Up boxes alert students to possible pitfalls and provide techniques for avoiding errors.
▶ Good Programming Practice boxes provide problem-solving strategies, as well as tips for writing readable, maintainable code.
▶ All Python code is color-coded so the student can better see syntax elements.
▶ Figures are also presented in color—an important improvement for several turtle applications and image processing programs in which color is used to highlight variations in data.
▶ As a response to instructor and student feedback, we added Appendix D, which provides answers to selected within-chapter Try It Out exercises.
▶ In Chapter 5, Bigger Data, we updated the approaches for accessing online data. New examples process the popular online data formats: CSV and JSON.
▶ Also starting in Chapter 5, we updated string formatting; we now use the string format
method.
▶ In the Planet Objects chapter and beyond, when we create classes, we define the instance variable names using the _ _
prefix to achieve encapsulation.
▶ Variable and function names now follow a standard camel-case scheme.
This text is organized into three parts. Chapters 1–5 introduce all of the key control structures and Python data types, emphasizing straightforward imperative programming constructs such as variables, loops, and conditionals. By the end of the first five chapters, all of the major Python data types have been covered—including integers, floats, strings, lists, dictionaries, tuples, and files.
In the first chapter we introduce the idea of an object as something you can use. In a sense, programming in Python is an objects-always approach. Students begin by using common programming concepts, while employing the modules that Python provides. These modules allow us to address more interesting problems without introducing unnecessary complexity. For example, we cover simple graphics in Chapter 1 by using a turtle graphics module. In Chapter 6, we cover image processing through the use of a simple image object that automatically loads an image from a file but allows the students to get and set the values of pixels.
The next chapters provide more details about the concepts introduced in Chapters 1–5. Chapters 6–9 provide students with an opportunity to get more comfortable with basic programming concepts while introducing additional problem-solving patterns. Students also learn more about the internal mechanisms of Python.
The last chapters emphasize object-oriented programming and introduce the concepts needed to design and build classes. When these topics are introduced, students are comfortable with the idea of using an object; in turn, building their own objects is a natural next step. Our first examples emphasize the importance of interactions between multiple real-world objects. After implementing simple classes, we introduce inheritance in a natural way by creating a graphics library and by implementing video games as an extension of the turtle module.
This text can be used in several ways. FIGURE P.1 provides an overview of some possibilities. One path is to move through the text sequentially. Instructors could either cover the entire text in a single semester or, because there is enough exploratory material to last an entire year, instructors could cover the material in two semesters. A second path would be to cover Chapters 1–5 and then move to Chapters 10–13. This approach provides a balance between imperative and object-oriented programming that fits easily into a single semester. If instructors have additional time, they can pick and choose from Chapters 6–9 before continuing to Chapters 10–13. Another suggestion would be to teach Chapters 1–9 in an introductory course that covers only imperative programming and the use of objects.
This text includes three kinds of exercises. First, some Try It Out exercises ask students to run the code that has been presented as part of the section. With these exercises, students use the code to learn or explore on their own. Second, other Try It Out exercises have students modify or extend the code provided in the section. Many times a simple version of the code is provided and students are asked to modify the code to improve it or add additional features. Third, programming exercises at the end of each chapter describe complete projects. These projects are related to the materials covered in the chapter, but do not extend or modify the code already provided.
Throughout the text we have included exercises within each section. These exercises are structured so that they can be used in several ways:
▶ If you are reading this text as a self-study, the Try It Out exercises provide breaking points where you have the opportunity to stop reading and practice the concepts.
▶ Instructors may find that many of the exercises contain material to cover in a lecture that complements the material the students have read.
▶ In a traditional lecture course, the exercises can be used as homework problems. We have had good luck in the introductory computer science course using many small programming assignments. Often the students are able to do more than 30 small programming assignments during a single semester.
▶ We have also used this text as part of a “hands-on” introduction to computer science. A small amount of lecture can be used to reinforce the main points of a chapter, but the majority of the class time is used to work on the exercises that complement the reading. This approach follows a growing trend in active learning computer science education and was well received by our students.
TABLE P.1 maps the problems that are introduced in each chapter with the key computer science concepts that are established in the chapter.
TABLE P.1 Problems and Key Concepts |
||
---|---|---|
Chapter | Problem Key | Computer Science Concepts |
1. Introduction to Python |
Drawing polygons and approximating a circle; Common problem-solving strategies |
Numeric data types, assignment statements, using turtle objects, simple for loops, simple functions |
2. πthon: Estimating Pi |
Various methods for approximating pi | Accumulator pattern, simple selection statements, Boolean expressions, functions that return values, math module, random module |
3 Codes and Other Secrets |
Cipher algorithms—transposition, substitution algorithms | String data type and methods, modulo arithmetic, user input, selection and iteration with strings |
4. Introducing the Python Collections |
Computing simple statistics | Python list, tuple, and dictionary data types |
5. Bigger Data: File I/O |
Computing statistics with larger data sets | File input and output, while loops, string formatting, reading online data in CSV and JSON formats |
6. Image Processing |
Digital image processing, pixel manipulations, enlarge and reduce images, edge detection algorithms | Nested loops, passing functions as parameters, scoping rules |
7. Data Mining: Cluster Analysis |
Cluster analysis and visualization of large data sets | Python list and dictionary data types, more on reading from files, and more on while loops |
8. Cryptanalysis |
Methods for cracking transposition and substitution ciphers; frequency analysis | Storing nontrivial data in lists and dictionaries, string processing, regular expression module |
9. Fractals: The Geometry of Nature |
What fractals are; how we can use them to simulate trees, shrubs, snowflakes, and other natural objects | Recursion, grammars, and production rules |
10. Planet Objects |
Calculating and plotting the interaction of large celestial bodies | Introduce object-oriented design and construction of classes, instance variables, methods |
11. Simulation |
Simulating predator–prey relationships | Using objects in a simulation; many objects with simple behaviors can lead to interesting complex results |
12. Father Was a Rectangle |
Designing an object-oriented graphics library with points, lines, polygons, and other shapes | Inheritance, polymorphism, using the turtle module to implement a higher-level graphics library |
13. Video Games |
Designing and implementing a simple video game | Event-driven programming, inheritance, static variables, static methods |
Instructor supplements, including answers to chapter exercises, a test bank, and PowerPoint lecture outlines, are available for instructor download. Source code from listings presented in the chapters is also provided for students and instructors. For more information and to request access, contact your account representative at go.jblearning.com/python3e.
If you want to ask a question or if you discover a technical error in the text, please contact Julie Anderson at [email protected]. We will post any corrections on the text’s website.
We would like to acknowledge the contributions to this text from many partners, colleagues, and family members.
First and foremost, thank you to our publisher, Jones & Bartlett Learning, for its help and guidance through the publishing process: Laura Pagluica, Director of Product Management; John Fuller, Project Specialist; and Melissa Duffy, Product Assistant. We also want to thank the compositor, codeMantra; the copy editor, Jill Hobbs; and Kristin Parker, who designed the cover.
Additional thanks go to the following reviewers for their constructive comments and suggestions:
Nathan Backman, Buena Vista University
Jerry Cooley, South Dakota State University
Erica Eddy, University of Wisconsin–Parkside
Susan Eileen Fox, Macalester College
Harry J. Foxwell, George Mason University
Fredrick Glenn, Gwinnett Technical College
Carolyn Granda, Indiana University Southeast
Zack Hubbard, Rowan-Cabarrus Community College
Xiangdong Li, New York City College of Technology, City University of New York
Kevin Lillis, St. Ambrose University
Renita Murimi, Oklahoma Baptist University
David R. Musicant, Carleton College
Jason Myers, Wayne State University
Michel Paquette, Vanier College
Kate Pulling, College of Southern Nevada
Yenumula B. Reddy, Grambling State University
Dennis Roebuck, Delta College
Jay Shaffstall, Muskingum University
Christine Shannon, Centre College
Ivan Temesvari, Oakton Community College
Brad and David would also like to thank our Luther Computer Science colleagues Kent Lee, Steve Hubbard, and Walt Will. We are grateful to our students in CS150 who used early drafts of this text.
Julie would also like to thank her son, Jon Anderson, a data scientist, for his comments and review.
3.134.110.16