Basic Data Types

4.1.2. Basic Data Types#

First, we’ll look at a few basic data storage types. We’ll also be including some code examples you can look at, though don’t worry yet if you don’t understand the code, since we’ll be covering these in more detail throughout the rest of the book.

Booleans (True / False)#

Binary consisting of 0s and 1s make it easy to represent true and false values, where 1 often represents true and 0 represents false. Most programming languages have built-in ways of representing True and False values.

Screenshot of the tweet from before, but with the blue checkmark highlighted

Fig. 4.4 A blue checkmark (at the time) was something an account either had or didn’t have, so it can be stored as a binary value.#

Booleans are often created when doing sort of comparison or test, like:

  • Do I have enough money in my wallet to pay for the item?

  • Does this tweet start with “hello” (meaning it is a greeting)?

Numbers#

Numbers are normally stored in two different ways:

Screenshot of the tweet from before, but with the numbers highlighted: replies, retweets, likes

Fig. 4.5 The number of replies, retweets, and likes can be represented as integer numbers (197.8K can be stored as a whole number like 197,800).#

When computers store numbers, there are limits to how much space is can be used to save each number. This limits how big (or small) the numbers can be, and causes rounding with floating-point numbers.

Additionally, programming languages might include other ways of storing numbers, such as fractions, complex numbers [d3], or limited number sets (like only positive integers).

Strings (Text)#

Computers typically store text by dividing the text into characters (the individual letters, spaces, numerals, punctuation marks, emojis, and other symbols). These characters are then stored in order and called strings (that is a bunch of characters strung together, like in Fig. 4.6 below).

A photo of a string banner with shiny individual letters hanging on it spelling "HAPPY BIRTHDAY"

Fig. 4.6 A physical string of the characters: “H”, “A”, “P”, “P”, “Y”, “ “, “B”, “I”, “R”, “T”, “H”, “D”, “A”, “Y”. (image source)#

In our example tweet, we can see some different pieces of information that might be represented with strings:

Screenshot of the tweet from before, but with pieces of text highlighted: The user name, twitter handle, and the tweet text

Fig. 4.7 The user name, twitter handle, and the tweet text can all be represented with strings.#

Text can be stored with extra formatting information, such as fonts and colors, in different document file formats [d4] like Word Documents, PDF files, html website files [d5], etc.

Note: We’ll demonstrate strings later in this chapter, and in more detail in Chapter 7: Trolling