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

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 FirstProgrammingintroduces the core concepts of writing computerprograms -- variables, decisions, loops, functions, and objects --which apply regardless of the programming language. This bookoffers concrete examples and exercises in the dynamic and versatilePython language to demonstrate and reinforce these concepts.Learn the basic tools to start writing the programs that interestyou, and get a better understanding of what software can (andcannot) do. When you're finished, you'll have the necessaryfoundation to learn any programming language or tackle any softwareproject you choose.With a focus on programming concepts, this book teaches you howto:

  • 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 avoidembarrassing coding errors

  • We think your time is too valuable to waste struggling with newconcepts. Using the latest research in cognitive science andlearning theory to craft a multi-sensory learning experience,Head First Programminguses a visually rich formatdesigned for the way your brain works, not a text-heavy approachthat puts you to sleep.

    Table of Contents

    1. Dedication
    2. A Note Regarding Supplemental Files
    3. Advance Praise for Head First Programming
    4. Praise for other Head First books
    5. Authors of Head First Programming
    6. 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
    7. 1. Starting to Code: Finding your way
      1. Programming lets you do more
        1. But what are g and guess?
      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
    8. 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 smart
      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
    9. 3. Functions: Let’s get organized
      1. Starbuzz is out of beans!
      2. What does the new program need to do?
      3. Don’t duplicate your code...
      4. ...Reuse your code instead
      5. Reuse code with functions
      6. Always get things in the right order
      7. Return data with the return command
      8. Use the Force, Web Luke
      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
    10. 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 open, for, close pattern
      4. The file contains more than numbers...
      5. Split 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 3 scores makes the code more complex
      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 array lets you manage a whole train of data
      13. Python gives you arrays with lists
        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
    11. 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 for
      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
    12. 6. Modular Programming: Keeping things straight
      1. Head First Health Club is upgrading some systems
      2. The program needs to create a transaction file
      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 your program were rejected
      7. The new bank uses a new format
      8. Your coffee bar program still uses the old format
      9. Don’t just update your copy
        1. Smart programmers write modular code
      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 same name
      15. Fully Qualified Names (FQNs) prevent your programs from getting confused
      16. The discounts get the customers flooding in
      17. Your Programming Toolbox
    13. 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
    14. 8. Guis and Data: Data entry widgets
      1. Head-Ex needs a new delivery system
      2. They’ve already designed the interface
      3. Read data from the GUI
      4. The Entry and Text widgets let you enter text data into your GUI
        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 anything in the fields
      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 model
      13. The system tells the other widgets when the model changes
      14. So how do you use models in tkinter?
      15. Head-Ex’s business is expanding
      16. There are too many depots on the GUI
      17. An OptionMenu lets you have as many options as needed
      18. The model stays the same
        1. But wait... you don’t have to list all the values like that
      19. Things are going great at Head-Ex
      20. Your Programming Toolbox
    15. 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 exception
      4. Catch the exception
      5. Watch for exceptions with try/except
      6. There’s an issue with the exception handler
      7. A message box demands attention
      8. Creating message boxes in Python
        1. Message boxes that say stuff
        2. Message boxes that ask stuff
      9. Your Programming Toolbox
    16. 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
    17. 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 other functions
      4. Your new function needs to create widgets and event handlers
      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. self 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!
    18. 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: Advanced Scary programming topics
      10. #10: Other IDEs, shells, and text editors
    19. Index
    20. About the Authors
    21. Copyright