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 Learn to Code

Book Description

What will you learn from this book?

It’s no secret the world around you is becoming more connected, more configurable, more programmable, more computational. You can remain a passive participant, or you can learn to code. With Head First Learn to Code you’ll learn how to think computationally and how to write code to make your computer, mobile device, or anything with a CPU do things for you. Using the Python programming language, you’ll learn step by step the core concepts of programming as well as many fundamental topics from computer science, such as data structures, storage, abstraction, recursion, and modularity.

Why does this book look so different?

Based on the latest research in cognitive science and learning theory, Head First Learn to Code uses a visually rich format to engage your mind, rather than a text-heavy approach that puts you to sleep. Why waste your time struggling with new concepts? This multi-sensory learning experience is designed for the way your brain really works.

Table of Contents

  1. Praise for Head First Learn to Code
  2. Praise for other books by Eric Freeman
  3. Head First Learn to Code
  4.  
  5. the author
  6. 1. 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.
      1. And we know what your brain is thinking.
    3. Metacognition: thinking about thinking
      1. So how DO you get your brain to think coding is as important as a tiger?
    4. Here’s what WE did:
    5. Here’s what YOU can do to bend your brain into submission
    6. Read Me
      1. We want you to learn the thinking process behind programming.
      2. In this book we use Python.
      3. We don’t exhaustively cover every aspect of the Python language.
      4. You can use a Mac or PC, or Linux for that matter.
      5. The activities are NOT optional.
      6. The redundancy is intentional and important.
      7. The examples are as lean as possible.
      8. The Brain Power exercises don’t usually have answers.
      9. Get the code examples, help, and discussion online.
    7. You’re going to have to install Python
    8. A word on organizing your code
    9. Acknowledgments*
    10. The Review Team
  7. 2. 1 thinking computationally: Getting Started
    1. Breaking it down
    2. How coding works
    3. Are we even speaking the same language?
    4. The world of programming languages
    5. How you’ll write and run code with Python
      1. there are no Dumb Questions
    6. A very brief history of Python
    7. Putting Python through its paces
    8. Saving your work
    9. Congrats on coding your first Python program!
      1. there are no Dumb Questions
    10. Phrase-O-Matic
    11. Getting the code in the machine
  8. 3. 2 simple values, variables, and types: Know Your Value
    1. Coding the Dog Age Calculator
    2. Going from pseudocode to code
    3. Step 1: Getting some input
    4. How the input function works
    5. Using variables to remember and store values
    6. Assigning the user’s input to a variable
    7. Step 2: Getting more input
    8. It’s time to run some code
    9. Getting some code entered
    10. A deep dive on variables
    11. Adding some expression
    12. Variables are called VARY-ables for a reason
    13. Better living through operator precedence
    14. Computing with operator precedence
    15. Crack the Code Challenge
    16. Back away from that keyboard!
      1. there are no Dumb Questions
    17. Step 3: Computing the dog’s age
    18. Houston, we’ve got a problem!
    19. To err is human to code
    20. A little more debugging...
      1. Careful what you ask for...
    21. What are Python types, anyway?
      1. there are no Dumb Questions
    22. Fixing our code
      1. there are no Dumb Questions
    23. Houston, we have liftoff
    24. Step 4: User-friendly output
      1. Who am I? Solution
    25. Crack the Code Challenge SOLUTION
  9. 4. 3 booleans, decisions, and loops: Decisive Code
    1. Would you like to play a game?
      1. How Rock, Paper, Scissors works
      2. How you’re going to play against the computer
    2. First, a high-level design
    3. The computer’s choice
      1. How to generate a random number
    4. How to use the random number
      1. there are no Dumb Questions
    5. Taking things further...
    6. True? Or False?
    7. Introducing the Boolean type
    8. Making decisions
    9. Decisions and more decisions
    10. Back to Rock, Paper, Scissors
    11. Getting the user’s choice
      1. Good eye.
    12. Taking a look at the user’s choice
    13. Adding the code to detect a tie
    14. Who won?
      1. Right.
    15. How to implement the game logic
    16. More about Boolean operators
    17. Display the winner
    18. Got documentation?
    19. How to add comments to your code
    20. We need to finish that game!
    21. How do we know if the user’s choice is invalid?
    22. Checking out and cleaning up the expression
    23. How to continually prompt the user
    24. Doing things more than once
    25. How the while loop works
    26. How to use while to prompt the user until you get a valid choice
    27. Congratulations on coding your first game!
      1. Beware of the The Dreaded Infinite Loop
  10. 5. 4 lists and iteration: Providing Some Structure
    1. Can you help Bubbles-R-Us?
    2. How to represent multiple values in Python
    3. How lists work
      1. How to create a list
      2. How to access a list item
      3. Updating a value in the list
    4. How big is that list, anyway?
    5. Accessing the last item in the list
    6. Python makes this even easier
    7. Using Python’s negative indices
      1. there are no Dumb Questions
    8. Meanwhile, back at Bubbles-R-Us...
    9. Cubicle conversation
    10. How to iterate over a list
    11. Fixing the output glitch
    12. Really fixing the output glitch
    13. The for loop, the preferred way to iterate over a list
      1. How the for loop works
    14. How the for loop works on a range of numbers
    15. Doing more with ranges
      1. Try a starting and ending number
      2. Add a step size
      3. Count backward
      4. Or start from negative numbers
      5. there are Dumb Questions
    16. Putting it all together
    17. Test drive the bubble report
      1. Fireside Chats
    18. Cubicle conversation continued...
    19. Building your own list, from scratch
    20. Doing even more with lists
      1. Delete an item from a list
      2. Add one list to another
      3. Or insert items into your list
      4. there are no Dumb Questions
    21. Test drive the final report
    22. And the winners are...
    23. Testing the most cost-effective solution
      1. You’re right: we didn’t need to.
  11. 6. 5 functions and abstraction: Getting Functional
    1. What’s wrong with the code, anyway?
    2. Turning a block of code into a FUNCTION
    3. We created a function, so how do we use it?
    4. But how does all this actually work?
      1. there are no Dumb Questions
    5. Functions can RETURN things too
    6. How to call a function that has a return value
      1. Good catch.
    7. Getting a little refactoring under our belts
    8. Running the code
    9. How to abstract the avatar code
    10. Writing the get attribute function body
    11. Calling get attribute
    12. Let’s talk about variables a little more...
    13. Understanding variable scope
      1. there are no Dumb Questions
    14. When variables get passed to functions
    15. Making the drink_me function call
    16. What about using global variables in functions?
      1. Fireside Chats
    17. Going further with parameters: default values and keywords
    18. How default parameter values work
    19. Always list your required parameters first!
    20. Using arguments with keywords
    21. How to think about all these options
      1. The case of the attempted robbery not worth investigating
  12. 7. 4, part 2, sorting and nested iteration: Putting Some Order in Your Data
    1. Cubicle conversation
    2. Understanding bubble sort
    3. Starting with pass 1
    4. Pass 2
    5. Pass 3
    6. Some bubble sort pseudocode
    7. Cubicle conversation continued...
    8. Implementing bubble sort in Python
    9. Computing bubble solution numbers
  13. 8. 6 text, strings, and heuristics: Putting It All Together
    1. Welcome to the data sciences
    2. How do you compute something like readability?
    3. The game plan
    4. Writing some pseudocode
    5. We need some text to analyze
      1. How to get multiline text into Python
    6. Setting up the function
    7. First up: we need the total number of words in our text
    8. Cubicle conversation
      1. there are no Dumb Questions
    9. Computing the total number of sentences
    10. Writing the count_sentences function
    11. Computing the number of syllables, or, learning to love heuristics
    12. Cubicle conversation continued...
    13. Setting up the heuristic
    14. Writing the heuristic
    15. How to count vowels
    16. Ignoring consecutive vowels
    17. Writing the code to ignore consecutive vowels
    18. Removing final e’s, y’s, and punctuation
      1. He who controls the slice, controls the...
    19. Putting slicing (substrings) to work
    20. Finishing off the heuristic code
    21. Implementing the reading ease formula
    22. Taking it even further
  14. 9. 7 modules, methods, classes, and objects: Getting Modular
    1. Cubicle conversation
    2. A quick module review
      1. there are no Dumb Questions
    3. Cubicle conversation continued...
    4. The name global variable
    5. Cubicle conversation continued...
    6. Updating analyze.py
    7. Using analyze.py as a module
    8. Adding docstrings to analyze.py
    9. Exploring other Python modules
    10. Wait, did someone say “turtles”?!
    11. Creating your very own turtle
    12. Turtle lab
      1. there are no Dumb Questions
    13. Adding a second turtle
    14. What are turtles, anyway?
    15. What are objects?
    16. Okay, what’s a class then?
      1. A class is not an object, it’s used to construct them.
      2. there are no Dumb Questions
    17. A class tells us what an object knows and what an object can do
    18. How to use objects and classes
    19. What about those methods and attributes?
    20. Seeing classes and objects everywhere
    21. Get ready for some turtle races
    22. Planning the game
    23. Let’s start coding
    24. Setting up the game
    25. Writing the setup code
    26. Not so fast!
    27. Starting the race
    28. Don’t worry, this isn’t the end of objects.
      1. Odd goings-on at the turtle races.
    29. Mod coding cross
    30. Mod cross Solution
  15. 10. 8 recursion and dictionaries: Beyond Iteration and Indices
    1. A different way to compute
    2. And now the different way...
    3. Now let’s write some code for our two cases
    4. Let’s get some more practice
    5. Using recursion to detect palindromes
    6. Writing a recursive palindrome detector
      1. there are no Dumb Questions
      2. Fireside Chats
    7. The Anti-Social Network
    8. Introducing the dictionary
    9. Let’s see how to create a dictionary
    10. And then add items
    11. And then retrieve values by their keys
    12. Keys and values do not have to be strings
    13. Of course you can remove keys as well
    14. But you might want to test to see if it exists first
      1. there are no Dumb Questions
    15. What about iterating through a dictionary?
    16. Okay, but can we get literal for a sec?
    17. Leveraging dictionaries at the Anti-Social Network
    18. But how do we add more attributes?
    19. Remember the Anti-Social Network’s killer feature?
    20. Finding the most anti-social user
    21. Now, it’s all you!
    22. Can we just remember function call results?
    23. Using a dictionary to remember our Fibonacci results
    24. We might as well use the $5 words: memoization
    25. Some more brain building
    26. A closer look at the koch function
    27. Truly exploring the koch fractal
    28. They don’t call it the Koch snowflake for nothin’
  16. 11. 9 saving and retrieving files: Persistence
    1. Ready for some Crazy Libs?
      1. How to Play
    2. How Crazy Libs is going to work
    3. Step 1: Read the text of the story from a file
    4. To read a file in Python, you first have to open it
    5. How to use file paths
    6. Relative paths
    7. Absolute paths
    8. Oh, and don’t forget to clean up when you’re done!
    9. Reading a file into your Python code
    10. Using the file object to read a file
    11. Oh, give it a break already...
    12. Hey, we’ve got a Crazy Lib game to finish!
    13. Using the readline method of the file object
    14. How do we know when we’ve read the last line?
    15. Ah, but there’s a easier way using the power of Python sequences
    16. Reading in a Crazy Lib template
    17. Processing the template text
    18. Now let’s process the text
    19. Using a new string method to fix the bug
    20. Actually fixing the bug
    21. Some code has real problems
    22. Handling exceptions
    23. Explicitly handling exceptions
      1. there are Dumb Questions
    24. Updating Crazy Libs to handle exceptions
    25. Our last step: storing the Crazy Lib
    26. Updating the rest of the code
    27. File i/o cross Solution
  17. 12. 10 using web apis: You Really Should Get Out More
    1. Extending your reach with Web APIs
    2. How Web APIs work
    3. All Web APIs have a web address
      1. Consult the API documentation.
      2. Good point.
    4. Time for a quick upgrade
    5. Doing the upgrade
      1. there are no Dumb Questions
    6. All we need now is a good Web API...
    7. A closer look at the API
    8. Web APIs provide data using JSON
      1. there are no Dumb Questions
    9. Now let’s look at that request module again
    10. Putting it all together: making a request to Open Notify
    11. How to use JSON in Python
    12. Using the JSON module on our ISS data
    13. Let’s add some graphics
    14. Meet the screen object
    15. Let’s add a turtle to represent the ISS
    16. Turtles can look like space stations too
    17. Forget the ISS—where are we?
    18. Finishing off the ISS code
  18. 13. 11 widgets, events, and emergent behavior: Getting Interactive
    1. A closer look at the Game of Life
      1. Oh, you’d be surprised...
    2. What we’re going to build
    3. Do we have the right design?
    4. How we’re going to build the simulator
    5. Building the data model
    6. Representing the grid
    7. Computing a generation of the Game of Life
    8. Computing each cell’s fate
    9. Completing the model code
    10. Where are we?
    11. Building the view
    12. Creating your first widget
    13. Adding the rest of the widgets
    14. Correcting the layout
    15. Placing the widgets into a grid layout
    16. Translating the grid layout to code
      1. there are no Dumb Questions
    17. Moving on to the controller
    18. Adding an update function
      1. there are no Dumb Questions
    19. Ready for another new style of computation?
    20. Let’s add a click handler
    21. How the start/pause button is going to work
    22. Implementing the start/pause button
    23. Another kind of event
    24. We have the technology: the after method
    25. Are we there yet?
    26. How to enter and edit cells directly
    27. Writing the grid_view handler
    28. Now it’s time to add some patterns
    29. Writing a handler for the OptionMenu
    30. How to define your patterns
    31. Writing the pattern loader
  19. 14. 12 object-oriented programming: A Trip to Objectville
    1. Breaking it down, a different way
    2. What’s the point of object-oriented programming, anyway?
    3. Designing your first class
    4. Writing your first class
    5. How the constructor works
      1. there are no Dumb Questions
    6. Writing the bark method
    7. How methods work
    8. Adding some inheritance
    9. Implementing the ServiceDog class
    10. A closer look at subclassing
    11. A ServiceDog IS-A Dog
    12. Testing IS-A in code
    13. How would you describe yourself?
      1. there are no Dumb Questions
    14. Overriding and extending behavior
    15. Welcome to Jargon City
    16. Object can HAS-Another object
    17. Designing a Doggie Hotel
    18. Implementing the Doggie Hotel
    19. Renovating the Doggie Hotel
    20. Adding some hotel activities
    21. I can do anything you can, or Polymorphism
    22. It’s about time we teach the other dogs to walk
    23. The power of inheritance
    24. Creating a Doggie Hotel walking service
    25. How are we going to hire a person to walk our dogs if we don’t even have people objects?
      1. Meanwhile, back at the turtle races…
      2. The turtle races case SOLVED
      3. Have you considered a career in coding?
  20. A. appendix: leftovers: The Top Ten Topics (We Didn’t Cover)
    1. #1 List comprehensions
    2. #2 Dates and times
    3. #3 Regular expressions
    4. #4 Other data types: tuples
    5. #5 Other data types: sets
    6. #6 Server-side coding
    7. #7 Lazy evaluation
    8. #8 Decorators
    9. #9 Higher-order and first-class functions
    10. #10 A lot of libraries
      1. Don’t worry, this isn’t goodbye.
  21. B. Bring your brain over to wickedlysmart.com
  22. Index
    1. Symbols
    2. A
    3. B
    4. C
    5. D
    6. E
    7. F
    8. G
    9. H
    10. I
    11. J
    12. K
    13. L
    14. M
    15. N
    16. O
    17. P
    18. Q
    19. R
    20. S
    21. T
    22. U
    23. V
    24. W
    25. Y
    26. Z