Introduction to Python for Kids Day 2

Are you interested in learning how to code in python? Did you read the material from day 1? If you haven’t, first go check out the introduction to python blog post before reading this blog post.

 

learn how to code in python for kids

 

learning python coding for kids

Changing Data Types

Going from an Integer to a String

What data type we are dealing with can make a difference.  You may encounter a situation where you are dealing with more than one data type.  If for example, you are dealing with an integer but we want to use it as a string, we can change its data type.

For example, suppose we have a variable minutes defined such that: minutes = 10.

Suppose we want to use this information as a part of a string concatenation.  

You may want to print out a statement that takes whatever is stored in the minutes variable and concatenate it to a string.

However, if we did something like:

print(“You have ” + minutes + ” left.”)

This would cause an ERROR though because the data stored in minutes is an integer and you’re trying to “add” it to a string.

Before we “add” it we can change the data in minutes to a string.  There is more than one way this can be done.

We could do:

minutes=10

minutes=str(minutes)  #here were are changing the data type to a string

print(“You have ” + minutes + ” left.”)

 

OR we can do:

minutes=10

print(“You have ” + str(minutes) + ” left.”)  #we can convert it to a string inside of our print function

 

Going from a String to an Integer

We can change a string value to an integer data type.  For example, suppose we have a “number” that for some reason is a string.  We will talk about this later

when we talk about the input function. Anyway, there will be times when you may have what looks like a number, but it is stored as a string.  

However, you may want to do some type of mathematical calculation with the value.  To do so, first you need to convert the string to an integer or a float.

For example, supposed we have a variable age that has been set to the value “40”

Currently, it is a string.  But what if we wanted to add the number 10 to it.

If we did:

age_10=age + 10

We would get an error because currently the data stored in age is the string “40”

What we’d need to do is to convert “40” to an integer or float.  We can convert it to an integer by doing something like this:

age=int(age)

age_10=age+10

OR we could do:

age_10=int(age) + 10

 

Challenge for you:

learn python coding for kids

Challenge:  Purchase 2 items (for example, apples and bread.  Give the cashier some money, and then figure out your change.  Print out the change received.

Hint:  Don’t forget that you may need to change the data type of the variable you have the change amount stored in before doing string concatenation.

Try the challenge before looking at possible answers below.

kids learning python coding

Possible answer:

apples=7

bread=2

total = apples+bread
print(total)
change=20-total
print(change)
print(“Your change back is $” + str(change))  #we needed to turn the data stored in the variable change into a string before “adding” it to another string

learn python coding for kids

How to Ask a User to Input Information

Some of the topics we’ve talked about are some of the data types in python, naming variables, string concatenation, and using a line break in python.

Today, we are going to talk about the input function.  We used the print function to output things, but what if we want to ask a user to input some information?

Remember, we are going to the Grand Canyon. Hooray!!! I hope that you want to go with us.  What if we wanted to ask someone if they wanted to go with us?

Using the input function we can ask our friend if she wants to go with us, and the computer will wait for her to input her answer.  

It’s a function, so we’ll use parentheses.

So for example, to create a situation where a user is asked if she wants to go with us AND the computer will wait for her to input the information, type the following:

input(“Do you want to go?”)

The computer is now waiting for someone to input an answer.  So we were able to have the computer print out something WITHOUT using the print function.

If we wanted there to be a space between the question mark and the user’s answer, put a space between the question mark and the ending quotation mark:

input(“Do you want to go? “)

 

learn python coding for kids

 

Using the Line Break inside the input function

What if we wanted the person’s answer to be on the next line?  How would we accomplish this?

We can type:  input(“Do you want to go? \n”)

learn python coding for kids

 

Exercise:

Using the input function, ask the user What city do you live in?

Try the exercise before looking at a possible answer below.

kids learning python coding

 

input(“What city do you live in? \n”)

 

learn python coding for kids

 

Using a Variable and the Input Function

It’s nice that we can ask someone some information and she gets to input that information, but what if we want to recall that information later?

We can create a variable and set it equal to our input function.

For example to store our friend’s answer to recall later we can do

answer=input(“Do you want to go? \n”)

Then type: print(answer) to see the information recalled.

Using the Input function and String Concatenation

Let’s ask someone’s age.  Then let’s print out something telling him his age in a sentence.

age=input(“What is your age? \n”)

print(“You are ” + age ” years old.”)

Notice, how I spaced things so the words printed out nicely in the last sentence.

Why was I able to do this string concatenation? It’s because when you use the input function, the data is a string.

Importance of Knowing what Data Types you have

What if we were to ask the user’s age, and then we wanted to calculate how old he’d be in 10 years?

We could something like this:

num1=input(“What is your age? \n”)

age_in10=num1+10

This last statement will cause an ERROR?  Why??

It turns out that when you use the input function, the data is a STRING.  So the variable num1 is a string.

Then when we created age_in10 we tried to add together the string num1 and the integer 10.  We can’t do this.  

First, let’s change num1 to an integer before trying to add it to the number 10.

So try something like:  age_in10=int(num1) + 10.

Then do print(age_in10) to see the result.

learn python coding for kids

Exercise:

  1. Create a program where you ask the user how much he paid for his plane ticket to the Grand Canyon.
  2. Then calculate what half of the cost would be.
  3. Then tell him you have a special offer where you will be giving him back the calculated amount of money.

Try the exercise before looking at a possible answer below.

 

kids learning python coding

cost=input(“How much money did you pay for your plane ticket to the Grand Canyon? \n”)

cost=int(cost)  #we turn the data that’s in cost into an integer because it was a string BUT we want to do a mathematical calculation using it

moneyback=cost/2

moneyback=str(moneyback)  #before we try to do a string concatentation, we change moneyback into a string


print(“We have a special offer where you will be getting back $” + moneyback + “!” )

 

learn python coding for kids

 

Homework Assignment 1:

We are at the airport, ready to go to the Grand Canyon. It’s a self-check in at the airport.  Create a program:

  1. Where you ask the person’s name and store the information as a variable.
  2. Ask the person’s destination and store it as a variable.  
  3. Greet the person by name.
  4. Tell them you hope that they have a nice time in their destination, and use the destination’s name.

 

Try the homework before looking at a possible answer below.

kids learning python coding

name=input(“What is your name? \n”)

dest=input(“What is your destination? \n”)

print(“Welcome ” + name ”  to the Raleigh-Durham airport.”)

print(“We hope you have a nice time in ” + dest)

 

learn python coding for kids

 

Homework Assignment 2:

We are at the airport, ready to go to the Grand Canyon. It’s a self-check in at the airport.  Create a program:

  1. Ask the traveler how many bags they are bringing with them on the trip.
  2. Charge them $25 for every bag they are bringing
  3. Print out a sentence that tells how much they are being charged.

Try the homework before looking at a possible answer below.

kids learning python coding

 

bags=input(“How many bags are you bringing on the airplane? \n”)

charge=25*int(bags)
print(charge)
print(“It will cost you $” + str(charge) +”.”)

Recommended Posts

Leave a Comment

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