Contents

Preface

1 Introduction to Computer Science

1.1 Computer Science

What Do Computing Professionals Do?

Models, Algorithms, and Programs

Tools of the Trade

What Is Computer Science?

1.2 Computer Systems

Computer Hardware

Operating Systems

Networks and Network Protocols

Programming Languages

Software Libraries

1.3 Python Programming Language

Short History of Python

Setting Up the Python Development Environment

1.4 Computational Thinking

A Sample Problem

Abstraction and Modeling

Algorithm

Data Types

Assignments and Execution Control Structures

Chapter Summary

2 Python Data Types

2.1 Expressions, Variables, and Assignments

Algebraic Expressions and Functions

Boolean Expressions and Operators

Variables and Assignments

Variable Names

2.2 Strings

String Operators

Indexing Operator

2.3 Lists

List Operators

Lists Are Mutable, Strings Are Not

List Methods

2.4 Objects and Classes

Object Type

Valid Values for Number Types

Operators for Number Types

Creating Objects

Implicit Type Conversions

Explicit Type Conversions

Class Methods and Object-Oriented Programming

2.5 Python Standard Library

Module math

Module fractions

2.6 Case Study: Turtle Graphics Objects

Chapter Summary

Solutions to Practice Problems

Exercises

3 Imperative Programming

3.1 Python Programs

Our First Python Program

Python Modules

Built-In Function print()

Interactive Input with input()

Function eval()

3.2 Execution Control Structures

One-Way Decisions

Two-Way Decisions

Iteration Structures

Nesting Control Flow Structures

Function range()

3.3 User-Defined Functions

Our First Function

print() versus return

Function Definitions Are “Assignment” Statements

Comments

Docstrings

3.4 Python Variables and Assignments

Mutable and Immutable Types

Assignments and Mutability

Swapping

3.5 Parameter Passing

Immutable Parameter Passing

Mutable Parameter Passing

3.6 Case Study: Automating Turtle Graphics

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

4 Text Data, Files, and Exceptions

4.1 Strings, Revisited

String Representations

The Indexing Operator, Revisited

String Methods

4.2 Formatted Output

Function print()

String Method format()

Lining Up Data in Columns

4.3 Files

File System

Opening and Closing a File

Patterns for Reading a Text File

Writing to a Text File

4.4 Errors and Exceptions

Syntax Errors

Built-In Exceptions

4.5 Case Study: Logging File Access

A Thin Wrapper Function

Logging File Names

Getting and Formatting the Date and Time

Final Implementation of openLog()

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

5 Execution Control Structures

5.1 Decision Control and the if Statement

Three-Way (and More!) Decisions

Ordering of Conditions

5.2 for Loop and Iteration Patterns

Loop Pattern: Iteration Loop

Loop Pattern: Counter Loop

Loop Pattern: Accumulator Loop

Accumulating Different Types

Loop Patterns: Nested Loop

5.3 More on Lists: Two-Dimensional Lists

Two-Dimensional Lists

Two-Dimensional Lists and the Nested Loop Pattern

5.4 while Loop

5.5 More Loop Patterns

Iteration Patterns: Sequence Loop

Loop Pattern: Infinite Loop

Loop Pattern: Loop and a Half

5.6 Additional Iteration Control Statements

break Statement

continue Statement

pass Statement

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

6 Containers and Randomness

6.1 Dictionaries

User-Defined Indexes as Motivation for Dictionaries

Dictionary Class Properties

Dictionary Operators

Dictionary Methods

A Dictionary as a Substitute for Multiway Condition

Dictionary as a Collection of Counters

6.2 Other Built-In Container Types

Class tuple

tuple Objects Can Be Dictionary Keys

Dictionary Method items(), Revisited

Class set

Using the set Constructor to Remove Duplicates

set Operators

set Methods

6.3 Character Encodings and Strings

Character Encodings

ASCII

Unicode

UTF-8 Encoding for Unicode Characters

6.4 Module random

Choosing a Random Integer

Choosing a Random “Real”

Shuffling, Choosing, and Sampling at Random

6.5 Case Study: Games of Chance

Blackjack

Creating and Shuffling the Deck of Cards

Dealing a Card

Computing the Value of a Hand

Comparing the Player's and the House's Hands

Main Blackjack Function

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

7 Namespaces

7.1 Encapsulation in Functions

Code Reuse

Modularity (or Procedural Decomposition)

Encapsulation (or Information Hiding)

Local Variables

Namespaces Associated with Function Calls

Namespaces and the Program Stack

7.2 Global versus Local Namespaces

Global Variables

Variables with Local Scope

Variables with Global Scope

Changing Global Variables Inside a Function

7.3 Exceptional Control Flow

Exceptions and Exceptional Control Flow

Catching and Handling Exceptions

The Default Exception Handler

Catching Exceptions of a Given Type

Multiple Exception Handlers

Controlling the Exceptional Control Flow

7.4 Modules as Namespaces

Module Attributes

What Happens When Importing a Module

Module Search Path

Top-Level Module

Different Ways to Import Module Attributes

7.5 Classes as Namespaces

A Class Is a Namespace

Class Methods Are Functions Defined in the Class Namespace

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

8 Object-Oriented Programming

8.1 Defining a New Python Class

Methods of Class Point

A Class and Its Namespace

Every Object Has an Associated Namespace

Implementation of Class Point

Instance Variables

Instances Inherit Class Attributes

Class Definition, More Generally

Documenting a Class

Class Animal

8.2 Examples of User-Defined Classes

Overloaded Constructor Operator

Default Constructor

Playing Card Class

8.3 Designing New Container Classes

Designing a Class Representing a Deck of Playing Cards

Implementing the Deck (of Cards) Class

Container Class Queue

Implementing a Queue Class

8.4 Overloaded Operators

Operators Are Class Methods

Making the Class Point User Friendly

Contract between the Constructor and the repr() Operator

Making the Queue Class User Friendly

8.5 Inheritance

Inheriting Attributes of a Class

Class Definition, in General

Overriding Superclass Methods

Extending Superclass Methods

Implementing a Queue Class by Inheriting from list

8.6 User-Defined Exceptions

Raising an Exception

User-Defined Exception Classes

Improving the Encapsulation of Class Queue

8.7 Case Study: Indexing and Iterators

Overloading the Indexing Operators

Iterators and OOP Design Patterns

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

9 Graphical User Interfaces

9.1 Basics of tkinter GUI Development

Widget Tk: The GUI Window

Widget Label for Displaying Text

Displaying Images

Packing Widgets

Arranging Widgets in a Grid

9.2 Event-Based tkinter Widgets

Button Widget and Event Handlers

Events, Event Handlers, and mainloop()

The Entry Widget

Text Widget and Binding Events

Event Patterns and the tkinter Class Event

9.3 Designing GUIs

Widget Canvas

Widget Frame as an Organizing Widget

9.4 OOP for GUIs

GUI OOP Basics

Shared Widgets Are Assigned to Instance Variables

Shared Data Are Assigned to Instance Variables

9.5 Case Study: Developing a Calculator

The Calculator Buttons and Passing Arguments to Handlers

Implementing the “Unofficial” Event Handler click()

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

10 Recursion

10.1 Introduction to Recursion

Recursive Functions

Recursive Thinking

Recursive Function Calls and the Program Stack

10.2 Examples of Recursion

Recursive Number Sequence Pattern

Fractals

Virus Scanner

10.3 Run Time Analysis

The Exponent Function

Counting Operations

Fibonacci Sequence

Experimental Analysis of Run Time

10.4 Searching

Linear Search

Binary Search

Other Search Problems

10.5 Case Study: Tower of Hanoi

Classes Peg and Disk

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

11 The Web and Search

11.1 The World Wide Web

Web Servers and Web Clients

“Plumbing” of the WWW

Naming Scheme: Uniform Resource Locator

Protocol: HyperText Transfer Protocol

HyperText Markup Language

HTML Elements

Tree Structure of an HTML Document

Anchor HTML Element and Absolute Links

Relative Links

11.2 Python WWW API

Module urllib.request

Module html.parser

Overriding the HTMLParser Handlers

Module urllib.parse

Parser That Collects HTTP Hyperlinks

11.3 String Pattern Matching

Regular Expressions

Python Standard Library Module re

11.4 Case Study: Web Crawler

Recursive Crawler, Version 0.1

Recursive Crawler, Version 0.2

The Web Page Content Analysis

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

12 Databases and Data Processing

12.1 Databases and SQL

Database Tables

Structured Query Language

Statement SELECT

Clause WHERE

Built-In SQL Functions

Clause GROUP BY

Making SQL Queries Involving Multiple Tables

Statement CREATE TABLE

Statements INSERT and UPDATE

12.2 Database Programming in Python

Database Engines and SQLite

Creating a Database with sqlite3

Committing to Database Changes and Closing the Database

Querying a Database Using sqlite3

12.3 Functional Language Approach

List Comprehension

MapReduce Problem Solving Framework

MapReduce, in the Abstract

Inverted Index

12.4 Parallel Computing

Parallel Computing

Class Pool of Module multiprocessing

Parallel Speedup

MapReduce, in Parallel

Parallel versus Sequential MapReduce

Chapter Summary

Solutions to Practice Problems

Exercises

Problems

Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset