Learn to Code in 7 Days
Swift Playgrounds teaches the basics.
Learn real coding the fun way
Curious about coding? It’s easy to dip your toe in the water with Swift Playgrounds, a Mac app that uses fun, interactive puzzles to teach the basics of coding.
Fire up the first “playground,” called Learn to Code 1, and tackle each of its seven chapters a day at a time. By the end of the week, you’ll have a good sense of what it means to be a developer!
Here’s what to expect.
Day 1: Commands
After a quick intro, Swift Playgrounds presents you with a split screen: on the right, a videogame world inhabited by a cute, one-eyed creature named Byte; on the left, a text area where you enter Swift commands to control Byte.
Your task is to get Byte to travel across the terrain and collect gems, enter portals, flip switches, and more. How? By putting the right commands in the right order—which is really what coding is all about.
Enter your first Swift command by simply typing moveForward()—then click Run My Code and watch Byte move forward. Congratulations, you’ve written your first program!
Day 2: Functions
In this session, you’ll make your code more concise by grouping a series of commands to create a time-saving function. So instead of having to enter turnLeft() three times to get Byte to turn right, you might create a function called turnRight() that performs those three left turns with one command.
You’ll also learn how to break a large problem into small steps—a process called decomposition—that you can solve more quickly using functions.
Day 3: For loops
Time for a new tool! Loop indicates how many times Byte executes a command. This will come in handy as the landscape he navigates becomes more complex.
Although solving a puzzle is occasionally a cinch, you’ll often have to experiment, fail, and fix the mistakes you discover. For instance, if Byte walks into a wall or off a ledge, you might realize you’ve added one too many moveForward() commands, or looped too many times.
Pro tip: Swift Playgrounds helps you see the error of your ways by letting you run your program at different speeds—just tap the timer icon. The slowest mode highlights each snippet of code as Byte performs it; the fastest mode is handy when you want to see as quickly as possible whether your program works.
Day 4: Conditional code
Now for conditional code, which lets Byte react to information. For example, you can make Byte collect a gem only if he’s standing under it.
Sometimes you’ll write conditional code that doesn’t have the effect you intended. When Byte fails while following your instructions, it’s hard not to feel bad for him (he frowns and gets all sad), though it can also be kind of funny watching him walk into a wall again and again and again.
Day 5: Logical operators
In your next hour, you’ll learn about logical operators. These allow Byte to react to different combinations of circumstances. Certain conditions, like isOnGem, result in an answer that’s either true or false (called a Boolean). You can make Byte do one thing if a condition is true, another if it’s false.
This is a major jump in your coding skills. Until now, you’ve been able to make Byte follow explicit instructions, but now he’s making his own decisions. (OK, you programmed him to make those decisions, but your little friend is getting smarter—and so are you.)
Day 6: While loops
At this point you’ll start to see how apps you use every day employ the types of commands you’ve learned. You might even start thinking in code. For example, you could imagine a developer writing code to displayTutorial() only if it’s the first time someone has launched the app.
“While loops” solve the opposite problem: They tell a command to repeat as long as a condition is true. For example, you can tell Byte to walk straight ahead as long as there are no paths to the right or left. (In some ways, programming can feel like speaking to a child who takes everything literally: If you don’t tell Byte to check for a wall, he’ll happily run right into it.)
Day 7: Algorithms
Congratulations, you made it to the end of the week! Today’s lesson is really a review of everything you’ve done so far: packaging commands together to create a set of clear conditional instructions for the computer to follow. (You’ve been writing algorithms for days without knowing it!)
You’ll also learn to fine-tune your commands to account for more complex situations: If Byte has multiple ways to complete a task, how do you determine the most efficient one?
There’s still plenty to learn, but making it this far—and having fun doing it—should inspire you to continue the journey.