0%

Book Description

Python Programming in Context, Third Edition provides a comprehensive and accessible introduction to Python fundamentals. Updated with the latest version of Python, the new Third Edition offers a thorough overview of multiple applied areas, including image processing, cryptography, astronomy, the Internet, and bioinformatics. Taking an active learning approach, each chapter starts with a comprehensive real-world project that teaches core design techniques and Python programming while engaging students. An ideal first language for learners entering the rapidly expanding field of computer science, Python gives students a solid platform of key problem-solving skills that translate easily across programming languages.

Table of Contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. Contents
  6. Preface
  7. 1 Introduction to Pythona
    1. 1.1 Objectives
    2. 1.2 What Is Computer Science?
    3. 1.3 Why Study Computer Science?
      1. 1.3.1 Everyday Applications of Computer Science
      2. 1.3.2 Why Computer Science Is Important
    4. 1.4 Problem-Solving Strategies
    5. 1.5 Python Overview
      1. 1.5.1 Primitive Elements
      2. 1.5.2 Naming Objects
      3. 1.5.3 Abstraction
      4. 1.5.4 Repetition
    6. 1.6 Summary
      1. Key Terms
      2. Python Keywords
      3. Programming Exercises
  8. 2 πthon: Estimating Pi
    1. 2.1 Objectives
    2. 2.2 What Is Pi?
    3. 2.3 More About the math Module
    4. 2.4 The Archimedes Approach
      1. 2.4.1 The Python Implementation
      2. 2.4.2 Developing a Function to Compute Pi
    5. 2.5 Accumulator Approximations
      1. 2.5.1 The Accumulator Pattern
      2. 2.5.2 Summation of Terms: The Leibniz Formula
      3. 2.5.3 Product of Terms: The Wallis Formula
    6. 2.6 A Monte Carlo Simulation
      1. 2.6.1 Boolean Expressions
      2. 2.6.2 Compound Boolean Expressions and Logical Operators
      3. 2.6.3 Selection Statements
      4. 2.6.4 Completing the Implementation
      5. 2.6.5 Adding Graphics
    7. 2.7 Summary
      1. Key Terms
      2. Python Keywords, Modules and Commands
      3. Programming Exercises
  9. 3 Codes and Other Secrets
    1. 3.1 Objectives
    2. 3.2 The String Data Type
      1. 3.2.1 Concatenation
      2. 3.2.2 Repetition
      3. 3.2.3 Indexing
      4. 3.2.4 String Slicing
      5. 3.2.5 String Searching
      6. 3.2.6 String Methods
      7. 3.2.7 Character Functions
    3. 3.3 Encoding and Decoding Messages
    4. 3.4 Transposition Cipher
      1. 3.4.1 Encrypting Using Transposition
      2. 3.4.2 Decrypting a Transposed Message
      3. 3.4.3 Asking for Input
    5. 3.5 Substitution Cipher
    6. 3.6 Creating a Key
    7. 3.7 The Vigenère Cipher
    8. 3.8 Summary
      1. Key Terms
      2. New Python Keywords, Functions, and Constants
      3. Programming Exercises
  10. 4 Introducing the Python Collections
    1. 4.1 Objectives
    2. 4.2 What Is Data?
    3. 4.3 Storing Data for Processing
      1. 4.3.1 Strings Revisited
      2. 4.3.2 Lists
    4. 4.4 Calculating Statistics on Data
      1. 4.4.1 Simple Dispersion
    5. 4.5 Central Tendency
      1. 4.5.1 Mean
      2. 4.5.2 Median
      3. 4.5.3 Mode
    6. 4.6 Frequency Distribution
      1. 4.6.1 Using a Dictionary to Compute a Frequency Table
      2. 4.6.2 Computing a Frequency Table Without a Dictionary
      3. 4.6.3 Visualizing a Frequency Distribution
    7. 4.7 Dispersion: Standard Deviation
    8. 4.8 Summary
      1. Key Terms
      2. Python Keywords, Functions, and Methods
      3. Programming Exercises
  11. 5 Bigger Data: File I/O
    1. 5.1 Objectives
    2. 5.2 Using Files for Large Data Sets
      1. 5.2.1 Text Files
      2. 5.2.2 Iterating over Lines in a File
      3. 5.2.3 Writing a File
      4. 5.2.4 String Formatting
      5. 5.2.5 Alternative File-Reading Methods
    3. 5.3 Reading Data from the Internet
      1. 5.3.1 Using CSV Files
      2. 5.3.2 Using a while Loop to Process Data
      3. 5.3.3 List Comprehension
      4. 5.3.4 Reading JSON Data from the Internet
    4. 5.4 Correlating Data
    5. 5.5 Summary
      1. Key Terms
      2. Python Keywords and Functions
      3. Programming Exercise
  12. 6 Image Processing
    1. 6.1 Objectives
    2. 6.2 What Is Digital Image Processing?
      1. 6.2.1 The RGB Color Model
      2. 6.2.2 The cImage Module
    3. 6.3 Basic Image Processing
      1. 6.3.1 Negative Images
      2. 6.3.2 Grayscale
      3. 6.3.3 A General Solution: The Pixel Mapper
    4. 6.4 Parameters, Parameter Passing, and Scope
      1. 6.4.1 Call by Assignment Parameter Passing
      2. 6.4.2 Namespaces
      3. 6.4.3 Calling Functions and Finding Names
      4. 6.4.4 Modules and Namespaces
    5. 6.5 Advanced Image Processing
      1. 6.5.1 Resizing
      2. 6.5.2 Stretching: A Different Perspective
      3. 6.5.3 Flipping an Image
      4. 6.5.4 Edge Detection
    6. 6.6 Summary
      1. Key Terms
      2. Python Keywords, Functions, and Variables
      3. Programming Exercises
  13. 7 Data Mining: Cluster Analysis
    1. 7.1 Objectives
    2. 7.2 What Is Data Mining?
    3. 7.3 Cluster Analysis: A Simple Example
    4. 7.4 Implementing Cluster Analysis on Simple Data
      1. 7.4.1 Distance Between Two Points
      2. 7.4.2 Clusters and Centroids
      3. 7.4.3 The K-Means Cluster Analysis Algorithm
      4. 7.4.4 Implementation of K-Means
      5. 7.4.5 Implementation of K-Means, Continued
    5. 7.5 Implementing Cluster Analysis: Earthquakes
      1. 7.5.1 File Processing
      2. 7.5.2 Visualization
    6. 7.6 Cluster Analysis Shortcomings and Solutions
    7. 7.7 Summary
      1. Key Terms
      2. Python Keywords
      3. Programming Exercises
  14. 8 Cryptanalysis
    1. 8.1 Objectives
    2. 8.2 Introduction
    3. 8.3 Cracking the Rail Fence
      1. 8.3.1 Checking Our Work with a Dictionary
      2. 8.3.2 A Brute-Force Solution
      3. 8.3.3 A Rail Fence Decryption Algorithm
    4. 8.4 Cracking the Substitution Cipher
      1. 8.4.1 Letter Frequency
      2. 8.4.2 Ciphertext Frequency Analysis
      3. 8.4.3 Letter Pair Analysis
      4. 8.4.4 Word Frequency Analysis
      5. 8.4.5 Pattern Matching with Partial Words
      6. 8.4.6 Regular Expression Summary
    5. 8.5 Summary
      1. Key Terms
      2. Python Functions, Methods, and Keywords
      3. Programming Exercises
  15. 9 Fractals: The Geometry of Nature
    1. 9.1 Objectives
    2. 9.2 Introduction
    3. 9.3 Recursive Programs
      1. 9.3.1 Recursive Squares
      2. 9.3.2 Classic Recursive Functions
      3. 9.3.3 Drawing a Recursive Tree
      4. 9.3.4 The Sierpinski Triangle
      5. 9.3.5 Call Tree for a Sierpinski Triangle
    4. 9.4 Snowflakes, Lindenmayer, and Grammars
      1. 9.4.1 L-Systems
      2. 9.4.2 Automatically Expanding Production Rules
      3. 9.4.3 More Advanced L-Systems
    5. 9.5 Summary
      1. Key Terms
      2. Programming Exercises
  16. 10 Planet Objects
    1. 10.1 Objectives
    2. 10.2 Introduction
      1. 10.2.1 Programming
      2. 10.2.2 Object-Oriented Programming
      3. 10.2.3 Python Classes
    3. 10.3 Designing and Implementing a Planet Class
      1. 10.3.1 Constructor Method
      2. 10.3.2 Accessor Methods
      3. 10.3.3 Mutator Methods
      4. 10.3.4 Special Methods
      5. 10.3.5 Methods and self
      6. 10.3.6 Details of Method Storage and Lookup
    4. 10.4 Designing and Implementing a Sun Class
    5. 10.5 Designing and Implementing a Solar System
    6. 10.6 Animating the Solar System
      1. 10.6.1 Using Turtles
      2. 10.6.2 Planetary Orbits
      3. 10.6.3 Implementation
    7. 10.7 Summary
      1. Key Terms
      2. Python Keywords and Functions
      3. Programming Exercises
  17. 11 Simulation
    1. 11.1 Objectives
    2. 11.2 Bears and Fish
    3. 11.3 What Is a Simulation?
    4. 11.4 Rules of the Game
    5. 11.5 Design
    6. 11.6 Implementation
      1. 11.6.1 The World Class
      2. 11.6.2 The Fish Class
      3. 11.6.3 The Bear Class
      4. 11.6.4 Main Simulation
    7. 11.7 Growing Plants
    8. 11.8 A Note on Inheritance
    9. 11.9 Summary
      1. Key Terms
      2. Python Keywords and Functions
      3. Programming Exercises
  18. 12 Father Was a Rectangle
    1. 12.1 Objectives
    2. 12.2 Introduction
    3. 12.3 First Design
    4. 12.4 Basic Implementation
      1. 12.4.1 The Canvas Class
      2. 12.4.2 The GeometricObject Class
      3. 12.4.3 The Point Class
      4. 12.4.4 The Line Class
      5. 12.4.5 Testing Our Implementation
    5. 12.5 Understanding Inheritance
    6. 12.6 Limitations
    7. 12.7 An Improved Implementation
    8. 12.8 Implementing Polygons
    9. 12.9 Summary
      1. Key Terms
      2. Python Keywords, Methods, and Decorator
      3. Programming Exercises
  19. 13 Video Games
    1. 13.1 Objectives
    2. 13.2 Introduction
      1. 13.2.1 Event-Driven Programming
      2. 13.2.2 Simulating an Event Loop
      3. 13.2.3 A Multithreaded Event Loop
    3. 13.3 Event-Driven Programming with the Turtle
      1. 13.3.1 A Simple Etch-a-Sketch Using Key Presses
      2. 13.3.2 Placing Turtles Using Mouse Clicks
      3. 13.3.3 Bouncing Turtles
    4. 13.4 Creating Your Own Video Game
      1. 13.4.1 The LaserCannon Class
      2. 13.4.2 The BoundedTurtle Class
      3. 13.4.3 The Drone Class
      4. 13.4.4 The Bomb Class
      5. 13.4.5 Putting All the Pieces Together
    5. 13.5 Summary
      1. Key Terms
      2. Python Keywords and Decorator
      3. Programming Exercises
  20. APPENDIX A Installing the Required Software
    1. A.1 Installing Python
    2. A.2 Installing the Python Image Library and cImage
  21. APPENDIX B Python Quick Reference
    1. B.1 Python Reserved Words
    2. B.2 Numeric Data Types
    3. B.3 Built-in Functions
    4. B.4 Sequence Operators
    5. B.5 Dictionaries
    6. B.6 Files
    7. B.7 Formatting Output
    8. B.8 Iteration
    9. B.9 Boolean Expressions
    10. B.10 Selection
    11. B.11 Python Modules
    12. B.12 Regular Expression Patterns
    13. B.13 Defining Functions
    14. B.14 Defining Classes
    15. B.15 Deleting Objects
    16. B.16 Common Error Messages
  22. APPENDIX C turtle Reference
    1. C.1 Basic Move and Draw
    2. C.2 Turtle State
    3. C.3 Drawing State
    4. C.4 Filling
    5. C.5 More Drawing Control
    6. C.6 Controlling the Shape and Appearance
    7. C.7 Measurement Settings
    8. C.8 Drawing Speed
    9. C.9 Color
    10. C.10 Events
    11. C.11 Miscellaneous
  23. APPENDIX D Answers to Selected “Try It Out” Exercises
  24. INDEX
18.220.16.184