Introduction to Python for Kids

Anna, Andre, and the other kids in the STEM Club want to learn how to code. They asked Anna and Andre’s dad which language they should learn and he suggested Python.

Like there are different languages that we can learn to speak, there are different computer programming languages. Python is one such programming language.

Join the STEM Club as they learn how to code in Python.  

 

learn to code in python for kids

 

learn python coding for kids

Where will you be coding?

Anna and Andre’s Dad suggested that they use replit to do their coding.  By using replit they don’t have to download any software.  They can also share their code with each other when using replit as they learn to code.  You can also start a free account if you go to www.replit.com.  

To run “quicker” code you can type your code in the Console section.  After you type your code, just hit ENTER on your computer.

You can also run code in the Middle section (assuming your console is to the right of the Middle editor section).    To run your code from the Middle section, hit the RUN button.

(You can have the screens displayed side by side or stacked.  To change this setting, you can go to the Settings tab, and choose either side-by-side or stacked under the Layout option.)

learn python coding for kids

How to Make Comments 

As a programmer, you will want to sometimes leave yourself notes about what you were thinking about when you created a program.  Another good reason to write comments is if someone else comes behind you and looks at your code, having comments can make it easier for them to understand what you were doing in your program.   To create a comment, place a hashtag sign in front of the comment you want to make. The hashtag sign looks like this:  #  

An example comment is:

#Welcome to your first day of programming

Don’t worry about typing things exactly “right” as far as the proper syntax.  If you were to run your code, the computer knows that the things you have behind the # are comments.

You can type comments on their own lines, or you can type comments after code that you want the computer to read

For example, if I wanted the computer to print out the words Her name is Sue and then make a comment afterwards I can write:

print(“Her name is Sue”)  #this is my best friend’s name

We haven’t talked about the print function yet, but this will print out the words Her name is Sue, but also I was able to make a comment after the print function that the computer WILL see

as our comment.

some different data types in python learn python for kids

Some Different Data Types

Some introductory data types in Python are Integers, Strings, Float, and Boolean.  

Integers

Integers in Python are the same as Integers in Mathematics.  So Integers in Python is the set {….., -4, -3, -2, -1, 0, 1, 2, 3, 4, 5…..}.

Floats

Floats are decimals.  So examples of floats are 1.5, 2.1, 3.4.  Even if you have an “integer” BUT you add a decimal to it, it now is a float.  For example 3.0 will be considered

a float in python.

Strings

I generally think of strings as words, but anything that you put within quotation marks later will be considered a string.  To make something a string you can either use the double quotes or the single quotes.  You will begin and end the string with the quotation marks.  If you start with a double quote, end with the double quote marks.  Similarly with the single quote marks.  The space character is a part of your string. 

Examples of strings:  “Hi there.”       “My name is Andre.”       “10”        “My age is 8.”

Booleans

True and False are Boolean data types.  We are going to be checking to see if certain conditions are True or False.  Be careful, if you want to use these data types, you have to make sure that

you use a capital T and F.  So true and false are NOT considered boolean data values.

 learn python coding for kids

To verify the types of different things, you can type the following in the Console section:

type(5)

type(-1)

type(2.3)

type(-4.5)

type(“Hi there.”)

type(True)

type(False)

If you want to see the data types of different data values in the MIDDLE section of replit, then type things such as
print(type(3))

print(type(“Hi”))

print(type(True))

We will see examples later where having different data types can cause problems.

 

learn python coding for kids

Python is Case Sensitive

Notice how I told you that true is NOT considered a booelan value.  You must realize that python is CASE SENSITIVE, so True is NOT the same as true.

The word Hi is DIFFERENT from hi and is also DIFFERENT FROM  HI, ETC.

learn python coding for kids

How to Output Using the Print Function

A way to output things in python is to use the print function.  When using the print function, make sure you use parenthesis.  If I want to print out “My name is Anna.”  I can

do the following:   print(“My name is Anna.”)

Notice, because I had a STRING, I surrounded it with quotes.  I could have also done print(‘My name is Anna.’)

We can also print out numbers.  For example, we can do print(100)

If I do something like print(16/2), you will see that what gets returned is 8.0.  So now even though 16 and 2 were integers, the result is a float.

 

learn python coding for kids

Exercise Time:

  1. Print out a statement to tell others where you live.
  2. Print out a statement to tell others your age
  3. Print out the False boolean data value
  4. Print out the number 1000.

 

Try to do the exercises before looking at possible answers below.

 

kids learning python coding

 

print(“I live in North Carolina.”)

print(“I am 13 years old.”)   

print(False)

print(1000)

doing math in python learn to code for kids

We Can Do Math in Python

We can do mathematical calculations in python.  We can add, subtract, multiply, divide, use exponents, etc.  

To add, use the + sign

To subtract use the – sign

To multiply use the * sign

To divide use the / sign

To use exponents use the ** sign.  So for example 3 squared would be 3**2.  

Python uses the order of operations, so for example, 7+5*2 will be 7+10 = 17.

Try to make python do different calculations for you.

If you are in the Console section, you can for example type in 7*5+2 and hit ENTER and you will get an answer.

If you are in the Middle editor section , in order to make 7*5+2 print out,

do the following:  print(7*5+2)

 

learn python coding for kids

Exercise Time:

Use Python to calculate the following:

  1. 8*9 
  2. 5**2
  3. (8+4) / 5

 

Try to do the exercises before looking at possible answers below.

kids learning python coding

If you are in the middle section do the following:

  1. print(8*9 )
  2. print(5**2)
  3. print((8+4) / 5)

 

creating variables in python kids learn to code

Using Variables


So we can make python print things out. We can also have it do different mathematical calculations, but what if we want to recall that information for later use.  For example, suppose

I did 7+5 and I later want to use that information again, I can store that data in a variable. 

Naming Variables

When naming variables, make sure that you begin with a letter. You can use numbers as a part of the name BUT you can’t start with a number. You can use the underscore also in your name.

Remember that python is case sensitive so the variable Name is NOT the same as the variable name.

Create a variable name that is equal to your name.

For example:  name = “Andre”

Remember, since your name is a string you need to use quotation marks.

Then type print(name) to see that the information can be recalled later.

Create another variable called age, equal to your age.  For example age=10.

Then do print(age) to see that information can be recalled.

learn python coding for kids

Challenge for you:

Challenge : Create two variables, and multiply them.  Print out the result.

Try the challenge before looking at a possible answer below.

kids learning python coding

Possible answer:

a=5

b=2

c=a*b

print(c)

learn python coding for kids

Challenge for you:

  1. Purchase 2 items – for example apples and bread. 
  2. Give the cashier some money.
  3. Figure out your change. 
  4. Print out the change received.

Try the challenge before looking at a possible answer below.

kids learning python coding

apples=5
bread=4
total=apples+bread
mymoney=20
change=mymoney-total
print(change)

Redefining Variables

Suppose you have defined a variable. For example, suppose the cost of apples is $5 so that we do:

apples = 5.

What if the cost changes or for some reason we want to change the value stored in the variable apples.  We can easily do that.

We can redefine the variable by just setting it equal to a different value.

So we could have

apple=5

print( apples)  #this allows you to see that apples is equal to 5

apples = 7

print(apples) #this allows you to see that now apples is equal to the number 7

 

learn python coding for kids

Exercise:

  1. Create a variable called allowance.  
  2. Print out the variable to verify the value it has been set to.
  3. Redefine the variable such that now it is equal to 25.
  4. Print out the variable to see that the value has been changed to 25.

Try the challenges before looking at possible answers below.

kids learning python coding

Possible answer:

allowance = 20

print(allowance)

allowance = 25

print(allowance)

learn python coding for kids

Using the Line Break

Great news.  We’re going to the Grand Canyon.  Aren’t you excited!  Let’s print out the words Grand Canyon three times.  This is how:

print(“Grand Canyon” * 3)

Oopps. That didn’t space correctly. The space is also a character, so we must tell the computer to put a space in between each time it printed Grand Canyon.  

Let’s try this:  print(“Grand Canyon ” *3).  Ok, great, that worked.

What if we wanted it on 3 different lines without typing the same command 3 times.  We can use the LINE BREAK.  Using \n will tell the computer to go to the next line.

So let’s try print(“Grand Canyon \n” *3).  We’ll get Grand Canyon printed out 3 times and listed vertically. 

learn python coding for kids

String Concatenation

What if I wanted to print out my name on 3 different lines?  I could do 

print(“Shenek”)

print(“Catrell”)

print(“Alston”)  

But who wants to do that?  Let’s try instead to use string concatenation where we’ll use the + sign to “add” together strings.

print(“Shenek” + “Catrell” + “Alston”)

This however, does NOT print on 3 lines and does NOT space correctly.

Ok, let’s use string concatenation AND the line break together.

Type:  print(“Shenek \n” + “Catrell \n” + “Alston”).

This prints out my first, middle, and last name on three different lines.

learn python coding for kids

Exercise:

Print out your first, middle, and last names on three different lines.

Try to do the exercise before looking at a possible answer below.

kids learning python coding

print(“Shenek \n” + “Catrell \n” + “Alston”)

 

learn python coding for kids

Challenge for you:

Challenge 1:  Print the words” I can code.” 5 times on DIFFERENT Lines.

Try to do the challenge before looking at a possible answer below.

kids learning python coding

print(“I can code. \n” * 5)

learn python coding for kids

Some Things to Remember:

  1. If you want to make a comment, first put a hashtag sign.
  2. Be able to determine between an integer, float, string, and boolean data type.
  3. Remember, when dealing with a string, use quotes.
  4. Python is case sensitive.
  5. You can use the print function to output something.  Don’t forget to use parenthesis.
  6. We can create a variable if we want to store information to be recalled later.
  7. We can add strings together using string concatenation.

learn python coding for kids

 

Homework Assignment 1

  1. Pretend that there are a group of people who are planning to purchase an item together.  For example, you can pretend there are a group of 4 friends who are planning to buy a car together.
  2. Define a variable with the item you’re going to purchase as a group and assign the variable the cost of the item.
  3. Calculate how much each person is going to owe towards the purchase of the item (assuming that each person will pay the same amount).
  4. Print out how much each person owes.
  5. Print out a statement in the form of a sentence that tells how much each person owes.

Hint: You can’t concatenate a string and an integer.  So when trying to do #5 above, convert the amount owed to a string before trying to concatenate it to a string.

For example,

ball = 2

ball = str(2)    #this line will change the integer 2 into a string “2”

Try to do the homework before looking at a possible answer below.

kids learning python coding


group = 4
car = 2000
eachperson = car / group
print(eachperson)
print(“Each person owes ” + str(eachperson))

learn python coding for kids

Homework Assignment 2 

See if you can answer the following questions:

  1.  What data type are the following:     -4,    10.5,    True,     “I like to code.”    , 2.0,      ‘cloud’
  2. When dealing with a string you need to put what around it? 
  3.  Are False and false both boolean data types?
  4. Is it ok to start a variable name with a number?
  5. Can you redefine a variable, by setting it equal to a new data value?

Try to do the homework before looking at a possible answer below.

kids learning python coding

  1.  What data type are the following:     -4 is an integer        10.5 is a float        True is a Boolean     “I like to code.” is a string         2.0 is a float          ‘cloud’ is a string
  2. When dealing with a string you need to put what around it? Double or single quotation marks
  3.  Are False and false both boolean data types?  No, python is case sensitive so False and false are NOT the same.  False is a boolean data type but false is not.
  4. Is it ok to start a variable name with a number?  No, you can not start a variable name with a number.  A number can be included later in the name but not first.
  5. Can you redefine a variable, by setting it equal to a new data value?  Yes, you can give a variable a new value by setting it equal to a new data value.
Recommended Posts

Leave a Comment

0
Pinterest
fb-share-icon
Instagram
STEM book for kidslearn python coding for kids