Learn to write Python code - A quick tutorial for kids

Welcome to a crash course in the programming language Python! Lets get started…

REQUIREMENTS

  • Create a free account at https://repl.it/
  • As you get to each example click open in repl.it
  • When you get to repl.it click the Fork button so you can follow along in another browser

Lesson 1: Hello World

Lets start with getting a program to say “Hello World!” In Python, when we want the computer to “say” something we use the print() command. Just like writing a sentence, you need to put what you want the computer to say inside double quotes like this: print("Hello World!") Anything between double quotes in programming is called a string. Let’s try it below.

  • Try changing the string from "Hello World" to whatever you want!
  • Write more print("Another command!") below it and see what happens.
  • Mess around and don’t worry you can’t break anything.
  • Press the green play button to run the code.

Lesson 2: Variables

Now that we know how to make our program say things, let’s talk about variables. A variable is a way to store something. You can store all kinds of things inside variables - words, numbers, pictures, functions, the list goes on! Lets look at an example:

Max is 3 years old, and Hank is 5 years old. Let’s say we want to store their ages in variables. To do that in Python we would do:

maxAge = 3
hankAge = 5
print("Hank is:")
print(hankAge)

What do you think this program would say if you ran it?

Hank is:
5

What would you change to make it tell us how old Max is?

print("Max is:")
print(maxAge)

When you run this it would say

Max is:
3

Wouldn’t it be cool if we could make our code all print out on the same line like this Max is 3?

To do that and combine the 2 strings simply add them together like this

print("Max is" + maxAge)

This would print out:

Max is 3

Great work! Now let’s do some coding!

Look at the project below… What do you see?

  • We have 3 variables cowSound, pigSound, duckSound
  • We have 3 print commands
  • Press the green play button to see what happens

Uh-oh! Look what gets printed out when we run our program…

The cow says Moo!
The pig says
The duck says

Can you figure out how to fix it? Scroll below if you need some help.

Answer:

  • First we were missing a sound for our duck! to fix that we need to type Quack! between the double quotes.
  • Now if we run the program it will say:
The cow says Moo!
The pig says
The duck says Quack!
  • Now how come our pig isn’t talking!? If you look closely at the print() command you will see why…
  • Of course we forgot to add the sound!
  • Change print("The pig says") to print("The pig says" + pigSound)
  • Run the code again and all of our barnyard friends should be speaking properly!

Lesson 3: Lists and Loops

An list is another way to store things, but it can hold a list of things. Lets look at a few examples:

  • Lets say our parents want to make cookies and we need to create a shopping list
  • Think of the things you need to make cookies: Milk, eggs, sugar, chocolate chips, flour.
  • See how we wrote that sentence above? we separated each item we need with a comma. Writing code is very similar:
shoppingList = ["Milk", "Eggs", "Sugar", "Chocolate chips", "Flour"]

Lets print that list out…

print(shoppingList)

That will print out:

['Milk', 'Eggs', 'Sugar', 'Chocolate chips', 'Flour']

Now lets find out how to print specific items in our shopping list. Each item in our list has its own index. An index is a number used to get that item from the list. Lists always start with the number 0.

   0       1        2             3             4
['Milk', 'Eggs', 'Sugar', 'Chocolate chips', 'Flour']

So to print out the first item from our shopping list, we would write: print(shoppingList[0]) which would print Milk .

  • We access the items by using the name of our list shoppingList and putting the index of the number we want inside [ ]
  • So to print out Sugar we would do print(shoppingList[2])

Now how would you get our program to print out Flour ?

Answer: To print out Flour we would do print(shoppingList[4])

Lets practice and teach you about Loops!

  • A loop is a way for the program to do something over, and over, and over again
  • We use loops to do repetitive tasks for us so we don’t have to
  • Take a look at the code below.
  • Add some of your favorite movies to the favoriteMovies list
  • Let’s show you your first loop
for movie in favoriteMovies:    print(movie + " is one of my favorite movies")

This loop says … for each moviein our favoriteMovies list we want our computer to say “_____ is one of my favorite movies”

Press the play button to see what happens!

How cool is that! with one loop, we can make our program say lots of things!

Now try to get the program to say which movie in the list is your absolute favorite! Remember above how we use the index to get a specific item in a list.

Hint: print("My absolute favorite movie is " + favoriteMovies[PUT INDEX HERE])

Find all the tutorials here: https://repl.it/@BartPowers1

Software Engineer