So You Want to Learn Python (for Kids!)
Excellent choice! There’s plenty of reasons why learning Python is rising in popularity, but for kids, Python is a great programming language with which to start learning to code. Python is a powerful, easy-to-read, high-level programming language. This means commands read like English words instead of complicated 0s and 1s and this makes it easy for kids to learn Python without a lot of experience. This python tutorial for kids will help parents and teachers get their kids learning Python.
What is Python?
If you are completely new to computer programming, you might be wondering what Python is. Python is a programming language. Programming languages are simply a special way of giving computers sets of instructions to execute. You are probably familiar with some of the most common programming languages like Java or PHP. Learning Python is becoming more and more popular and Python was recently listed as one of the top 10 programming languages to know in 2018. In fact, that’s why teaching Python programming for kids has become so popular. Python is a programming language that provides real skills for the future. It is used to develop software and apps in a variety of settings. Many computer programmers enjoy using python because it is easy to read and accessible even to beginners.
Why is Python a Great Choice for Kids?
Is Python easy to learn? Yes! The commands and syntax (rules for how code must be laid out) in Python are relatively simple compared to some other programming languages. This makes Python for kids easy to get started, even with no experience coding. Another great feature when looking to design python exercises for kids is that Python has a wide range of libraries that we can import whenever we need a particular feature. This modular feature keeps Python flexible and also lets you use others’ libraries to easily build some interesting (and fun!) initial projects.
How Can I Help My Kids Learn Python?
Whether you are a teacher or parent, getting kids started learning Python is simple. Today we will be going over some simple python tutorials for kids that will make getting started learning Python for kids super easy. In today’s Python lesson, we are going to be reviewing very simple programming commands so that you and your students can get familiar with how Python works, and how we can eventually use this program to develop fun games and projects for kids.
This MASSIVE Python for Kids Tutorial is broken into three lessons. Each of these python lessons for kids will review some basic coding concepts and apply our knowledge to teach kids python. You can use the table of contents below to help Navigate through the Python tutorials so that you can go at your own student’s pace.
Python for Kids Tutorial One: Syntax, Loops, and Variables
What Concepts will be covered:
Today we are going to be exploring and learning about the following coding concepts:
- Syntax: Syntax is essentially the ‘spelling and grammar’ of computer programming languages. Just as it may be difficult to understand an English sentence without proper spelling and grammar, a computer can’t understand their commands unless they are laid out properly. Syntax defines the proper way to lay out commands in programming languages.
- Variables: In computer programming, a variable is a type of value that can change. In this python tutorial, we will be exploring how we can change variables in Python, and how this will affect the output of our programming.
- Loops: Loops contain a set of instructions that are continually repeated until a specific set of conditions are met. In this tutorial, we will learn to understand the difference between a for loop and a while loop.
How to open Python on your computer:
If you don’t yet have a way to code in Python and are unsure of how to begin, I personally like to use Anaconda, which includes the Spyder program (also known as an IDE, an integrated development environment). You can download Anaconda for free here.
Or, if you are looking for a really simple way to get started with Python right away, you can use an online Python IDE editor. Simply open up this page, https://www.tutorialspoint.com/online_python_ide.php, and you will be ready to get started right away!
Python Tutorial for Kids: Creating a FOR loop
Let’s get started learning about and understanding variables and for loops with the range command.
Once you and your student have a Python editor open, enter this text:
for x in range(1,6): print (x)
and run the program. Make sure they have an indent on the second line!
This is what you should see:
1 2 3 4 5 >>>
Ask your student to interpret what happened. Have them change the numbers in the range() method. (A method is just a name for a Python command.) What happens when you set the range to (1,3) what about (1,100). Your students will soon understand how to construct a python list of numbers within a certain range.
The goals are for your student to understand the limits of the range method (it won’t print the last number, e.g. 6), and to understand what a variable is.
We have just created a
for loop. What is a for loop? As we discussed earlier, loops are commonly used in computer programming. Loops give computers a set of instructions that are continually repeated. In a for loop, the computer executes the command for a fixed number of times. In our case, this is defined by the range.
We can also have our program list our numbers in reverse order. Have your students enter the following text:
for x in range(6,1,-1): print (x)
Did you see what happened there? Now we can use this method to help us code a popular children’s song. Have your students enter the following text:
>>> for x in range(5,0,-1): print (x, 'little monkeys jumping on the bed, 1 fell off and bumped his head, momma called the doctor and the doctor said, no more monkeys jumping on the bed')
You should see the following:
5 little monkeys jumping on the bed, 1 fell off and bumped his head, momma called the doctor and the doctor said, no more monkeys jumping on the bed 4 little monkeys jumping on the bed, 1 fell off and bumped his head, momma called the doctor and the doctor said, no more monkeys jumping on the bed 3 little monkeys jumping on the bed, 1 fell off and bumped his head, momma called the doctor and the doctor said, no more monkeys jumping on the bed 2 little monkeys jumping on the bed, 1 fell off and bumped his head, momma called the doctor and the doctor said, no more monkeys jumping on the bed 1 little monkeys jumping on the bed, 1 fell off and bumped his head, momma called the doctor and the doctor said, no more monkeys jumping on the bed
Python Tutorial for Kids: Variables
Now let’s have fun with the variables in this code! In our case, the variable in this code is x. What happens when they change the variable x in the first line to a y? Does this change if both variables are changed to a y? If they change the x in both lines to instead be the word RandomChickenVariable, will it still work? It’s a terrible variable name but yes! The variable does not have to be an ‘x’ or a ‘y’ it can be anything that you choose.
Python Tutorial for Kids: Creating a while loop
Let’s move on to now understanding while loops. Unlike for loops, which typically stop after a fixed number of times, while loops will stop only when a specific condition is met.
Have them enter this text:
x=0 while x is not 10: x=x+1 print (x) print('done!')
You should see the following:
1 2 3 4 5 6 7 8 9 10 done!
Have them describe what the code is doing using the words variable, and loop. In this example, x is the variable. x starts at 0 and increased by 1 each time the loop is run according to the formula x=x+1. Once 10 is reached, the condition to end the loop has been met, and the loop is finished. You will then see ‘done!’ printed.
The last code we ran was a for loop – this is called a while loop. Loops are useful because they can control our progress through our code; the ‘done!’ will not print until the loop has stopped running.
Python Tutorial for Kids: The Importance of Syntax
As we noted previously, Syntax is the spelling and grammar of computer programming. Computers will only be able to execute commands if we give it to them in a language that they understand. To help your student understand the importance of syntax in Python, have them remove the indent in print x so that it looks like this.
x=0 while x is not 10: x=x+1 print (x) print('done!')
Let your student play with the code. When you discuss the difference between these two versions with your student, the ultimate conclusion should be that the boundaries of loops are defined by the indents beneath their opening “for” or “while” line. The loop will not perform any code below the unindented line. However, if you try this:
x=0 while x is not 10: x=x+1 print x print('done!')
the code will fail with a message that looks something like this:
File "<ipython-input-10-ebd4d8eb92d4>", line 5 print('done!') ^ IndentationError: unexpected indent
Notice that Python sometimes tries to help you see where your error is by putting a carat ^ in the error message. This error occurs because there is no reason that the print(‘done!’) command should be indented. This is an error in the syntax. The computer cannot understand the command because the ‘spelling and grammar’ is wrong.
Useful tip: If your program gets stuck, you can press ctrl-c in the console to cancel the program, or click the red square to stop operation. Want to see what that looks like? Run this with your student:
x=0 while x is not 10: print x x=x+1
Have them explain why it is not working. The answer is that the value stored in the x variable never reaches 10 within the loop, so it will run forever and keep printing 0s.
Python Tutorial for Kids: Importing a Library
Our last exercise for this lesson will involve using a library I mentioned earlier. In this exercise, we will be turning our computer into a digital dice!
Type in this code:
from random import randint x = randint(1,4) print("dice roll:") print(x)
The library is random, and the method we are taking from it is randint.
random is a type of module in Python that gives us several functions available for use.
.randint(x, y) is a type of function available through
random. This function takes two parameters (two variables
y), it will select a random number between
y. You can set x and y to whatever numbers you like. In this example, we chose 1,6, just like a dice!
If there were many functions we knew we would need, we might just type import random – we’ll cover that another time!
Have your student describe what the code does. Once they have completed the above task, you can think with them about other modifications that can be made, such as changing the minimum and maximum of the numbers that can be produced or deciding to only roll again if the number is less than or equal to five.
This might look like this:
from random import randint roll=randint(1, 6) print(roll) if roll < 5 : repeat=roll print(roll) else: print("You lose")
If your code does not run, common errors are found in parentheses, colons, and indents, or the lack thereof.
- Logic statements like if, while, and for need to have their lines ended with a colon.
- For loops are only in effect for the lines that are indented underneath them. Make sure you only have one indent more than the for loop!
After these exercises, your student now has had experience working with variables, loops, logic statements, and importing functions. Welcome to Python!
Python for Kids Tutorial Two: All About Lists
Programming often has a lot of words that sound intimidating to kids learning python. One important thing to keep in mind as we study programming with Python is that every problem can and should be broken down into multiple steps. This helps us make clean code that other people can read without confusion. The following lessons introduce working with, editing, and storing data, which is just a fancy word for information.
Concepts covered today:
- Data types – there are multiple types of data that are defined in Python. We will be learning them gradually as we work with more and more types of Python commands!
- Lists – a set of information in a specific order that can be changed
Creating a List
Lists are very easy to create in Python. We simply put a series of comma-separated items between square brackets. We can create a list of words by typing in the following:
myList = ["I", "don’t", "like", "pickles", "in","my", "sandwiches"]
This action is called declaration in programming; we have just declared the myList variable. This list stores a set of words. The two square brackets are important to define the list. We can use commands to access information about the list and to edit the data in the list.
How can we access information from a list?
Let’s say we want some basic information about this list. How long is it? What is the first piece of data stored? How about the last? What type of data do we have stored in it? We will now learn a variety of python commands to access information from our list.
If we want to know the length of our list, we enter this command:
You should see something that looks like this:
>>> len(myList) 7
The result of this is the length of your data.
The items in our list are indexed so that we can retrieve them easier. We can use the index operator  to find an item in our list. To look for the first piece of data, have your student type in myList. What do they notice? Is it what they expected?
>>> myList 'don’t'
What your student will get in return is “don’t” printed out. Let them experiment and try to get “I” as an answer. The correct way is to command the console to print myList. This shows your student that lists in Python are indexed starting with 0. Indexing looks like this:
What happens when they search for the last element of the list? Let your student figure out that myList will throw an error. Discuss with them that this occurs because, even though the length of the list is 7, the indexing beginning at 0 means that the last element is at index 6. This might be confusing at first, but with more practice, your student will get used to it quickly!
What type of data is stored within a list?
Now have your student enter type(myList). This will return something like the following:
>>> type(myList) <class 'list'>
Hm. That’s not quite what I wanted to ask. I want to know what type of information is stored inside the list. Let’s try this:
>>> type(myList) <class 'str'>
That looks better! ‘Str’ stands for string. Strings are bits of text; you can tell that a variable is a string when it has single or double quotes around it. If you look back at your previous commands, you’ll see that we declared the myList list entries to all have quotes around them.
Lists, strings, and integers! How do we tell them apart?
Let’s look in detail at 3 different types of data: lists, strings, and integers.
Your student can play around with this and become more familiar by defining two more variables.
pickles = ‘I don’t like pickles in my sandwiches’ pickles2= [“ I don’t like pickles in my sandwiches”]
Have them run the type and len commands on each of these and compare it with the results of myList. What do they notice? Let them explore on their own with their own variables if they want; part of the fun of programming is being able to easily create test examples to try out whatever weird ideas you have.
Here is what we get when we run the len commands on our two variables:
>>> len(pickles) 37 >>> len(pickles2) 1
Here is what we get when we run the type commands on our two variables:
>>> type(pickles) <class 'str'> >>> type(pickles2) <class 'list'>
Ultimately, the point is that the pickles variable is a string, not a list. The square brackets define a list. This variable will have a length of 37 because the len function counts the characters in the string. On the other hand, pickles2 is a list with one element in it, surrounded by quotes, which is why it has a length of one.
So far, we have learned about two types of data: lists and strings. Ask your student to look at the information we have gotten from the Python console. Do they see any other kind of data? Guide them to see the numbers 1 and 39, and have them type in type(39) and type(1). The resulting int answer represents an integer, which is any whole number, negative, positive, or 0.
>>> type(39) <class 'int'>
If your student is interested, have them try to access the first letter of the pickles string!
Let them try to guess how to do it with the hint: “it’s similar to how you access information in a list.”
The answer is pickles. What type of data is pickles? It is also a string. A string, it turns out, is made of multiple smaller strings!
Here are some examples of how we can access letters in our pickles string:
>>> pickles 'I' >>> pickles 'i' >>> pickles 'i' >>> pickles 'p'
Your student is learning about how Python stores data. They have now seen three types of variables in Python: strings, integers, and lists. Lists are able to store information in a specific order, and are indexed beginning from 0. This means that the last information stored will be at the position with a value one less than the length.
We have seen in our recently completed steps that lists have:
- Indexing that begins with 0
- Built-in attributes like length
Now we will look at existing commands and methods we can use with lists to modify their information.
Let’s return to our test list, which we will declare again as:
myList = ["I", "do not", "like", "pickles", "in","my", "sandwiches."]
Let’s try to add some words. Enter the following commands:
Type in myList to look at the contents of the list again.
Ask your student to describe what they think has happened. Encourage them to type in the command again, this time with a different number and a different word. What happens? Is it possible for the number to be too big? Let them experiment as much as they want.
>>> myList.insert(4,"or") >>> myList.insert(5,"tomatoes") >>> myList ['I', 'don’t', 'like', 'pickles', 'or', 'tomatoes', 'in', 'my', 'sandwiches']
Note: I encourage verbal descriptions of what is happening because when a child (or anyone, really) is given an instrument to use, it is easy to just begin banging away and typing things out. Describing code in words slows our brains down and is a good step in working on laying out the logic behind each line of code.
Here, the important concepts to touch upon with your student is that there are two terms inside the parentheses. Now you can discuss with your student that each of these terms is called a parameter in Python. The first parameter determines the index location at which the second parameter will be inserted. The second parameter, in this case, does not necessarily have to be a string; as we learned in the last lessons, a list can hold different types of variables – they do not all have to be the same!
Another command we can use is remove(). The remove method takes one parameter, which is the value of the entry to remove. By values, we mean the information stored in each list entry. Have your student make a copy of the list by typing
Have them remove the entry “don’t” from testList. Let them brainstorm and try things out – if they get confused, remind them of the previous exercises. Commands like insert and remove modify existing lists. So, we know that our command for remove will look like testList.remove().We also know that the remove() method needs a parameter because, otherwise, it would not know which list entry to remove!
Therefore, our resulting command is
>>> testList.remove("don’t") >>> testList ['I', 'like', 'pickles', 'or', 'tomatoes', 'in', 'my', 'sandwiches'] >>>
Now is a good time for us to discuss an important part of Python syntax. After that, we will do some more practice with list modification.
Parentheses Versus Brackets
Now that we have seen that arrays have built-in features like indexing and attribute like length, your student might have noticed that some commands require [brackets] and others (parentheses). This is part of Python syntax; syntax refers to the way that a programming language uses punctuation and spacing to organize its flow and operation.
In general, brackets indicate that data is being created or accessed. One example of data being created is the declaration of our variable myList. One example of data being accessed is when we got the first entry in the list by typing in myList.
Discussion questions to help your student understand:
- How can you tell that a variable is a list when you are creating it?
- What happens if we try to use parentheses to create a list?
- What do we use when we want to access a certain index value in a list?
As we discussed in the previous lesson, parameters are the inputs that we provide to each Python command, although not every command needs parameters. Parameters go in parentheses.
Discussion questions/test exercises to help your student understand:
- What is a parameter?
- What happens if we try to use brackets instead of parentheses for a list-modifying command like insert or remove?
- Why do you think it is important for there to be a difference between bracket usage and parentheses usage?
- This is an important and very fundamental concept! Bracket and parentheses differentiation is important because it avoids confusion between whether the programmer is giving a command or asking for information.
- Example: if we have listA=[2,3,4,5], then we have list entries that are numbers. If there were no difference between brackets and parentheses, then remove(2) would be confusing because we would not be able to tell if we wanted to remove the value 2 (at index 0), or the value 4 (which is at index 2)
Your student is learning about how to manipulate variables in Python; we value coding because it is able to handle large amounts of data at a time. By working with lists, your student is learning how to access data using parameter inputs and gaining important basic knowledge of syntax.
Pin for Later!
Katie is mom of two rambunctious boys and a self-proclaimed super nerd. With a background in neuroscience, she is passionate about sharing her love of all things STEM with her kids. She loves to find creative ways to teach kids computer science and geek out about coding and math. You can find her blogging at Teach Your Kids Code and in her spare time, documenting her families travels at Tear Free Travel.