Intro
In this chapter we will be taking a look at the fundamental data-types. By the end you should be able to recognise the different data-types and what the use-cases for each one might be.
Watch the video version on: YouTube.
Data-Types?
First what is a data-type? All data stored in the computer must have a type otherwise a program would not be able to know how to handle it. This is similar to how data is useless on it’s own unless we have context, spoken about in different tutorial.
Boolean
Starting off with the Boolean type (may also be referred to as bool). It is the simplest data-type having only two possible states either true or false. This may seem familiar to binary where instead of true/false it is 1 or 0.
Take a look at the below example, how could we convert this to use boolean types and be more machine-friendly?
|
|
Using Booleans we could get a result such as shown below:
|
|
FACT: This text is actually called pseudocode, which is used to express the steps required for a program to be written.
Integer
Moving on to our next data-type, the integer (also known as int). This type stores whole numbers they can also be negative or positive.
A whole number is a number without the decimal point.
In the sentence below where is an integer used?
|
|
If you thought “18”, you would be correct. Since “175.5” is not a whole number.
Real / Float
We know now how to represent whole number, but what happens if we need to represent fractional numbers? Well we have the Real or Floating Point (also known as float) data-type. Like Integers they can also be negative or positive.
In the sentence below where are the Real numbers?
|
|
If you spotted “5.0” and 175.5 you would be correct. But why is “5.0” a Real? Although we would interpret it as being a whole number; a computer program would read it has a Real since the decimal point is shown.
Character
Storing human-readable text in programs are stored in the Character data-type (also known as char). This allows for a single alphanumeric character to be stored.
Characters are generally represented in code by surrounding the character with single quotes.
Here’s some examples:
'A'
'h'
'1'
'!'
FACT: In computer programs characters are stored in standard encoding format. This is generally either ASCII or Unicode.
String
Following on from the Character type. The String type (also known as str) is used to represent one or multiple characters.
Strings are generally represented in code by surrounding the characters with double quotes.
Here’s some examples:
"A"
"Abc"
"1234+"
Type Casting
What happens if we need our program to change the data-type of some data? We can use a process called casting to accomplish this. There are limitations to casting as depending on the type we are casting from and to can result in some data being lost in the process. Sometimes if used incorrectly casting can also cause programs to crash!
For example:
- Casting the string
"42"
to a integer would result in42
- Casting the float
9.5
to an integer would result in9
(we lost some data)
But why might we want to cast between data-types? In some circumstances certain pieces of code will require a specific data-type. If we wanted to perform some mathematical operations on a string we would first need to convert it into either a float or integer type, also if we are utilising some code not under our control; we will need to follow what data-types it expects.
Conclusion
In this chapter you should now know:
- Differences with the fundamental data-types
- What type casting is and what situations it may be used
FACT: All types covered in this section (apart from the String) are known as “primitive types”.