Working with multiple data types

A data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and the value of each property is as expected. Let’s take a look at some data types we work with at Relief Solutions.

1.Integer (int)
It is the most common numeric data type used to store numbers without a fractional component (-707, 0, 707).

2.Floating Point (float)
It is also a numeric data type used to store numbers that may have a fractional component like monetary values do (707.07, 0.7, 707.00). Please note that number is often used as a data type that includes both int and float types.

3.Character (char)
It is used to store a single letter, digit, punctuation mark, symbol, or blank space.

4.String (str or text)
It is a sequence of characters and the most commonly used data type to store text. Additionally, a string can also include digits and symbols, however, it is always treated as text. A phone number is usually stored as a string (+250-788-333333) but can also be stored as an integer (9996663333).

5.Boolean (bool)
It represents the values true and false. When working with the boolean data type, it is helpful to keep in mind that sometimes a boolean value is also represented as 0 (for false) and 1 (for true).

6.Enumerated type (enum)
It contains a small set of predefined unique values (also known as elements or enumerators) that can be compared and assigned to a variable of enumerated data type. The values of an enumerated type can be text-based or numerical. In fact, the boolean data type is a pre-defined enumeration of the values true and false. For example, if rock and jazz are the enumerators, an enumerated type variable genre can be assigned either of the two values, but not both. Assuming that you are asked to fill in your preferences on a music app and are asked to choose either one of the two genres via a dropdown menu, the variable genre will store either rock or jazz. With enumerated type, values can be stored and retrieved as numeric indices (0, 1, 2) or strings.

7.Array
Also known as a list, an array is a data type that stores a number of elements in a specific order, typically all of the same type. Since an array stores multiple elements or values, the structure of data stored by an array is referred to as an array data structure. Each element of an array can be retrieved using an integer index (0, 1, 2,…), and the total number of elements in an array represents the length of an array.

For example, an array variable genre can store one or more of the elements rock, jazz, and blues. The indices of the three values are 0 (rock), 1 (jazz), and 2 (blues), and the length of the array is 3 (since it contains three elements). Continuing on the example of the music app, if you are asked to choose one or more of the three genres and you happen to like all three (cheers to that), the variable genre will store all three elements (rock, jazz, blues).

8.Date
Needs no explanation; typically stores a date in the YYYY-MM-DD format (ISO 8601 syntax).

9.Time
Stores a time in the hh:mm:ss format. Besides the time of the day, it can also be used to store the time elapsed or the time interval between two events which could be more than 24 hours. For example, the time elapsed since an event took place could be 72+ hours (72:00:59).

10.Datetime
Stores a value containing both date and time together in the YYYY-MM-DD hh:mm:ss format.

11.Timestamp
Typically represented in Unix time, a timestamp represents the number of seconds that have elapsed since midnight (00:00:00 UTC), 1st January 1970. It is typically used by computer systems to log the precise date and time of an event, down to the number of seconds, in a format that is unaffected by time zones. Therefore unlike datetime, timestamp remains the same irrespective of your geographical location.