Data within programs will behave in different ways depending on the data type. The main data types are:
String holds character data as text
Integer holds whole numbers
Float holds numbers with a decimal point
Boolean holds either ‘True’ or ‘False’
Characters are strung together to form words and sentences. It is convenient to treat these as a single variable the data type for this is called ‘string’.
str(MyString)
MyString = "Hello World!)
Item | Description |
---|---|
Name | Harry Potter |
Address | 29 Craft Avenue |
Telephone number | 022984848 |
Car Registration number | CX45 829 |
Colours | Blue |
You can perform very limited operations on strings, strings are not suitable for numeric operations.
The integer data type is used to store whole numbers – i.e. those without a decimal point.
1234 is an integer
1234.0 is not an integer
An integer can deal with both positive and negative whole numbers.
If you try and store a decimal number in an integer data type, it will cut off everything after the decimal point, which is likely to cause a problem with the program later down the line. This ‘type error’ can be quite difficult to spot.
int (MyInteger)
MyInteger = 1.234
The number in MyInteger will actally be 1, with the decimal part discarded.
The real data type also stores numbers. But, unlike integer, real is used to store numbers that use decimal points.
However it is important for the program to know how much data after the decimal point to keep track of.
As such, there are two common formats for real data – “floating point” (or “float”) and “double”‘, depending on how much data you want to keep after the decimal point.
float – uses 4 bytes (32 bits) for storage. It can store numbers up to six decimal places
double – uses 8 bytes (64 bits) for storage. It can store up to fifteen decimal places
For example, a floating point real number can be declared in Python like this:
float MyReal
MyReal = 1.223
For most programs, a precision of six decimal places is good enough, so float is used.
Computers are based on logic and so to handle a logic variable the Boolean data type is used.
The Boolean data type can only contain two values – either TRUE or FALSE.
In terms of a number, logic FALSE is normally a 0, and logic TRUE is 1
In Python for example, if you wish to keep track of a Boolean variable, the True and False statements set its value.
Like this
logged_in = false
We will discuss how to use boolean data and boolean operators in a later section on computational logic.