4 - Types of Data

The programs that you write are going to have to deal with a whole bunch of different data. But it is important to realize that there are different types of data and that certain actions can only be performed on certain types of data. We can think of the different data types as fitting into several categories which are numerical, sequences, booleans and mappings. At this time we will just look at some of the types of data found within the numerical and sequences category.

Let us consider one of the most important pieces of data to you….your name! I’ll use the example name of drew (my name!) for this example. I think that you would agree that the name drew is a piece of data, but what type is it? You might be thinking that it is a word and you would be correct in this thought. But do you think the computer thinks that drew is a word? Nope! The computer just sees drew as a sequence of characters. The computer also sees rdew as a sequence of characters. We would see that rdew is not a word because we can read English and see that there is no word with that sequence of letters. But the computer only looks at the sequence of characters and does not look for any meaning in their order (unless of course we write a program that shows the computer how to look for meaning). So what do we call the type of data that is a specific sequence of characters? We call is a string. When we write strings we use quotation marks (‘single’ or “double”) to identify where the string starts and ends. Below there are four examples of how you would print out various strings. You can see that strings can contain all kinds of characters, such as spaces, numbers, and symbols (@,\$,%,etc).

Now there are other types of data found within the sequences category but for now we are going to move onto two numerical data types. If you asked all your friends how many pets they have at home what kind of values would you get? The table below shows some examples of what might end up with. What do you notice about all this data?

 2 0 1 3 4 1

Does anyone have 2.5 pets at home? I sure hope not! That would be strange. All the data is whole number! The proper term for these types of numbers is integers. Integers is type of numerical data where there is no fractional or decimal part to the value.

Now what if you asked your friend how tall they were in metres? Would the data that you collect be integers? Have a look at the example data below.

 1.43 1.15 1.81 1.56 1.62 1.23

I’m guessing you’ve noticed that this data is not integers because they are decimal/fractional values. We call this type of data floats. Floats are used when you have data that has decimal or fractional values. An easy way to figure out if you need to be using integers or floats is to ask yourself, “Can I have ½ or ¼ of this data item?”. If the answer is yes, then you want to use floats.

Ok, good! You now know a little about the following data types: strings, integers, and floats. Let’s try applying what you now know!

4.1 - Let’s try!

1. Write a program that outputs (prints) a string, integer and float. Use the template below to get your started.

 #print a stringprint()#print an integerprint()#print a floatprint()

1. Now write a program that adds up how many siblings you have. The template below should help guide you.

 #ask the user (input) how many sisters they havesisters = input()#ask the user (input) how many brothers they havebrothers = input()#now try to add them up to find total siblingsnumber_of_siblings = print(number_of_siblings)

1. What happened when you tried to add the two variables that contain the number of siblings together? Can you figure out what the problem is?

1. Any luck figuring it out? Well, it looks like there is a problem with the data types. When you ask the user for input using the input() function it always returns the entered data as a string. But you entered a integer right!??!?! Yes, you entered an integer but the computer sees anything that is being entered from an input() as a string. Let’s say you have 2 sisters then when you enter the integer 2 the computer actually thinks of it as the string “2”. Confusing I know, but the computer can’t just guess what types of data it thinks you are entering so it always, when using input(), makes it a string. Now think of what would happen when the computer tries to add together two strings. We can try it out again below.

 sum1 = "2" + "1"print(sum1)sum2 = "dog" + "cat"print(sum2)sum3 = “monkey” + “banana” + “long tail”print(sum3)#let's make "2" and "1" into integerssum4 = 2 + 1print(sum4)

1. Ok, so when adding strings together the computer assumes that we just want to join them. But what if we want to fix our original siblings calculator to actually add the integers together? Well, we need to convert the data that was entered into integers. Experiment with code below and use what you learn to update your original sibling calculator to work correctly.

 #convert to an integerint("3243")#convert to a stringstr(3243)#convert to a floatfloat(32.43)#let's test thingsnum1 = "12"num2 = "10"print(num1 + num2)print(int(num1) + int(num2))