Python Programming: An Introduction to Computer Science
This is an introductory college-level textbook designed to teach the fundamental principles of computer science using Python as the primary programming language. It focuses on problem-solving, design, and programming through a spiral coverage model and a graphics-oriented approach.
課程總覽
📚 Content Summary
This is an introductory college-level textbook designed to teach the fundamental principles of computer science using Python as the primary programming language. It focuses on problem-solving, design, and programming through a spiral coverage model and a graphics-oriented approach.
A gentle and comprehensive introduction to the art of computer science through the lens of Python.
Author: John M. Zelle
Acknowledgments: Guido van Rossum (Foreword), Jim Leisy, and academic colleagues from Wartburg College and various universities including Owen Astrachan, Cay Horstmann, and Nell Dale.
🎯 Learning Objectives
- Distinguish between hardware/software and algorithms/programs.
- Explain how a Python interpreter translates high-level code into executable instructions via byte code.
- Define and invoke simple Python functions using parameters and arguments.
- List and describe the six stages of the Software Development Process.
- Apply Python syntax rules to create valid identifiers and mathematical expressions with correct operator precedence.
- Differentiate between the "Box" and "Sticky Note" variable models and explain the process of evaluation and assignment.
- Distinguish between
intandfloatdata types and apply explicit type conversions. - Implement the accumulator pattern to solve mathematical problems like factorials.
- Utilize the Python
mathlibrary to solve algebraic equations, including the quadratic formula. - Define and apply core OO concepts: objects, methods, accessors, mutators, and aliasing.
🔹 Lesson 1: Introduction to Computer Science and Python Basics
Overview: This lesson introduces computer science as the study of computable processes, rather than just the study of physical machines. Students will explore the concept of the "Universal Machine," where software provides the power to transform hardware into any tool imaginable. The session concludes with a technical introduction to Python basics, including its interpreter-based execution, function definitions, and the distinction between syntax and semantics.
Learning Outcomes:
- Distinguish between hardware/software and algorithms/programs.
- Explain how a Python interpreter translates high-level code into executable instructions via byte code.
- Define and invoke simple Python functions using parameters and arguments.
🔹 Lesson 2: The Software Development Process and Simple Programs
Overview: This lesson introduces the systematic approach to creating software through the six-step development process and the fundamental building blocks of Python programming. Students will explore basic syntax, including identifiers, expressions, and assignment, while contrasting the "sticky note" variable model with traditional concepts. The lesson concludes with the implementation of definite loops using the range() function to handle repetitive calculations.
Learning Outcomes:
- List and describe the six stages of the Software Development Process.
- Apply Python syntax rules to create valid identifiers and mathematical expressions with correct operator precedence.
- Differentiate between the "Box" and "Sticky Note" variable models and explain the process of evaluation and assignment.
🔹 Lesson 3: Numeric Data Representation and the Math Library
Overview: This lesson explores how computers represent and manipulate numerical data, distinguishing between discrete whole numbers (integers) and approximations of real numbers (floating-point). Students will learn to perform complex calculations using Python's built-in operators and the math library, while understanding the underlying limitations of binary representation and precision.
Learning Outcomes:
- Distinguish between
intandfloatdata types and apply explicit type conversions. - Implement the accumulator pattern to solve mathematical problems like factorials.
- Utilize the Python
mathlibrary to solve algebraic equations, including the quadratic formula.
🔹 Lesson 4: Object-Oriented Graphics and Interactive GUIs
Overview: This lesson transitions students from text-based programming to Graphical User Interfaces (GUIs) using an Object-Oriented (OO) approach. Students will learn to manipulate graphical objects (points, lines, shapes), manage coordinate systems, and create interactive programs that respond to mouse clicks, keyboard strokes, and text entries.
Learning Outcomes:
- Define and apply core OO concepts: objects, methods, accessors, mutators, and aliasing.
- Construct and display graphical objects including Points, Lines, Circles, Rectangles, Ovals, and Polygons.
- Implement coordinate transformations and interactive elements (mouse/keyboard/entry boxes) to create functional GUIs.
🔹 Lesson 5: Sequences: Strings, Lists, and File I/O
Overview: This lesson explores how computer science represents and manipulates sequences of data, specifically focusing on strings and lists. Students will learn the mechanics of indexing and slicing, the critical distinction between mutable and immutable objects, and how to interface with external data through file processing. Additionally, the lesson covers data encoding via Unicode and the foundational principles of cryptography using basic ciphers.
Learning Outcomes:
- Perform advanced string and list manipulations using indexing, slicing, and built-in methods (split, join, upper, lower).
- Design and implement basic encoding/decoding algorithms and cryptographic ciphers (Caesar and Substitution).
- Develop batch-processing programs that open, read, process, and close external data files.
🔹 Lesson 6: Defining Functions and Variable Scope
Overview: This lesson explores the transition from writing monolithic scripts to creating modular programs using functions. It covers the syntax for defining and calling functions, the mechanism of passing information via formal and actual parameters, and how functions communicate results back through return values (including multiple values and the None object). Students will also learn about variable scope—specifically the distinction between local and global variables—and how functions can modify mutable parameters to influence program state.
Learning Outcomes:
- Demonstrate how to define and invoke functions to reduce code duplication and improve program structure.
- Differentiate between formal and actual parameters and explain positional parameter matching.
- Implement functions that return single values, multiple values, or default to the
Noneobject.
🔹 Lesson 7: Decision Structures and Exception Handling
Overview: This lesson introduces decision structures, which allow programs to execute different sequences of instructions based on specific conditions. Students will learn to form Boolean expressions using relational operators, implement one-way, two-way, and multi-way decisions, and use nesting for complex logic. The lesson also covers modular execution with the __name__ variable and robust error management through exception handling.
Learning Outcomes:
- Implement simple, two-way, and multi-way decision structures using
if,else, andelif. - Construct and evaluate Boolean expressions using relational operators and lexicographic ordering for strings.
- Apply the
try-exceptpattern to handle runtime errors and prevent program crashes.
🔹 Lesson 8: Indefinite Loops and Boolean Logic
Overview: This lesson transitions from definite "counted" loops to indefinite loops, which execute based on conditions rather than a fixed sequence. Students will master the while statement and various design patterns for data processing, including interactive, sentinel, and file-based loops. Additionally, the lesson covers Boolean Logic, providing the algebraic tools necessary to construct and simplify complex decision-making structures in code.
Learning Outcomes:
- Design and implement indefinite loops using the Python
whilestatement. - Apply common loop patterns, including interactive loops, sentinel loops, and the priming read pattern.
- Simplify complex logical conditions using Boolean Algebra and DeMorgan’s Laws.
🔹 Lesson 9: Simulation, Design, and Stepwise Refinement
Overview: This lesson explores the systematic process of developing complex computer programs through Top-Down Design and Stepwise Refinement. Students will learn to decompose large problems into manageable "chunks" using abstraction and modularization, visualize program hierarchies via Structure Charts, and implement simulations using Monte Carlo techniques. The curriculum also covers iterative development strategies like Prototyping and Spiral Development, alongside robust testing methods like Unit Testing.
Learning Outcomes:
- Decompose a complex problem into smaller, solvable sub-problems using Top-Down Design.
- Construct and interpret Structure Charts that illustrate data flow (parameters and return values) between modules.
- Apply Monte Carlo simulation techniques to model probabilistic events using Python's
randomlibrary.
🔹 Lesson 10: Defining Classes and Event-Driven Programming
Overview: This lesson transitions from procedural, function-based design to object-based design using Python classes. Students will learn how to encapsulate data (instance variables) and behaviors (methods) within a single unit, create interactive graphical user interface (GUI) widgets, and manage program flow through event-driven programming and synchronized animation loops.
Learning Outcomes:
- Differentiate between function-based and object-based program designs.
- Define custom Python classes using constructors (
__init__), instance variables, and theselfparameter. - Implement encapsulation and documentation using docstrings.
🔹 Lesson 11: Data Collections: Advanced Lists and Dictionaries
Overview: This lesson explores advanced data collection techniques in Python, focusing on the versatile nature of lists and dictionaries. Students will learn to distinguish lists from traditional arrays, perform complex list operations like slicing and record sorting, and apply non-sequential mapping through dictionaries. The module culminates in applying these structures to statistical analysis and classic algorithms like the Sieve of Eratosthenes.
Learning Outcomes:
- Contrast Python's dynamic lists with fixed-size, homogeneous arrays.
- Perform advanced list manipulations including slicing, selective deletion, and custom sorting of object records.
- Implement statistical algorithms (Mean, Median, Standard Deviation) and table-driven designs using collections.
🔹 Lesson 12: Object-Oriented Design (OOD) Principles
Overview: This lesson explores the transition from top-down functional design to Object-Oriented Design (OOD), a data-centered approach where systems are described as a collection of interacting "black boxes" (objects). Students will learn to identify candidate objects and methods, implement multi-class simulations like Racquetball and Dice Poker, and apply the three pillars of OOP: Encapsulation, Polymorphism, and Inheritance. The lesson culminates in managing complex graphical user interface (GUI) interactions through subclassing and method overriding.
Learning Outcomes:
- Identify potential classes and methods from a problem statement by analyzing nouns and verbs.
- Implement a complex, multi-object simulation using structured classes (e.g.,
Player,Game,Stats). - Distinguish and apply the fundamental concepts of encapsulation, polymorphism, and inheritance.
🔹 Lesson 13: Algorithm Design, Recursion, and Efficiency
Overview: This lesson explores the fundamental techniques used to solve computational problems efficiently. It covers searching strategies, the mechanics of recursive problem solving (base cases and recursive steps), and a comparison of sorting algorithms like Selection Sort and Merge Sort to understand how algorithm design impacts performance.
Learning Outcomes:
- Compare and implement Linear and Binary Search strategies.
- Design recursive functions using base cases and recursive steps for problems like string reversal and fast exponentiation.
- Analyze the performance trade-offs between recursion and iteration and between different sorting algorithms (n^2 vs. n \log n).