Programming for Kids

Similar documents
Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Programming for Beginners

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Week - 01 Lecture - 04 Downloading and installing Python

Git Workbook. Self-Study Guide to Git. Lorna Mitchell. This book is for sale at

Your First Meteor Application

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

A computer program is a set of instructions that causes a computer to perform some kind of action. It isn t the physical parts of a computer like the

XP: Backup Your Important Files for Safety

Your . A setup guide. Last updated March 7, Kingsford Avenue, Glasgow G44 3EU

Hello! ios Development

Functional Reactive Programming on ios

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Intro. Scheme Basics. scm> 5 5. scm>

Upload Your Site. & Update. How to

Using GitHub to Share with SparkFun a

Word: Print Address Labels Using Mail Merge

Using IDLE for

Lab 1: Setup 12:00 PM, Sep 10, 2017

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Decisions, Decisions. Testing, testing C H A P T E R 7

How To Upload Your Newsletter

DOWNLOAD PDF EXCEL MACRO TO PRINT WORKSHEET TO

Eanes ios5 Upgrade Guide

Functional Programming in Ruby

Introduction to L A TEX for MCS-236

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

the NXT-G programming environment

Printing Envelopes in Microsoft Word

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Introduction to Programming

Introduction to Programming with JES

Tutorial 1: Unix Basics

COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition

CMSC 201 Spring 2018 Lab 01 Hello World

What is the Best Way for Children to Learn Computer Programming?

Introduction to Unix - Lab Exercise 0

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

T H E I N T E R A C T I V E S H E L L

If Statements, For Loops, Functions

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

Civil Engineering Computation

Teach Yourself Enterprise Architect in Ten Days

Exploring UNIX: Session 3

Touring the Mac S e s s i o n 4 : S A V E, P R I N T, C L O S E & Q U I T

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Xchange for Samsung MAC User Guide. Version 2.4

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

4Sight for Mac User Guide. Version 2.4

COPYRIGHTED MATERIAL. An Introduction to Computers That Will Actually Help You in Life. Chapter 1. Memory: Not Exactly 0s and 1s. Memory Organization

Registering for the Apple Developer Program

Part II Composition of Functions

Ruby on Rails Welcome. Using the exercise files

How to Make a Book Interior File

Excel Basics: Working with Spreadsheets

1 Installation (briefly)

Physics REU Unix Tutorial

Legal Notice: Copyright 2012 and 2013 Hitman Advertising, all rights reserved. You are responsible for your own advertising in your business.

S A M P L E C H A P T E R

July 2014 IT Tips and TechSolutions News Vol. 4 No. 7 In This Months TechPoints

ASCII Art. Introduction: Python

CMSC 201 Spring 2019 Lab 06 Lists

Chapter 1. Getting Started

Software Compare and Contrast

CS 177 Recitation. Week 1 Intro to Java

Installing VS Code. Instructions for the Window OS.

Boolean Expressions. Is Equal and Is Not Equal

Text Input and Conditionals

A SIMPLE STEP-BY-STEP GUIDE FOR COMPUTER BEGINNERS

1. Welcome to ANCS+ 8.0 Automated Inventory Program. This course will teach you how to create and print your random sample, controlled property

Using the Zoo Workstations

9.2 Linux Essentials Exam Objectives

EchoSub v1.2 EchoStyle

Grunt Cookbook. Effective Recipes To Master Grunt. Matt Goldspink. This book is for sale at

Yup, left blank on purpose. You can use it to draw whatever you want :-)

Molecular Statistics Exercise 1. As was shown to you this morning, the interactive python shell can add, subtract, multiply and divide numbers.

Rockablepress.com Envato.com. Rockable Press 2010

IMPORTANT WORDS AND WHAT THEY MEAN

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

A Step by Step Guide to Postcard Marketing Success

Lesson 2 page 1. ipad # 17 Font Size for Notepad (and other apps) Task: Program your default text to be smaller or larger for Notepad

Running Java Programs

CME 193: Introduction to Scientific Python Lecture 1: Introduction

It would be interesting to determine the number of great ideas that

Adding content to your Blackboard 9.1 class

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

ONLINE REGISTRATION: A STEP-BY-STEP GUIDE

TI-83+ Z80 ASM for the Absolute Beginner

The Problem, the Solution and the End FinalPart

CMSC 201 Spring 2017 Lab 01 Hello World

CMSC 201 Fall 2018 Lab 04 While Loops

Testing and Debugging

Chapter 1 Introduction

Physics 306 Computing Lab 1: Hello, World!

APPM 2460 Matlab Basics

Barchard Introduction to SPSS Marks

Docker for Sysadmins: Linux Windows VMware

Filter and PivotTables in Excel

Touring the Mac. S e s s i o n 3 : U S E A N APPLICATION

Transcription:

Programming for Kids Peter Armstrong This book is for sale at http://leanpub.com/programmingforkids This version was published on 2016-05-08 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. 2012-2016 Peter Armstrong

Tweet This Book! Please help Peter Armstrong by spreading the word about this book on Twitter! The suggested hashtag for this book is #programmingforkids. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#programmingforkids

For Evan, literally

Contents Introduction....................... i Chapter 1: Hello, Command Line!........... 1 Exercises....................... 11 Chapter 2: Hello World!................. 12 Exercises....................... 14 Chapter 3: Hello, irb!.................. 16 Exercises....................... 17 Chapter 4: Functions.................. 18 Exercises....................... 22 This is a Sample..................... 23 For Parents........................ 24 About the Author.................... 27 About the Cover..................... 28 About Leanpub...................... 29

Introduction This book will teach you how to write computer programs! You will need to use a Mac computer to follow along. The programs are short, so you can type them all in yourself. This is true even if you can t type well. This book has a bunch of small chapters. Each chapter is about one idea. At the end of every chapter, there will be exercises for you to do. It is really important that you do all of them! Doing the exercises ensures that you know the material in the chapter. The answers for all the exercises are in the back of the book, and there are links to the answers for all the exercises. If your parents want to find out more about this book, they can read it with you. Or, they can just read the For Parents section at the back of the book. If you re going to use their computer, they might want to sit with you. That s fine. They ll learn something too! If you are following along on your Mac, you want to read the PDF file that your parents downloaded. If you double-click on the PDF file, it will open in a program called Preview. You will be switching back and forth between reading this book in Preview and typing stuff in Terminal. If you have clicked on the Preview to switch pages, you will need to click on the Terminal window to focus it before typing any commands. If switching between Preview and Terminal gets annoying, there are a couple things you can do. If your parents have a i

Introduction ii printer, they can print the book. Or, if they have an ipad, they can download the EPUB file, add it to itunes and then sync it to the ipad. Let s get started!

Chapter 1: Hello, Command Line! This chapter is about the command line! This book is for kids like you who use a Mac computer, either at home or at school. Normally you use a Mac by clicking on stuff with a mouse. In this chapter you will learn a different way to use a Mac. It is called the command line, and you just use the keyboard. Once upon a time, about 30 years ago, all computers had was a command line. Even though the command line is simple, it is also very powerful. If you know how to use it, you will be like a wizard who can type strange spells and make your computer do amazing things. Relax, it s not scary. If you get something wrong, you won t break anything! First we need to learn the basics. You already know what a keyboard is. But there may be some keys you may not have used before. These are the Command, Option, Control and Shift keys. 1

Chapter 1: Hello, Command Line! 2 The Command, Option, Control and Shift Keys We are going to start a program called Terminal. Terminal is what lets you use the command line on your Mac. To start Terminal, hold down the Command key and press the Space bar. (A shorter way of saying this is to say type Command + Space. So, that s what I will say from now on.) Typing Command + Space opens a program called Spotlight in the top right corner of your screen. Spotlight Spotlight lets you type the names of programs to run. We are going to run Terminal. So, type terminal in Spotlight and hit the Enter key. You might see it show up in a drop down list after you type term. If so, you can just click on that choice instead of finishing typing terminal.

Chapter 1: Hello, Command Line! 3 Running Terminal You will see Terminal, which will look something like this. Terminal Terminal is the command line of the Mac. (Don t worry about the []$ stuff at the beginning of the line. That s just what the prompt looks like on my computer, and it is possible to customize your prompt. Your prompt might look like > or.) Before we get started, let s have the command line say hello to us. To do this, we re going to run the say program. Type say hello in Terminal.

Chapter 1: Hello, Command Line! 4 Then, press the Enter key to run the command. Hopefully your Mac said hello to you! If not, make sure the volume is on and try that again. By the way, after entering any command in Terminal you need to hit Enter to run it. Now, let s start by making a folder for all the stuff we are going to do in this book. Type mkdir stuff in Terminal and hit Enter. The command mkdir is said make dir and stands for make directory. Directory is another word for folder. So, you just

Chapter 1: Hello, Command Line! 5 made a folder called stuff. Next, we are going to go into that folder. Type cd stuff in Terminal and hit Enter. The cd command stands for change directory. So, you are changing into the directory called stuff that you just made. Let s look inside this directory. In Terminal, type ls (a lowercase L and a lowercase S) and hit Enter. Typing ls shows nothing! The ls command means list, which means to show the files and folders inside this folder. There is nothing in this folder, since we just made it. So, this is why typing ls shows nothing. Now, let s change that by creating a file. We are going to use a program called a text editor to edit the file. A text editor lets you type words in a file, and save the file just like you would save a game. We are just going to use a small text editor called nano. Nano means really small, and it s a good name since nano is a really small, simple text editor. So, in Terminal, type nano and hit the Enter key.

Chapter 1: Hello, Command Line! 6 This starts nano. The screen will look like this. In nano, type hello. You don t need to hit the Enter key. You can see that nano is being helpful, by showing you the list of commands you can type by holding down the control key and typing a letter. (Nano is showing the control key like the caret (ˆ), but it is not shift + 6, it is the control key.)

Chapter 1: Hello, Command Line! 7 So, to save the file, hold the Control key down and type the o key. Nano will ask you what you want to call the file you are saving, by showing text saying File Name to Write. Type hello.txt and press the Enter key. Nano will save the file and tell you it was one line long, by saying Wrote 1 line.

Chapter 1: Hello, Command Line! 8 (If you had hit Enter after typing hello earlier, it will say Wrote 2 lines. That s fine, don t worry.) Now that we ve saved our file, let s quit nano by holding down the Control key and typing x. (A shorter way of saying this is to say type Control + x. So, that s what I will say from now on.) You will be back at the command line inside Terminal. Let s look inside our stuff folder again. Remember last time we typed ls we saw it was empty. Type ls in Terminal again. You will see the hello.txt file you created.

Chapter 1: Hello, Command Line! 9 Let s look inside this file. Type cat hello.txt in Terminal and hit Enter. Here, cat is not an animal. Instead, it is a command that shows you what is in your file. Since we typed the word hello, this is what you will see. So, the hello.txt file had hello inside it, so when you typed cat hello.txt you saw hello. You now know how to use some of the basic commands of the command line! You learned a bunch of strange commands, things like mkdir, cd, ls and cat. The neat thing is that these commands are actually just programs that were written by other people! In the next chapter, you will create your first program, which you will also be able to run from the command line! Finally, we are going to quit Terminal. Type Command + Q or choosing Quit Terminal from the Terminal menu.

Chapter 1: Hello, Command Line! 10 By the way, if you ever get something on the commmand line really wrong and you don t know what to do next, you can always just quit Terminal and open it up again. Make sure you do the Exercises on the next page before continuing!

Chapter 1: Hello, Command Line! 11 Exercises 1. Start Terminal and cd into the stuff directory. 2. Use nano to make a file named hooray.txt that contains the text hooray. Quit nano when you re done. 3. Use the cat program to see the contents of your hooray.txt file. 4. Quit Terminal. You can see the answers to the exercises, or just continue to the next chapter.

Chapter 2: Hello World! Let s write our first computer program! Start Terminal, cd into the stuff directory and run nano. (If that did not make sense, see the Chapter 1 Exercises.) We re going to create a really simple program. Type puts "Hello World!" in nano. There s no need to hit the Enter key. I ll talk more about what puts means later. To type a quote mark like ", hold the shift key and type the ' key. There s no difference between the start and end quote marks. That s it! Type Control + o to save the file. 12

Chapter 2: Hello World! 13 Nano will ask you what you want to call the file you are saving, by showing text saying File Name to Write: Type hello.rb and press the Enter key. As you type the file name, you will see it shown in File Name to Write area near the bottom of Terminal. Nano will save the file and tell you it was one line long, by saying Wrote 1 line. (If you hit the Enter key, it will say Wrote 2 lines. That s fine too.) Now that we ve saved our file, type Control + x to quit nano. Let s run our program! In Terminal, type ruby hello.rb and hit Enter. You need a space between ruby and hello.rb.

Chapter 2: Hello World! 14 Your First Computer Program! Congratulations, you ve written your first computer program! As you just saw, puts printed something to the screen. But, what was that strange word ruby? Ruby is a programming language. You speak English, but there are lots of other languages that people speak. Similarly, there are lots of different languages you can use to tell a computer what to do. Ruby is one of the easier ones to use, and your Mac comes with Ruby already installed. The program you wrote was a Ruby program! The file extension (the stuff after the. in the filename) for Ruby programs is rb, so we named the file hello.rb. In the next chapter, we will play with Ruby on its own command line! Exercises 1. Write and run a Ruby program named hi.rb that prints

Chapter 2: Hello World! 15 Hi!. You can see the answers to the exercises, or just continue to the next chapter.

Chapter 3: Hello, irb! Ensure that Terminal is running, and that you are in the stuff directory. (If that did not make sense, see the Chapter 1 Exercises.) Ruby is a programming language. It also has its own command line called irb, which stands for Interactive Ruby Shell. To start irb, just type irb in Terminal. Note that irb has its own prompt. On my Mac, it looks like irb(main):001:0>. If you have an older version of Ruby on an older Mac, your irb prompt might just look like >>. The irb prompt just lets you know that irb is waiting for you to type a command. We ll ignore the other details that it shows. Anyway, let s get irb to do your math homework! Type 2+2 in irb and hit Enter. For your parents sake: you should just use irb to check your answers when you do your math homework! :) 16

Chapter 3: Hello, irb! 17 To do multiplication, you use * (shift + 8) not x. Try 3*2 in irb. 1 irb> 3*2 2 => 6 To do division, you use / (which is beside the right shift key). 1 irb> 6/2 2 => 3 To quit irb, type quit. One of the reasons that Ruby is an easy programming language to learn is that it has irb, so you can experiment interactively really quickly. In the next chapter, we re going to learn what functions are! Exercises 1. Have irb do 3-2. The minus key, -, is beside the 0. 2. Have irb do 3.0/2. What do you think the answer is? You can see the answers to the exercises, or just continue to the next chapter.

Chapter 4: Functions If all we could do with programming languages was add numbers and say hello to ourselves, we wouldn t have anything more than a lousy calculator! Let s do something more interesting! We re going to define a function. Most computer programs are made up of a lot of functions. A function is something that does some stuff and returns a value. The return value is the answer. Start irb again by typing irb in Terminal. (If Terminal isn t running, start it using Spotlight, and go into your stuff directory by typing cd stuff.) Let s make a function that knows how to double any number. Type this in irb, hitting Enter at the end of each line: 1 def double(x) 2 x+x 3 end To type ( you hold shift and type 9, and to type ) you hold shift and type 0. 18

Chapter 4: Functions 19 This function is only 3 lines long, but there s lots going on here. The word def is how we start defining a function in Ruby. (Programmers don t like to type long words, so def is short for define.) The inputs to the function are put in brackets, which are the () things. In this example, we have one input. Since we don t like typing, we will name this input x. The name of the function is called double. One way to double a number is to add it to itself. To double 2, you say 2 + 2, which is 4. To double 3, you say 3 + 3, which is 6. What we are doing is making a function which can double any number. We don t know what that number is going to be in advance, so we just called it x. So, to double x, we just say x+x. Finally, the last line is end, since that is how you end a function definition in Ruby. When you typed this in, you saw that defining the function printed => nil.

Chapter 4: Functions 20 All this means is that return value of actually defining the function is nothing. In Ruby, we call nothing nil. Anyway, we made a function that knows how to double a number, by adding it to itself. Let s see how it works. I m going to show the irb prompt as irb> from now on. This way, you will see what you type as input to irb (at the irb prompt) and what irb prints as output (with a => arrow). Anyway, type double 2 in irb. When you follow along, you do not actually type irb>. Just type the stuff after it. 1 irb> double 2 2 => 4 2 + 2 is 4, so that looks right. When you define a function you use def end. When you use the function that you defined, you are said to be calling the function To call a function, you just use the name of the function, and any arguments. So double 2 is a function call. You can also put the arguments to the function in brackets, like this: double(2). Let s try a bigger number!

Chapter 4: Functions 21 1 irb> double 123 2 => 246 123 + 123 is 246, so that looks right too! Let s make another function. This one will triple numbers! To triple something, you either add it to itself twice (2+2+2 is 6, and 6 is the triple of 2) or you multiply it by 3. The way you multiply something in Ruby (and in most programming languages) is with *. 1 irb> def triple(y) 2 irb> y*3 3 irb> end Note there is a space between def and triple. Here, we re going to name the input to the function y, not x. This input is called a parameter. It doesn t matter what you name the parameter, as long as you use the same name inside the function. So, you could have said this (don t actually type this): 1 irb> def triple(dog) 2 irb> dog*3 3 irb> end Also, note that spaces don t usually matter. So, you could also have said this (don t actually type this either):

Chapter 4: Functions 22 1 irb> def triple(cat) 2 irb> cat + cat + cat 3 irb> end Anyway, let s test our new function! Type triple 3 in irb, and hit Enter. 1 irb> triple 3 2 => 9 3 * 3 is 9, which is the same thing as 3 + 3 + 3. So, this looks right! Let s try a bigger number. 1 irb> triple 10 2 => 30 10 * 3 is 30, which is the same thing as 10 + 10 + 10. We ve now made 2 functions! In the Exercises, we ll make 3 more! Leave irb open. Exercises 1. Write a function called quad to quadruple a number by mulitplying it by four. Test it afterward. 2. Write a function called quad2 to quadruple a number by adding it to itself the correct number of times. 3. Write a function called quad3 to quadruple a number by using the double function you made before. You can see the answers to the exercises, or just continue to the next chapter.

This is a Sample This is a sample of a longer book, which is available for sale on Leanpub at https://leanpub.com/programmingforkids¹. The full book is 13 chapters. The English version is over 170 pages and over 19,000 words. 30% of all royalties go to Watsi, which is a great charity! ¹https://leanpub.com/programmingforkids 23

For Parents This is a book for you to read with your child, or for your child to read by himself or herself. I wrote it to teach my 9 year old son the basics of programming. He has been playing video games for years, and he wants to learn programming since he wants to make his own video games someday. This book is intended to be the first step. (No, it doesn t teach you how to create the kinds of video games a 9 year old can dream up; that takes a lot more knowledge!) The reason this book exists is to be the best book in the world for a kid who is wanting to learn to program computers to read first. Computer programming is a good skill to have, regardless of what occupation your child eventually does as an adult. (I d argue it s much more important than lots of the math than you learn in high school, for example.) But more importantly, learning how to program computers teaches a rigor and discipline of thinking which is useful in any field. This book exists to show kids that they can program computers, and to help them get started. With the exception of this appendix, this book is written like a book for kids in elementary school. My goal is that this book should be accessible for kids between ages 9 and 14. In North America, that s grades 3-8. My son is working through it as I write it. (I don t think it s a good book for kids aged 7 and 8: I started writing this book when my son was 7, but he wasn t ready for it. So, I paused writing it for 2 years. If your child 24

For Parents 25 is 7 or 8, I think that something like Scratch is a better choice for kids of that age.) No knowledge of programming is assumed. The examples are as short as possible, since I assume the reader can t type well. (My son can t touch type, so if I make long examples I ll hear about it!) My goal is for this book to be the best programming book for kids to read first. After this book, they can follow what interests them. This book is written assuming you are using a Mac. I think that a Mac is the best computer for kids to learn to program on. Since this is a beginner book for kids, I can t write it generically to cover Mac, Windows and Linux. I have to pick one operating system, and have the child follow along verbatim. Besides teaching programming, the book also teaches basic use of the command line on a Mac. This is accessed via the Terminal program. The reason for this is that I feel that the best way to learn is to follow along, and the simplest way to follow along is to type everything. Real programmers use the command line every day. If you want to learn programming, you should use Terminal and files. Yes, you can play with stuff in a web browser at places like Codecademy, and while this is very friendly and instructive, it is fundamentally a different activity from what real programmers do. And, besides being easier, it s somehow less rewarding. If you are letting your child use your Mac computer to follow along, I strongly recommend you sit beside them and follow along! For example, I m not planning to teach the command to delete files, but it s fairly short!

For Parents 26 The examples are in Ruby. Ruby is a fairly simple programming language. If you ve ever heard of websites built on Ruby on Rails, you ve heard of Ruby: it s the programming language that Rails is written in. This is not a book about how to learn Ruby, however. The examples could have easily been written in JavaScript, CoffeeScript or Python. Finally, I really want your feedback! Did your child get stuck anywhere? If you have anything to say about the book, I want to hear it! Please email me at peter@leanpub.com and let me know!

About the Author I m the founder of Ruboss, a software consulting company based in Vancouver, BC, Canada. We re the creators of Leanpub, a website that anyone can use to self-publish in-progress ebooks like this one. I m also a programmer, an author and a father. I ve written two books for programmers (Flexible Rails and Hello! Flex 4), so I know how to explain things to fellow programmers. And my son is a very intelligent 9 year old, so I have a lot of experience explaining things to a smart child. This is probably the most challenging book I will ever write, as I honestly want this to be the best introduction to programming for all kids. 27

About the Cover The cover photo² is by Gareth Newstead³ and is from Unsplash⁴. ²https://unsplash.com/photos/YoC_rDkSS1U ³http://www.garethnewsteadphotography.com/?utm_medium=referral&utm_source= unsplash ⁴https://unsplash.com/ 28

About Leanpub This is a Leanpub book. I m the cofounder of Leanpub. Leanpub is a website which lets anyone publish their own books as they write them. The idea of publishing an in-progress ebook is something I call Lean Publishing. You can learn more about Lean Publishing by reading a free book⁵ that I wrote last year. Besides being totally free to use, being a Leanpub author can also be profitable. We pay authors 90% minus 50 cents royalties per copy sold. So, a $10 ebook pays $8.50 in royalties per copy! ⁵https://leanpub.com/lean 29