This post may contain affiliate links. As an Amazon Associate I earn from qualifying purchases.
Welcome to PART TWO of our Learn Python series. If you missed PART ONE of our series make sure to check it out before moving onto this lesson!
Teaching Your Kids About Data
Programming often has a lot of words that sound intimidating to beginners. 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
Python Tutorial for Kids: All About Lists
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.