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).
print("My name is Drew!")
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?
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.
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!