O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Practical Programming, 3rd Edition

Book Description

Classroom-tested by tens of thousands of students, this new edition of the bestselling intro to programming book is for anyone who wants to understand computer science. Learn about design, algorithms, testing, and debugging. Discover the fundamentals of programming with Python 3.6--a language that's used in millions of devices. Write programs to solve real-world problems, and come away with everything you need to produce quality code. This edition has been updated to use the new language features in Python 3.6.

No programming experience required! Incremental examples show you the steps and missteps that happen while developing programs, so you know what to expect when you tackle a problem on your own. Inspired by "How to Design Programs" (HtDP), discover a five-step recipe for designing functions, which helps you learn the concepts--and becomes an integral part of writing programs.

In this detailed introduction to Python and to computer programming, find out exactly what happens when your programs are executed. Work with numbers, text, big data sets, and files using real-world examples. Create and use your own data types. Make your programs reliable, work with databases, download data from the web automatically, and build user interfaces. As you use the fundamental programming tools in this book, you'll see how to document and organize your code so that you and other programmers can more easily read and understand it. This new edition takes advantage of Python 3.6's new features, including type annotations on parameters, return types and variable declarations, and changes to string formatting.

Most importantly, you'll learn how to think like a professional programmer.

What You Need:

You'll need to download Python 3.6, available from https://python.org.With that download comes IDLE, the editor we use for writing andrunning Python programs. (If you use Linux, you may need to installPython 3.6 and IDLE separately.)

Table of Contents

  1.  Acknowledgments
  2.  Preface
    1. Online Resources
  3. 1. What’s Programming?What’s Programming?
    1. Programs and Programming
    2. What’s a Programming Language?
    3. What’s a Bug?
    4. The Difference Between Brackets, Braces, and Parentheses
    5. Installing Python
  4. 2. Hello, Python
    1. How Does a Computer Run a Python Program?
    2. Expressions and Values: Arithmetic in Python
    3. What Is a Type?
    4. Variables and Computer Memory: Remembering Values
    5. How Python Tells You Something Went Wrong
    6. A Single Statement That Spans Multiple Lines
    7. Describing Code
    8. Making Code Readable
    9. The Object of This Chapter
    10. Exercises
  5. 3. Designing and Using Functions
    1. Functions That Python Provides
    2. Memory Addresses: How Python Keeps Track of Values
    3. Defining Our Own Functions
    4. Using Local Variables for Temporary Storage
    5. Tracing Function Calls in the Memory Model
    6. Designing New Functions: A Recipe
    7. Writing and Running a Program
    8. Omitting a return Statement: None
    9. Dealing with Situations That Your Code Doesn’t Handle
    10. What Did You Call That?
    11. Exercises
  6. 4. Working with Text
    1. Creating Strings of Characters
    2. Using Special Characters in Strings
    3. Creating a Multiline String
    4. Printing Information
    5. Getting Information from the Keyboard
    6. Quotes About Strings
    7. Exercises
  7. 5. Making Choices
    1. A Boolean Type
    2. Choosing Which Statements to Execute
    3. Nested if Statements
    4. Remembering Results of a Boolean Expression Evaluation
    5. You Learned About Booleans: True or False?
    6. Exercises
  8. 6. A Modular Approach to Program Organization
    1. Importing Modules
    2. Defining Your Own Modules
    3. Testing Your Code Semiautomatically
    4. Tips for Grouping Your Functions
    5. Organizing Our Thoughts
    6. Exercises
  9. 7. Using Methods
    1. Modules, Classes, and Methods
    2. Calling Methods the Object-Oriented Way
    3. Exploring String Methods
    4. What Are Those Underscores?
    5. A Methodical Review
    6. Exercises
  10. 8. Storing Collections of Data Using Lists
    1. Storing and Accessing Data in Lists
    2. Type Annotations for Lists
    3. Modifying Lists
    4. Operations on Lists
    5. Slicing Lists
    6. Aliasing: What’s in a Name?
    7. List Methods
    8. Working with a List of Lists
    9. A Summary List
    10. Exercises
  11. 9. Repeating Code Using Loops
    1. Processing Items in a List
    2. Processing Characters in Strings
    3. Looping Over a Range of Numbers
    4. Processing Lists Using Indices
    5. Nesting Loops in Loops
    6. Looping Until a Condition Is Reached
    7. Repetition Based on User Input
    8. Controlling Loops Using break and continue
    9. Repeating What You’ve Learned
    10. Exercises
  12. 10. Reading and Writing Files
    1. What Kinds of Files Are There?
    2. Opening a File
    3. Techniques for Reading Files
    4. Files over the Internet
    5. Writing Files
    6. Writing Example Calls Using StringIO
    7. Writing Algorithms That Use the File-Reading Techniques
    8. Multiline Records
    9. Looking Ahead
    10. Notes to File Away
    11. Exercises
  13. 11. Storing Data Using Other Collection Types
    1. Storing Data Using Sets
    2. Storing Data Using Tuples
    3. Storing Data Using Dictionaries
    4. Inverting a Dictionary
    5. Using the in Operator on Tuples, Sets, and Dictionaries
    6. Comparing Collections
    7. Creating New Type Annotations
    8. A Collection of New Information
    9. Exercises
  14. 12. Designing Algorithms
    1. Searching for the Two Smallest Values
    2. Timing the Functions
    3. At a Minimum, You Saw This
    4. Exercises
  15. 13. Searching and Sorting
    1. Searching a List
    2. Binary Search
    3. Sorting
    4. More Efficient Sorting Algorithms
    5. Merge Sort: A Faster Sorting Algorithm
    6. Sorting Out What You Learned
    7. Exercises
  16. 14. Object-Oriented Programming
    1. Understanding a Problem Domain
    2. Function isinstance, Class object, and Class Book
    3. Writing a Method in Class Book
    4. Plugging into Python Syntax: More Special Methods
    5. A Little Bit of OO Theory
    6. A Case Study: Molecules, Atoms, and PDB Files
    7. Classifying What You’ve Learned
    8. Exercises
  17. 15. Testing and Debugging
    1. Why Do You Need to Test?
    2. Case Study: Testing above_freezing
    3. Case Study: Testing running_sum
    4. Choosing Test Cases
    5. Hunting Bugs
    6. Bugs We’ve Put in Your Ear
    7. Exercises
  18. 16. Creating Graphical User Interfaces
    1. Using Module tkinter
    2. Building a Basic GUI
    3. Models, Views, and Controllers, Oh My!
    4. Customizing the Visual Style
    5. Introducing a Few More Widgets
    6. Object-Oriented GUIs
    7. Keeping the Concepts from Being a GUI Mess
    8. Exercises
  19. 17. Databases
    1. Overview
    2. Creating and Populating
    3. Retrieving Data
    4. Updating and Deleting
    5. Using NULL for Missing Data
    6. Using Joins to Combine Tables
    7. Keys and Constraints
    8. Advanced Features
    9. Some Data Based On What You Learned
    10. Exercises
  20.  Bibliography