1. Trang chủ
  2. » Tất cả

271022754-Python-Book

445 4 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • The Context of Software Development

    • Software

    • Development Tools

    • Learning Programming with Python

    • Writing a Python Program

    • The Python Interactive Shell

    • A Longer Python program

    • Summary

    • Exercises

  • Values and Variables

    • Integer and String Values

    • Variables and Assignment

    • Identifiers

    • Floating-point Numbers

    • Control Codes within Strings

    • User Input

    • The eval Function

    • Controlling the print Function

    • String Formatting

    • Multi-line Strings

    • Summary

    • Exercises

  • Expressions and Arithmetic

    • Expressions

    • Mixed Type Expressions

    • Operator Precedence and Associativity

    • Comments

    • Errors

      • Syntax Errors

      • Run-time Exceptions

      • Logic Errors

    • Arithmetic Examples

    • More Arithmetic Operators

    • Algorithms

    • Summary

    • Exercises

  • Conditional Execution

    • Boolean Expressions

    • Boolean Expressions

    • The Simple if Statement

    • The if/else Statement

    • Compound Boolean Expressions

    • The pass Statement

    • Floating-point Equality

    • Nested Conditionals

    • Multi-way Decision Statements

    • Conditional Expressions

    • Errors in Conditional Statements

    • Summary

    • Exercises

  • Iteration

    • The while Statement

    • Definite Loops vs. Indefinite Loops

    • The for Statement

    • Nested Loops

    • Abnormal Loop Termination

      • The break statement

      • The continue Statement

    • while/else and for/else

    • Infinite Loops

    • Iteration Examples

      • Computing Square Root

      • Drawing a Tree

      • Printing Prime Numbers

      • Insisting on the Proper Input

    • Summary

    • Exercises

  • Using Functions

    • Introduction to Using Functions

    • Standard Mathematical Functions

    • time Functions

    • Random Numbers

    • Importing Issues

    • Summary

    • Exercises

  • Writing Functions

    • Function Basics

    • Parameter Passing

    • Documenting Functions

    • Function Examples

      • Better Organized Prime Generator

      • Command Interpreter

      • Restricted Input

      • Better Die Rolling Simulator

      • Tree Drawing Function

      • Floating-point Equality

    • Custom Functions vs. Standard Functions

    • Summary

    • Exercises

  • More on Functions

    • Global Variables

    • Default Parameters

    • Introduction to Recursion

    • Making Functions Reusable

    • Functions as Data

    • Lambda Expressions

    • Generators

    • Local Function Definitions

    • Summary

    • Exercises

  • Objects

    • Using Objects

    • String Objects

    • File Objects

    • Fraction Objects

    • Turtle Graphics

    • Graphics with tkinter Objects

    • Other Standard Python Objects

    • Object Mutability and Aliasing

    • Garbage Collection

    • Summary

    • Exercises

  • Lists

    • Using Lists

    • List Traversal

    • Building Lists

    • List Membership

    • List Assignment and Equivalence

    • List Bounds

    • Slicing

    • List Element Removal

    • Lists and Functions

    • List Methods

    • Prime Generation with a List

    • Command-line Arguments

    • List Comprehensions

    • Summary of List Creation Techniques

    • Lists vs. Generators

    • Summary

    • Exercises

  • Sorting and Searching

    • Sorting

    • Flexible Sorting

    • Search

      • Linear Search

      • Binary Search

    • Recursion Revisited

    • List Permutations

    • Randomly Permuting a List

    • Reversing a List

    • Summary

    • Exercises

  • Tuples, Dictionaries, and Sets

    • Tuples

    • Arbitrary Argument Lists

    • Dictionaries

    • Using Dictionaries

    • Counting with Dictionaries

    • Grouping with Dictionaries

    • Keyword Arguments

    • Sets

    • Set Quantification with all and any

    • Summary

    • Exercises

  • Handling Exceptions

    • Motivation

    • Common Standard Exceptions

    • Handling Exceptions

    • Handling Multiple Exceptions

    • The Catch-all Handler

    • Catching Exception Objects

    • The try Statement's Optional else Block

    • finally block

    • Exception Handling Scope

    • Raising Exceptions

    • Using Exceptions Wisely

    • Summary

    • Exercises

  • Custom Types

    • Circle Objects

    • Restricting Access to Members

    • Bank Account Objects

    • Stopwatch Objects

    • Automated Testing

    • Class Inheritance

    • Custom Exceptions

    • Summary

    • Exercises

  • Index

Nội dung

Fundamentals of Python Programming D T F RA Richard L Halterman Southern Adventist University June 17, 2015 Fundamentals of Python Programming Copyright © 2015 Richard L Halterman All rights reserved Formerly Learning to Program with Python Copyright © 2011–2015 Richard L Halterman All rights reserved See the preface for the terms of use of this document i Contents The Context of Software Development 1.1 Software 1.2 Development Tools 1.3 Learning Programming with Python 1.4 Writing a Python Program 1.5 The Python Interactive Shell 1.6 A Longer Python program 11 1.7 Summary 12 1.8 Exercises 13 Values and Variables 15 2.1 Integer and String Values 15 2.2 Variables and Assignment 19 2.3 Identifiers 24 2.4 Floating-point Numbers 27 2.5 Control Codes within Strings 30 2.6 User Input 32 2.7 The eval Function 33 2.8 Controlling the print Function 35 2.9 String Formatting 37 2.10 Multi-line Strings 41 2.11 Summary 42 2.12 Exercises 43 Expressions and Arithmetic 47 3.1 47 Expressions ©2015 Richard L Halterman Draft date: June 17, 2015 ii CONTENTS 3.2 Mixed Type Expressions 53 3.3 Operator Precedence and Associativity 53 3.4 Comments 55 3.5 Errors 56 3.5.1 Syntax Errors 56 3.5.2 Run-time Exceptions 57 3.5.3 Logic Errors 59 3.6 Arithmetic Examples 60 3.7 More Arithmetic Operators 62 3.8 Algorithms 64 3.9 Summary 65 3.10 Exercises 67 Conditional Execution 71 4.1 Boolean Expressions 71 4.2 Boolean Expressions 72 4.3 The Simple if Statement 73 4.4 The if/else Statement 78 4.5 Compound Boolean Expressions 80 4.6 The pass Statement 84 4.7 Floating-point Equality 85 4.8 Nested Conditionals 86 4.9 Multi-way Decision Statements 96 4.10 Conditional Expressions 102 4.11 Errors in Conditional Statements 104 4.12 Summary 105 4.13 Exercises 106 Iteration 111 5.1 The while Statement 111 5.2 Definite Loops vs Indefinite Loops 118 5.3 The for Statement 119 5.4 Nested Loops 123 5.5 Abnormal Loop Termination 129 5.5.1 The break statement 130 ©2015 Richard L Halterman Draft date: June 17, 2015 iii CONTENTS 5.5.2 The continue Statement 133 5.6 while/else and for/else 134 5.7 Infinite Loops 136 5.8 Iteration Examples 139 5.9 5.8.1 Computing Square Root 140 5.8.2 Drawing a Tree 141 5.8.3 Printing Prime Numbers 143 5.8.4 Insisting on the Proper Input 146 Summary 147 5.10 Exercises 148 Using Functions 155 6.1 Introduction to Using Functions 156 6.2 Standard Mathematical Functions 160 6.3 time Functions 164 6.4 Random Numbers 166 6.5 Importing Issues 169 6.6 Summary 171 6.7 Exercises 172 Writing Functions 175 7.1 Function Basics 176 7.2 Parameter Passing 190 7.3 Documenting Functions 192 7.4 Function Examples 194 7.4.1 Better Organized Prime Generator 195 7.4.2 Command Interpreter 196 7.4.3 Restricted Input 197 7.4.4 Better Die Rolling Simulator 199 7.4.5 Tree Drawing Function 200 7.4.6 Floating-point Equality 201 7.5 Custom Functions vs Standard Functions 203 7.6 Summary 206 7.7 Exercises 207 ©2015 Richard L Halterman Draft date: June 17, 2015 CONTENTS More on Functions iv 213 8.1 Global Variables 213 8.2 Default Parameters 218 8.3 Introduction to Recursion 219 8.4 Making Functions Reusable 224 8.5 Functions as Data 226 8.6 Lambda Expressions 228 8.7 Generators 231 8.8 Local Function Definitions 237 8.9 Summary 243 8.10 Exercises 244 Objects 249 9.1 Using Objects 249 9.2 String Objects 250 9.3 File Objects 254 9.4 Fraction Objects 261 9.5 Turtle Graphics 262 9.6 Graphics with tkinter Objects 264 9.7 Other Standard Python Objects 267 9.8 Object Mutability and Aliasing 267 9.9 Garbage Collection 271 9.10 Summary 272 9.11 Exercises 273 10 Lists 275 10.1 Using Lists 277 10.2 List Traversal 281 10.3 Building Lists 282 10.4 List Membership 287 10.5 List Assignment and Equivalence 288 10.6 List Bounds 293 10.7 Slicing 294 10.8 List Element Removal 296 10.9 Lists and Functions 297 ©2015 Richard L Halterman Draft date: June 17, 2015 CONTENTS v 10.10List Methods 299 10.11Prime Generation with a List 300 10.12Command-line Arguments 302 10.13List Comprehensions 304 10.14Summary of List Creation Techniques 308 10.15Lists vs Generators 309 10.16Summary 309 10.17Exercises 310 11 Sorting and Searching 315 11.1 Sorting 315 11.2 Flexible Sorting 318 11.3 Search 320 11.3.1 Linear Search 320 11.3.2 Binary Search 323 11.4 Recursion Revisited 332 11.5 List Permutations 337 11.6 Randomly Permuting a List 347 11.7 Reversing a List 352 11.8 Summary 352 11.9 Exercises 352 12 Tuples, Dictionaries, and Sets 355 12.1 Tuples 355 12.2 Arbitrary Argument Lists 359 12.3 Dictionaries 361 12.4 Using Dictionaries 365 12.5 Counting with Dictionaries 367 12.6 Grouping with Dictionaries 370 12.7 Keyword Arguments 371 12.8 Sets 374 12.9 Set Quantification with all and any 375 12.10Summary 379 12.11Exercises 380 ©2015 Richard L Halterman Draft date: June 17, 2015 CONTENTS 13 Handling Exceptions vi 381 13.1 Motivation 381 13.2 Common Standard Exceptions 383 13.3 Handling Exceptions 384 13.4 Handling Multiple Exceptions 387 13.5 The Catch-all Handler 389 13.6 Catching Exception Objects 391 13.7 The try Statement’s Optional else Block 393 13.8 finally block 395 13.9 Exception Handling Scope 398 13.10Raising Exceptions 405 13.11Using Exceptions Wisely 411 13.12Summary 411 13.13Exercises 412 14 Custom Types 413 14.1 Circle Objects 413 14.2 Restricting Access to Members 419 14.3 Bank Account Objects 421 14.4 Stopwatch Objects 424 14.5 Automated Testing 427 14.6 Class Inheritance 430 14.7 Custom Exceptions 431 14.8 Summary 431 14.9 Exercises 432 Index ©2015 Richard L Halterman 434 Draft date: June 17, 2015 vii Preface Legal Notices and Information This document is copyright ©2011-2015 by Richard L Halterman, all rights reserved Permission is hereby granted to make hardcopies and freely distribute the material herein under the following conditions: • The copyright and this legal notice must appear in any copies of this document made in whole or in part • None of material herein can be sold or otherwise distributed for commercial purposes without written permission of the copyright holder • Instructors at any educational institution may freely use this document in their classes as a primary or optional textbook under the conditions specified above A local electronic copy of this document may be made under the terms specified for hardcopies: • The copyright and these terms of use must appear in any electronic representation of this document made in whole or in part • None of material herein can be sold or otherwise distributed in an electronic form for commercial purposes without written permission of the copyright holder • Instructors at any educational institution may freely store this document in electronic form on a local server as a primary or optional textbook under the conditions specified above Additionally, a hardcopy or a local electronic copy must contain the uniform resource locator (URL) providing a link to the original content so the reader can check for updated and corrected content The current URL is http://python.cs.southern.edu/pythonbook/pythonbook.pdf ©2015 Richard L Halterman Draft date: June 17, 2015 Chapter The Context of Software Development A computer program, from one perspective, is a sequence of instructions that dictate the flow of electrical impulses within a computer system These impulses affect the computer’s memory and interact with the display screen, keyboard, mouse, and perhaps even other computers across a network in such a way as to produce the “magic” that permits humans to perform useful tasks, solve high-level problems, and play games One program allows a computer to assume the role of a financial calculator, while another transforms the machine into a worthy chess opponent Note the two extremes here: • at the lower, more concrete level electrical impulses alter the internal state of the computer, while • at the higher, more abstract level computer users accomplish real-world work or derive actual pleasure So well is the higher-level illusion achieved that most computer users are oblivious to the lower-level activity (the machinery under the hood, so to speak) Surprisingly, perhaps, most programmers today write software at this higher, more abstract level also An accomplished computer programmer can develop sophisticated software with little or no interest or knowledge of the actual computer system upon which it runs Powerful software construction tools hide the lower-level details from programmers, allowing them to solve problems in higher-level terms The concepts of computer programming are logical and mathematical in nature In theory, computer programs can be developed without the use of a computer Programmers can discuss the viability of a program and reason about its correctness and efficiency by examining abstract symbols that correspond to the features of real-world programming languages but appear in no real-world programming language While such exercises can be very valuable, in practice computer programmers are not isolated from their machines Software is written to be used on real computer systems Computing professionals known as software engineers develop software to drive particular systems These systems are defined by their underlying hardware and operating system Developers use concrete tools like compilers, debuggers, and profilers This chapter examines the context of software development, including computer systems and tools ©2015 Richard L Halterman Draft date: June 17, 2015

Ngày đăng: 13/04/2019, 01:35