This post may contain affiliate links. As an Amazon Associate I earn from qualifying purchases.
So You Want to Learn Python
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 coding with 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. 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 Beginners?
The commands and syntax (rules for how code must be laid out) in Python are relatively simple compared to some other programming languages. Another feature, to which we will be returning to time and time again and we build our abilities in learning Python, 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 help strengthen your own development projects.
How Can I Help My Kids to Learn Python?
Whether you are a teacher or parent, getting kids started learning Python is simple. Today we will be going over a simple lesson plan to get kids started using Python. 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.
What Concepts Will We Learn Today:
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 Part One: 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 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 Part Two: 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!
Python Tutorial for Kids Part Three: 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 Part Four: 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 Part Five: 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. Check back soon — teach your kids code will go in-depth into these with future articles! Welcome to Python.
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.