Python Without Fear

Book description

Praise for this book, Python Without Fear

“This is really a great book. I wish I’d had it when I was learning Python.”

–John M. Wargo, author of Apache Cordova 4 Programming

Praise for the previous book in the series, C++ Without Fear

“I’m in love with your C++ Without Fear book. It keeps me awake for hours during the night. Thanks to you, I got most of the idea in just a few hours.”

–Laura Viral, graduate physics student at CERN and Istanbul, Turkey

“It’s hard to tell where I began and ended with your book. I felt like I woke up and literally knew how to write C++ code. I can’t overstate the confidence you gave me.”

– Danny Grady, senior programmer/analyst at a Fortune 500 Company

Whether you’re new to programming or moving from another language, Python Without Fear will quickly make you productive! Brian Overland’s unique approach to Python includes:

  • Taking you by the hand while teaching topics from the very basics to intermediate and advanced features of Python
  • Teaching by examples that are explained line by line
  • Heavy emphasis on examples that are fun and useful, including games, graphics, database applications, file storage, puzzles, and more!
  • How to think “Pythonically” and avoid common “gotchas”

Register your product at informit.com/register for convenient access to downloads, updates, and/or corrections as they become available.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication Page
  5. Contents
  6. Preface
    1. Steering Around the “Gotchas”
    2. How to Think “Pythonically”
    3. Intermediate and Advanced Features
    4. Learning in Many Different Styles
    5. What’s Going on “Under the Hood”
    6. Why Python?
  7. Acknowledgments
  8. Author Bio
  9. Chapter 1 Meet the Python
    1. A Brief History of Python
    2. How Python Is Different
    3. How This Book Works
    4. Installing Python
    5. Begin Using Python with IDLE
    6. Correcting Mistakes from Within IDLE
    7. Dealing with Ends of Lines
    8. Additional Help: Online Sources
  10. Chapter 2 A Python Safari: Numbers
    1. Python and Numbers
      1. Interlude Why Doesn’t C++ Support Infinite Integers?
      2. Interlude How Big Is a Google?
    2. Python and Floating-Point Numbers
    3. Assigning Numbers to Variables
      1. Interlude What Do Python Assignments Really Do?
    4. Variable-Naming Conventions in This Book
    5. Some Python Shortcuts
    6. Chapter 2 Summary
  11. Chapter 3 Your First Programs
    1. Temperatures Rising?
      1. Interlude Python’s Use of Indentation
    2. Putting in a Print Message
    3. Syntax Summaries
      1. Example 3.1. Quadratic Equation as a Function
      2. How It Works
    4. Getting String Input
    5. Getting Numeric Input
      1. Example 3.2. Quadratic Formula with I/O
      2. How It Works
    6. Formatted Output String
      1. Example 3.3. Distance Formula in a Script
      2. How It Works
    7. Chapter 3 Summary
  12. Chapter 4 Decisions and Looping
    1. Decisions Inside a Computer Program
    2. Conditional and Boolean Operators
    3. The if, elif, and else Keywords
      1. Interlude Programs and Robots in Westworld
      2. Example 4.1. Enter Your Age
      3. How It Works
    4. while: Looping the Loop
      1. Example 4.2. Factorials
      2. How It Works
      3. Optimizing the Code
      4. Example 4.3. Printing Fibonacci Numbers
      5. How It Works
    5. “Give Me a break” Statement
      1. Example 4.4. A Number-Guessing Game
      2. How It Works
      3. Interlude Binary Searches and “O” Complexity
    6. Chapter 4 Summary
  13. Chapter 5 Python Lists
    1. The Python Way: The World Is Made of Collections
    2. Processing Lists with for
    3. Modifying Elements with for (You Can’t!)
      1. Example 5.1. A Sorting Application
      2. How It Works
      3. Optimizing the Code
    4. Indexing and Slicing
    5. Copying Data to Slices
    6. Ranges
      1. Example 5.2. Revised Factorial Program
      2. How It Works
      3. Optimizing the Code
      4. Example 5.3. Sieve of Eratosthenes
      5. How It Works
      6. Optimizing the Code
    7. List Functions and the in Keyword
      1. Interlude Who Was Eratosthenes?
    8. Chapter 5 Summary
  14. Chapter 6 List Comprehension and Enumeration
    1. Indexes and the enumerate Function
    2. The Format String Method Revisited
      1. Example 6.1. Printing a Table
      2. How It Works
    3. Simple List Comprehension
      1. Example 6.2. Difference Between Squares
      2. How It Works
      3. Interlude Proving the Equation
    4. “Two-Dimensional” List Comprehension
    5. List Comprehension with Conditional
      1. Example 6.3. Sieve of Eratosthenes 2
      2. How It Works
      3. Optimizing the Code: Sets
      4. Example 6.4. Pythagorean Triples
      5. How It Works
      6. Interlude The Importance of Pythagoras
    6. Chapter 6 Summary
  15. Chapter 7 Python Strings
    1. Creating a String with Quote Marks
    2. Indexing and “Slicing”
    3. String/Number Conversions
      1. Example 7.1. Count Trailing Zeros
      2. How It Works
      3. Interlude Python Characters vs. Python Strings
    4. Stripping for Fun and Profit
      1. Example 7.2. Count Zeros, Version 2
      2. How It Works
    5. Let’s Split: The split Method
    6. Building Strings with Concatenation (+)
      1. Example 7.3. Sort Words on a Line
      2. How It Works
    7. The join Method
    8. Chapter 7 Summary
  16. Chapter 8 Single-Character Ops
    1. Naming Conventions in This Chapter
    2. Accessing Individual Characters (A Review)
    3. Getting Help with String Methods
    4. Testing Uppercase vs. Lowercase
    5. Converting Case of Letters
    6. Testing for Palindromes
      1. Example 8.1. Convert Strings to All Caps
      2. How It Works
      3. Optimizing the Code
      4. Example 8.2. Completing the Palindrome Test
      5. How It Works
      6. Optimizing the Code
      7. Interlude Famous Palindromes
    7. Converting to ASCII Code
    8. Converting ASCII to Character
      1. Example 8.3. Encode Strings
      2. How It Works
      3. Interlude The Art of Cryptography
      4. Example 8.4. Decode Strings
      5. How It Works
    9. Chapter 8 Summary
  17. Chapter 9 Advanced Function Techniques
    1. Multiple Arguments
    2. Returning More Than One Value
      1. Interlude Passing and Modifying Lists
      2. Example 9.1. Difference and Sum of Two Points
      3. How It Works
    3. Arguments by Name
    4. Default Arguments
      1. Example 9.2. Adding Machine
      2. How It Works
      3. Optimizing the Code
    5. Importing Functions from Modules
      1. Example 9.3. Dice Game (Craps)
      2. How It Works
      3. Interlude Casino Odds Making
    6. Chapter 9 Summary
  18. Chapter 10 Local and Global Variables
    1. Local Variables, What Are They Good For?
    2. Locals vs. Globals
    3. Introducing the global Keyword
    4. The Python “Local Variable Trap”
      1. Interlude Does C++ Have Easier Scope Rules?
      2. Example 10.1. Beatles Personality Profile (BPP)
      3. How It Works
      4. Example 10.2. Roman Numerals
      5. How It Works
      6. Optimizing the Code
      7. Interlude What’s Up with Roman Numerals?
      8. Example 10.3. Decode Roman Numerals
      9. How It Works
      10. Optimizing the Code
    5. Chapter 10 Summary
  19. Chapter 11 File Ops
    1. Text Files vs. Binary Files
    2. The Op System (os) Module
      1. Interlude Running on Other Systems
    3. Open a File
    4. Let’s Write a Text File
      1. Example 11.1. Write File with Prompt
      2. How It Works
    5. Read a Text File
    6. Files and Exception Handling
      1. Interlude Advantages of try/except
      2. Example 11.2. Text Read with Line Numbers
      3. How It Works
    7. Other File Modes
    8. Chapter 11 Summary
  20. Chapter 12 Dictionaries and Sets
    1. Why Do We Need Dictionaries, Ms. Librarian?
    2. Adding and Changing Key-Value Pairs
    3. Accessing Values
    4. Searching for Keys
      1. Interlude What Explains Dictionary “Magic”?
      2. Example 12.1. Personal Phone Book
      3. How It Works
    5. Converting Dictionaries to Lists
      1. Example 12.2. Reading Items by Prefix
      2. How It Works
      3. Example 12.3. Loading and Saving to a File
      4. How It Works
    6. All About Sets
    7. Operations on Sets
      1. Interlude What’s So Important About Sets?
      2. Example 12.4. Revised Sieve of Eratosthenes
      3. How It Works
    8. Chapter 12 Summary
  21. Chapter 13 Matrixes: 2-D Lists
    1. Simple Matrixes
    2. Accessing Elements
    3. Irregular Matrixes and Length of a Row
    4. Multiplication (*) and Lists
    5. The Python Matrix Problem
    6. How to Create N*M Matrixes: The Solution
      1. Interlude Why Isn’t It Easier?
      2. Example 13.1. Multiplication Table
      3. How It Works
      4. Example 13.2. User-Initialized Matrix
      5. How It Works
      6. Optimizing the Code
    7. How to Rotate a Matrix
      1. Interlude Pros and Cons of Garbage Collection
      2. Example 13.3. Complete Rotation Example
      3. How It Works
      4. Optimizing the Code
    8. Chapter 13 Summary
  22. Chapter 14 Winning at Tic-Tac-Toe
    1. Design of a Tic-Tac-Toe Board
    2. Plan of This Chapter
      1. Phase 1
      2. Phase 2
      3. Phase 3
    3. Python One-Line if/else
      1. Example 14.1. Simple Two-Player Game
      2. How It Works
      3. Interlude Variations on Tic-Tac-Toe
    4. The count Method for Lists
      1. Example 14.2. Two-Player Game with Win Detection
      2. How It Works
    5. Introducing the Computer Player
      1. Example 14.3. Computer Play: The Computer Goes First
      2. How It Works
      3. Playing Second
      4. Interlude The Art of Heuristics
    6. Chapter 14 Summary
  23. Chapter 15 Classes and Objects I
    1. What’s an Object?
    2. Classes in Python
      1. How Do I Define a Simple Class?
      2. How Do I Use a Class to Create Objects?
      3. How Do I Attach Data to Objects?
      4. How Do I Write Methods?
    3. The All-Important __init__ Method
      1. Interlude Why This self Obsession?
    4. Design for a Database Class
      1. Interlude C++ Classes Compared to Python
      2. Example 15.1. Tracking Employees
      3. How It Works
    5. Defining Other Methods
    6. Design for a Point3D Class
    7. Point3D Class and Default Arguments
    8. Three-Dimensional Tic-Tac-Toe
      1. Example 15.2. Looking for a 3-D Win
      2. How It Works
      3. Example 15.3. Calculating Ways of Winning
      4. How It Works
      5. Optimizing the Code
    9. Chapter 15 Summary
  24. Chapter 16 Classes and Objects II
    1. Getting Help from Doc Strings
    2. Function Typing and “Overloading”
      1. Interlude What Is Duck Typing?
    3. Variable-Length Argument Lists
      1. Example 16.1. PointN Class
      2. How It Works
      3. Optimizing the Code
    4. Inheritance
    5. The Fraction Class
      1. Example 16.2. Extending the Fraction Class
      2. How It Works
    6. Class Variables and Methods
    7. Instance Variables as “Default” Values
      1. Example 16.3. Polygon “Automated” Class
      2. How It Works
      3. Interlude OOPS, What Is It Good For?
    8. Chapter 16 Summary
  25. Chapter 17 Conway’s Game of Life
    1. Interlude The Impact of “Life”
    2. Game of Life: The Rules of the Game
    3. Generating the Neighbor Count
    4. Design of the Program
      1. Example 17.1. The Customized Matrix Class
      2. How It Works
    5. Moving the Matrix Class to a Module
      1. Example 17.2. Printing a Life Matrix
      2. How It Works
    6. The Famous Slider Pattern
      1. Example 17.3. The Whole Game of Life Program
      2. How It Works
      3. Interlude Does “Life” Create Life?
    7. Chapter 17 Summary
  26. Chapter 18 Advanced Pythonic Techniques
    1. Generators
    2. Exploiting the Power of Generators
      1. Example 18.1. A Custom Random-Number Generator
      2. How It Works
      3. Interlude How Random Is “Random”?
    3. Properties
      1. Getter Methods
      2. Setter Methods
      3. Putting Getters and Setters Together
      4. Example 18.2. Multilevel Temperature Object
      5. How It Works
    4. Decorators: Functions Enclosing Other Functions
    5. Python Decoration
      1. Example 18.3. Decorators as Debugging Tools
      2. How It Works
    6. Chapter 18 Summary
  27. Chapter 19 Tkinter and Graphical Programming
    1. Begin Using tkinter
    2. Display “Hello, World!”
    3. “Hello, World” with a Button
    4. Changing Attributes During Execution
      1. Example 19.1. The Swami Knows
      2. How It Works
    5. Getting Attributes
    6. Introducing the Canvas Widget
    7. Binding Canvas Events
    8. Event Handlers
    9. More About the “pack“ Geometry Manager
      1. Example 19.2. Color Changer
      2. How It Works
    10. The “grid“ Geometry Manager
    11. Tic-Tac-Toe with Canvas Widgets
    12. Subclassing the Canvas Class
      1. Example 19.3. GUI Tic-Tac-Toe
      2. How It Works
    13. Where to Go from Here
    14. Chapter 19 Summary
  28. Appendix A Python Operator Precedence Table
  29. Appendix B Summary of Most Important Formatting Rules for Python 3.0
    1. 1. Formatting Ordinary Text
    2. 2. Formatting Arguments
    3. 3. Specifying Order of Arguments
    4. 4. Right Justification Within Field of Size N
    5. 5. Left Justification Within Field of Size N
    6. 6. Truncation: Limit Size of Print Field
    7. 7. Combined Truncation and Justification
    8. 8. Length and Precision of Floating-Point Numbers
    9. 9. The Padding Character
  30. Appendix C Glossary
    1. Index

Product information

  • Title: Python Without Fear
  • Author(s): Brian Overland
  • Release date: October 2017
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780134688251