Cover image for Head First Programming

Book description

Looking for a reliable way to learn how to program on your own, without being overwhelmed by confusing concepts? Head First Programming introduces the core concepts of writing computer programs -- variables, decisions, loops, functions, and objects -- which apply regardless of the programming language. This book offers concrete examples and exercises in the dynamic and versatile Python language to demonstrate and reinforce these concepts. Learn the basic tools to start writing the programs that interest you, and get a better understanding of what software can (and cannot) do. When you're finished, you'll have the necessary foundation to learn any programming language or tackle any software project you choose. With a focus on programming concepts, this book teaches you how to:

  • Understand the core features of all programming languages, including: variables, statements, decisions, loops, expressions, and operators

  • Reuse code with functions

  • Use library code to save time and effort

  • Select the best data structure to manage complex data

  • Write programs that talk to the Web

  • Share your data with other programs

  • Write programs that test themselves and help you avoid embarrassing coding errors

We think your time is too valuable to waste struggling with new concepts. Using the latest research in cognitive science and learning theory to craft a multi-sensory learning experience, Head First Programming uses a visually rich format designed for the way your brain works, not a text-heavy approach that puts you to sleep.

Table of Contents

  1. Dedication
  2. Special Upgrade Offer
  3. A Note Regarding Supplemental Files
  4. Advance Praise for
  5. Praise for other
  6. Authors of Head First Programming
  7. How to Use This Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. We know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did
    6. Here’s what YOU can do to bend your brain into submission
    7. Read Me
    8. The technical review team
    9. Acknowledgments
    10. Safari® Books Online
  8. 1. Starting to Code: Finding your way
    1. Programming lets you do more
      1. But what are
    2. So how do you run your code?
    3. Create a new program file
    4. Prepare and run your code
    5. A program is more than a list of commands
    6. Codeville: Your program is like a network of roads
    7. Branches are code intersections
    8. if/else branches
    9. The Python code needs interconnecting paths
    10. Python uses indents to connect paths
    11. Loops let you run the same piece of code over and over again
    12. Python’s while loop
    13. Your Programming Toolbox
  9. 2. Textual Data: Every string has its place
    1. Your new gig at Starbuzz Coffee
    2. Here’s the current Starbuzz code
    3. The cost is embedded in the HTML
    4. A string is a series of characters
    5. Find characters inside the text
    6. But how do you get at more than one character?
    7. Beans‘R’Us is rewarding loyal customers
      1. The price moved
    8. Searching is complex
    9. Python data is
    10. Strings and numbers are different
    11. The program has overloaded the Beans’R’Us Server
    12. Time... if only you had more of it
    13. You’re already using library code
    14. Order is restored
    15. Your Programming Toolbox
  10. 3. Functions: Let’s get organized
    1. Starbuzz is out of beans!
    2. What does the new program need to do?
    3. Don’t
    4. ...
    5. Reuse code with
    6. Always get things in the right order
    7. Return data with the
    8. Use the
    9. The function always sends the same message
    10. Use parameters to avoid duplicating functions
    11. Someone decided to mess with your code
    12. The rest of the program can’t see the password variable
    13. When you call a function, the computer creates a fresh list of variables
    14. When you leave a function, its variables get thrown away
    15. Starbuzz is fully stocked!
    16. Your Programming Toolbox
  11. 4. Data in Files and Arrays: Sort it out
    1. Surf’s up in Codeville
    2. Find the highest score in the results file
    3. Iterate through the file with the
    4. The file contains more than numbers...
    5. each line as you read it
    6. The split() method cuts the string
    7. But you need more than one top score
    8. Keeping track of
    9. An ordered list makes code much simpler
    10. Sorting is easier in memory
      1. Keep the data in memory
    11. You can’t use a separate variable for each line of data
    12. An
    13. Python gives you arrays with
      1. Arrays can be extended
    14. Sort the array before displaying the results
    15. Sort the scores from highest to lowest
    16. And the winner is...?
    17. You somehow forgot the surfer names
    18. Your Programming Toolbox
  12. 5. Hashes and Databases: Putting data in its place
    1. Who won the surfing contest?
    2. Associate the name with the score
      1. Use a hash
    3. Associate a key with a value using a hash
    4. Iterate hash data with
    5. The data isn’t sorted
      1. Python hashes don’t have a sort() method...
      2. ...but there is a function called sorted()
    6. When data gets complex
      1. So what does RSA’s data look like?
    7. Return a data structure from a function
    8. Here’s your new board!
    9. Meanwhile, down at the studio...
    10. The code remains the same; it’s the function that changes
    11. TVN’s data is on the money!
    12. Your Programming Toolbox
  13. 6. Modular Programming: Keeping things straight
    1. Head First Health Club is upgrading some systems
    2. The program needs to create a
    3. Use strings to format strings
    4. A late night email ruins your day
    5. $50,000... for a donut?!
    6. Only the sales from
    7. The new bank uses a
    8. Your coffee bar program still uses the
    9. Don’t just update
      1. Smart programmers write
    10. So how do you create a module...?
      1. ...and how do you use it?
    11. The transaction file is working great, too
    12. The health club has a new requirement
    13. The Starbuzz code
    14. The two discount functions have the
    15. Fully Qualified Names (FQNs) prevent your programs from getting confused
    16. The discounts get the customers flooding in
    17. Your Programming Toolbox
  14. 7. Building a Graphical User Interface: Going all gooey
    1. Head First TVN now produces game shows
    2. pygame is cross platform
    3. 0... 2... 1... 9... blast off!
    4. tkinter gives you the event loop for free
    5. tkinter is packed with options
    6. The GUI works, but doesn’t do anything
    7. Connect code to your button events
    8. The GUI program’s now ready for a screentest
    9. But TVN is still not happy
    10. Label it
    11. Your Programming Toolbox
  15. 8. Guis and Data: Data entry widgets
    1. Head-Ex needs a new delivery system
    2. They’ve already designed the interface
    3. from the GUI
    4. The
      1. Entry widget: for entering single lines of text
      2. Text widget: for longer, multi-line text
    5. Read and write data to text fields
    6. Large Text fields are harder to handle
    7. One of the Head-Ex deliveries went astray
    8. Users can enter
    9. Radio buttons force users to choose a valid depot
    10. Creating radio buttons in tkinter
    11. The radio buttons should work together
    12. The radio buttons can share a
    13. The system tells the other widgets when the model changes
    14. So how do you use
    15. Head-Ex’s business is expanding
    16. There are too many depots on the GUI
    17. An
    18. The
      1. But wait... you don’t have to list
    19. Things are going great at Head-Ex
    20. Your Programming Toolbox
  16. 9. Exceptions and Message Boxes: Get the message?
    1. What’s that smell?
    2. Someone changed the file permissions
    3. When it couldn’t write to the file, the program threw an
    4. the exception
    5. Watch for exceptions with try/except
    6. There’s an issue with the exception handler
    7. A message box
    8. Creating message boxes in Python
      1. Message boxes that
      2. Message boxes that
    9. Your Programming Toolbox
  17. 10. Graphical Interface Elements: Selecting the right tool
    1. Time to mix it up
    2. The music just kept on playing...
    3. Not all events are generated by button clicks
    4. Controlling the Window Manager
    5. Capturing the protocol event isn’t enough
    6. Two buttons, or not two buttons? That is the question...
    7. The checkbox is an on/off, flip/flop toggle
    8. Working with checkboxes in tkinter
      1. Using the get() method
    9. Pump up the volume!
    10. Model a slider on a scale
      1. Start with the volume
    11. Use pygame to set the volume
    12. Use tkinter for everything else
    13. The DJ is over the moon!
    14. Your Programming Toolbox
  18. 11. Custom Widgets and Classes: With an object in mind
    1. The DJ wants to play more than one track
      1. How will you create the widgets for each track?
    2. Create code for each track as a function
    3. The new function contains
    4. Your new function needs to create widgets
    5. The DJ is confused
    6. Group widgets together
      1. Create a new type of widget
    7. A frame widget contains other widgets
    8. A class is a machine for creating objects
    9. A class has methods that define behavior
    10. But how does an object call a method?
      1. identifies the widget calling the method
    11. The SoundPanel class looks a lot like the create_gui() function
    12. class = methods + data
    13. The DJ has an entire directory of tracks
    14. It’s party time!
      1. The mixer program brought the house down!
      2. Congratulations!
    15. Your Programming Toolbox
    16. Leaving town...
    17. It’s been great having you here in Codeville!
  19. A. Leftovers: The Top Ten Things (we didn’t cover)
    1. #1: Doing things “The Python Way”
    2. #2: Using Python 2
    3. #3: Other programming languages
    4. #4: Automated testing techniques
    5. #5: Debugging
    6. #6: Command-line execution
    7. #7: Ooops... we could’ve covered more OOP
    8. #8: Algorithms
    9. #9:
    10. #10: Other IDEs, shells, and text editors
  20. Index
  21. About the Authors
  22. Special Upgrade Offer
  23. Copyright